情况: request.js 库请求接口, express.js 做 server ,实现了 curl http://localhost:8080/proxy-api 本地一个地址,在 router 里用 request.js 请求接口,统计了一下请求耗时,单个请求耗时很低,如下:
get http://ip:9190/user/getUserInfo 13 ms
然后分别使用 webbench 和 ab 做并发测试,并发 500 ,发现接口有非常大的耗时。
# 测试命令
ab -n 1000 -c 200 -r http://localhost:8080/proxy-api
webbench -t 10 -c 500 http://localhost:8080/proxy-api
# 截取部分响应耗时:
get http://ip:9190/user/getUserInfo 2019 ms
cost time: 2020
get http://ip:9190/user/getUserInfo 2062 ms
cost time: 2062
get http://ip:9190/user/getUserInfo 2064 ms
cost time: 2065
get http://ip:9190/user/getUserInfo 2063 ms
cost time: 2063
get http://ip:9190/user/getUserInfo 2062 ms
cost time: 2063
get http://ip:9190/user/getUserInfo 2063 ms
cost time: 2063
get http://ip:9190/user/getUserInfo 2061 ms
cost time: 2062
get http://ip:9190/user/getUserInfo 2063 ms
cost time: 2064
get http://ip:9190/user/getUserInfo 2063 ms
...
...
get http://ip:9190/user/getUserInfo 1362 ms
cost time: 1362
get http://ip:9190/user/getUserInfo 1361 ms
cost time: 1362
get http://ip:9190/user/getUserInfo 1362 ms
cost time: 1362
get http://ip:9190/user/getUserInfo 1362 ms
cost time: 1362
get http://ip:9190/user/getUserInfo 1362 ms
cost time: 1362
get http://ip:9190/user/getUserInfo 1363 ms
cost time: 1363
get http://ip:9190/user/getUserInfo 1362 ms
cost time: 1362
...
...
get http://ip:9190/user/getUserInfo 1006 ms
cost time: 1006
get http://ip:9190/user/getUserInfo 627 ms
cost time: 628
get http://ip:9190/user/getUserInfo 629 ms
cost time: 629
get http://ip:9190/user/getUserInfo 628 ms
cost time: 629
get http://ip:9190/user/getUserInfo 1403 ms
cost time: 1403
get http://ip:9190/user/getUserInfo 1402 ms
请问哪位朋友有没有解决这类问题的经验? +++++++++++++++++ 2016-11-03 13:00:06 补充一下,简单测试问题的代码,序号输出是无序的,说明是异步,只不过越到后面越耗时:
const request = require('request').defaults({
pool: { maxSockets: 5000 }
});
const c = 500;
const api = '替换为一个api';
function doGet(i) {
const start = Date.now();
const index = i;
request.get(api, () => {
const end = Date.now() - start;
console.log(`${index}: ${end}ms`);
});
}
for (let i = 0; i < c; i++) {
doGet(i);
}
执行结果:
3: 570ms
1: 582ms
5: 580ms
7: 580ms
9: 580ms
11: 580ms
2: 582ms
0: 591ms
4: 582ms
6: 582ms
12: 582ms
10: 582ms
8: 583ms
14: 583ms
13: 583ms
16: 583ms
17: 584ms
20: 583ms
22: 583ms
24: 583ms
26: 583ms
28: 582ms
30: 582ms
...
457: 2262ms
467: 2263ms
469: 2263ms
237: 2283ms
474: 2267ms
471: 2267ms
493: 2269ms
475: 2272ms
479: 2271ms
477: 2272ms
485: 2272ms
483: 2273ms
481: 2273ms
487: 2273ms
489: 2274ms
495: 2273ms
497: 2273ms
499: 2274ms
491: 4380ms