预备知识:
wrk压测工具(一款简单的HTTP压测工具)
Usage: wrk <options> <url>
Options:
-c, --connections <N> Connections to keep open
-d, --duration <T> Duration of test
-t, --threads <N> Number of threads to use
-s, --script <S> Load Lua script file
-H, --header <H> Add header to request
--latency Print latency statistics
--timeout <T> Socket/request timeout
-v, --version Print version details
Numeric arguments may include a SI unit (1k, 1M, 1G)
Time arguments may include a time unit (2s, 2m, 2h)
压测接口命令
./wrk -t16 -c1000 -d30s http://172.17.xxx.xxx:8080/xxx/process?imei=xxxx\&oaid=xxxx\&idfa=\&os=1
这里使用 Spring WebFlux 和自定义线程池来提高并发量
核心脱敏代码如下
package com.szlm.rtaserver.work.controller;
import com.szlm.rtaserver.work.Utils.R;
import com.szlm.rtaserver.work.entity.RtaRequestVo;
import com.szlm.rtaserver.work.handler.MessageHandler;
import com.szlm.rtaserver.work.service.TaoBaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Title: TaoBaoController
* @Description:
* @author: tangyao
* @date: 2022/3/11 10:16
* @Version: 1.0
*/
@Slf4j
@RestController()
@RequestMapping("/xxxx")
public class TaoBaoRtaController {
@Autowired
TaoBaoService taoBaoService;
@Autowired
ThreadPoolExecutor threadPoolExecutor;
@Autowired
private MessageHandler messageHandler;
@GetMapping("/process")
public Mono<R> process(@RequestParam("imeiMd5") String imeiMd5,
@RequestParam("oaidMd5") String oaidMd5,
@RequestParam("idfaMd5") String idfaMd5,
@RequestParam(value = "os") int os) {
RtaRequestVo rtaRequestVo = new RtaRequestVo();
rtaRequestVo.setImeiMd5(imeiMd5);
rtaRequestVo.setOaidMd5(oaidMd5);
rtaRequestVo.setIdfaMd5(idfaMd5);
rtaRequestVo.setOs(os);
if (StringUtils.isEmpty(imeiMd5) && StringUtils.isEmpty(oaidMd5) && StringUtils.isEmpty(idfaMd5)) {
return Mono.create(monoSink -> {
monoSink.success(R.ok().setData("imeiMd5 , oaidMd5 ,idfaMd5三者至少提供一个"));
});
}
return this.messageHandler.rtaHandler(rtaRequestVo);
}
}
package com.szlm.rtaserver.work.handler;
import com.szlm.rtaserver.work.Utils.R;
import com.szlm.rtaserver.work.entity.RtaRequestVo;
import com.szlm.rtaserver.work.service.TaoBaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @Classname MessageHandler
* @Description TODO
* @Date 2022/3/14 9:13 下午
* @Author by tangyao
*/
@Component
@Slf4j
public class MessageHandler {
@Autowired
TaoBaoService taoBaoService;
@Autowired
ThreadPoolExecutor threadPoolExecutor;
public Mono<R> rtaHandler(RtaRequestVo rtaRequestVo) {
return Mono.create(monoSink ->
CompletableFuture.supplyAsync(
() -> taoBaoService.callInterfaceTaoBao(rtaRequestVo)
, threadPoolExecutor)
.whenComplete((aBoolean, e) -> {
if (e != null) {
log.error(e.getMessage());
}
R r;
if (aBoolean == null) {
String msg = "您的请求过于频繁,请稍后重试您的请求过于频繁,请稍后重试;";
log.info(msg);
r = R.error(msg);
} else {
r = R.ok().setData(aBoolean);
}
monoSink.success(r);
}));
}
}
带宽100m,
调用淘宝接口的connectionTimeout和readTimeout为300ms,
压测时长 120s
测试直接调用淘宝接口的QPS为 16500左右
调用自己接口统计
总次数 | QPS | 接口超时 | 返回值rsp的errcode为null或errcode!=0 | 请求成功率 |
---|---|---|---|---|
1563177 | 13019 | 598 | 45907 | 97.06% |
1563652 | 13022 | 371 | 37998 | 97.55% |
1559918 | 12992 | 109 | 40246 | 96.99% |