nodejs 请求接口在高并发下耗时很大,而单个请求非常快

2016-11-03 11:30:34 +08:00
 tofishes

情况: 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

请问哪位朋友有没有解决这类问题的经验?

23740 次点击
所在节点    Node.js
46 条回复
penjianfeng
2016-11-03 11:38:39 +08:00
关注下,公司一个系统准备换成 node 跑,有结果后希望分享下:-)
tofishes
2016-11-03 11:45:54 +08:00
@penjianfeng 好的,目前就这个并发问题了。
janxin
2016-11-03 11:47:49 +08:00
是不是只开了一个进程?
mcfog
2016-11-03 11:49:27 +08:00
所以 8080 上是个无限牛逼的后端接口,然后 9190 上是你的问题的 node ,里面的逻辑是用 request 捅 8080 ,然后并发有问题?

没记错的话, request 库是带连接池的,默认并发只开了 5 ,所以你用并发 500 去压耗时很长是正常的
tofishes
2016-11-03 12:33:04 +08:00
@janxin 没有做任何进程操作,默认的简单程序
tofishes
2016-11-03 12:42:32 +08:00
@mcfog 你理解反了, 8080 上是 node sever, 9190 是后端接口,并发 node 上一个地址,该地址又用了 request 去请求接口,统计了一下 request 开始到完成的耗时。 然后你说的 request 连接池是 'pool': { maxSockets: 5000 } ?改为 5000 也没有明显提升。
ibigbug
2016-11-03 12:47:40 +08:00
你看下系统 cpu 内存 和 load ?
xxxyyy
2016-11-03 12:48:04 +08:00
@tofishes 你直接压后端的接口的耗时是多少?
sherlocktheplant
2016-11-03 12:50:56 +08:00
看看是否是内存不足 发生内存换页
tofishes
2016-11-03 13:09:05 +08:00
@xxxyyy 直接压后端耗时也很小的,所以是前端请求问题。
ab 压前端转发:
Requests per second: 171.05 [#/sec] (mean)
Time per request: 1169.261 [ms] (mean)
Time per request: 5.846 [ms] (mean, across all concurrent requests)
Transfer rate: 1981.31 [Kbytes/sec] received

ab 直接压后端接口:
Requests per second: 858.12 [#/sec] (mean)
Time per request: 233.068 [ms] (mean)
Time per request: 1.165 [ms] (mean, across all concurrent requests)
Transfer rate: 190.23 [Kbytes/sec] received
tofishes
2016-11-03 13:11:28 +08:00
@ibigbug
@sherlocktheplant 我都是在本地测试,只有后端接口是局域网内一个服务器, mac pro 13 中配,应该不是你们所说的问题
sherlocktheplant
2016-11-03 13:15:38 +08:00
@tofishes 发下转发的代码
powerfj
2016-11-03 13:16:27 +08:00
你这样测试很可能测试的是 request 请求的后端的接口的返回吧, 很可能是后面的接口不行了
sorra
2016-11-03 13:23:15 +08:00
把每个步骤的耗时都弄清楚一些。注意 console.log 也可能耗时。
网上宣传的高并发是高端服务器跑出来的,普通机器不要期望太高。
smallpath
2016-11-03 13:27:51 +08:00
先用 nginx 把请求转发到后端接口, 压一下看问题是不是 node 层的。
确定是 node 层的问题后,换一个请求库例如 superagent 跑一下,看看是不是所有库都有这个问题。
我们之前不是局域网时 ab 也是这样,但最后发现是本地带宽跑满了,解决后就没问题了
zhuangzhuang1988
2016-11-03 13:30:59 +08:00
用 visualstudio + node 扩展看下, 里面带有性能分析工具的
smallpath
2016-11-03 13:45:57 +08:00
superagent 跑起来还是有延迟的话,再换原生 http 模块试试,当然你有权限的话可以直接看后台接口的请求耗时对不对。
我觉得把 node 层请求用 nginx 转给后台可能有些效果
xxxyyy
2016-11-03 13:55:29 +08:00
@tofishes 是不是你的 node 版本问题,我用 node.js 7.0 测你给出来的代码,每个请求的耗时相差不大(第一个与最后一个相差都不超过 80ms )
shanelau
2016-11-03 14:37:45 +08:00
做排队啊。
关注下设备的性能,是什么原因找到的,网络还是 cpu 还是内存
tofishes
2016-11-03 14:40:59 +08:00
@xxxyyy 我从 v6.9.1 升级到了 v7.0.0 ,耗时减少一半,但依然有超过 1000ms 的。 api 地址干脆使用了 http://localhost ,是本机 nginx 默认页面,耗时从 150ms ~ 500ms 左右都有。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/317678

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX