AWS上运行的Node.js性能较差

我正在使用Node.js在AWS上运行一个非常简单的RESTful API。 API以'/ rest / users / jdoe'的forms接收请求并返回以下内容(全部在内存中完成,不涉及数据库):

{ username: 'jdoe', firstName: 'John', lastName: 'Doe' } 

与本地networking相比,Node.js + AWS上的这个API的性能很糟糕 – 在本地networking上只有9个请求/秒,而每秒2214个请求。 AWS正在运行m1.medium实例,而本地节点服务器是具有英特尔i7-950处理器的台式机。 试图弄清楚为什么如此巨大的性能差异。

使用Apache Bench的基准如下所示:

本地networking

10,000个并发100 /组的请求

 > ab -n 10000 -c 100 http://192.168.1.100:8080/rest/users/jdoe Document Path: /rest/users/jdoe Document Length: 70 bytes Concurrency Level: 100 Time taken for tests: 4.516 seconds Complete requests: 10000 Failed requests: 0 Write errors: 0 Total transferred: 2350000 bytes HTML transferred: 700000 bytes Requests per second: 2214.22 [#/sec] (mean) Time per request: 45.163 [ms] (mean) Time per request: 0.452 [ms] (mean, across all concurrent requests) Transfer rate: 508.15 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.4 0 2 Processing: 28 45 7.2 44 74 Waiting: 22 43 7.5 42 74 Total: 28 45 7.2 44 74 Percentage of the requests served within a certain time (ms) 50% 44 66% 46 75% 49 80% 51 90% 54 95% 59 98% 65 99% 67 100% 74 (longest request) 

AWS

具有100个并发组的1,000个请求(10,000个请求需要花费太长时间)

 C:\apps\apache-2.2.21\bin>ab -n 1000 -c 100 http://54.200.x.xxx:8080/rest/users/jdoe Document Path: /rest/users/jdoe Document Length: 70 bytes Concurrency Level: 100 Time taken for tests: 105.693 seconds Complete requests: 1000 Failed requests: 0 Write errors: 0 Total transferred: 235000 bytes HTML transferred: 70000 bytes Requests per second: 9.46 [#/sec] (mean) Time per request: 10569.305 [ms] (mean) Time per request: 105.693 [ms] (mean, across all concurrent requests) Transfer rate: 2.17 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 98 105 3.8 106 122 Processing: 103 9934 1844.8 10434 10633 Waiting: 103 5252 3026.5 5253 10606 Total: 204 10040 1844.9 10540 10736 Percentage of the requests served within a certain time (ms) 50% 10540 66% 10564 75% 10588 80% 10596 90% 10659 95% 10691 98% 10710 99% 10726 100% 10736 (longest request) 

问题:

  • AWS的连接时间为105毫秒(平均),而本地networking的连接时间为0毫秒。 我认为这是因为需要更多的时间才能打开到AWS的套接字,然后到本地networking上的服务器。 假设有来自全球多台机器的请求,那么在负载情况下是否有更好的性能需要完成?
  • 更严重的是服务器处理时间 – 本地服务器为45毫秒,AWS为9.9秒。 我无法弄清楚这里发生了什么。 服务器只按9.46个请求/秒。 这是花生!

任何对这些问题的深入了解。 如果在这样一个简单的应用程序上不能执行超级快速的操作,我很担心在Node + AWS上放置一个严重的应用程序。

以下是我的服务器代码的参考:

 var express = require('express'); var app = express(); app.get('/rest/users/:id', function(req, res) { var user = { username: req.params.id, firstName: 'John', lastName: 'Doe' }; res.json(user); }); app.listen(8080); console.log('Listening on port 8080'); 

编辑

单个请求隔离发送(-n 1 -c 1)

 Requests per second: 4.67 [#/sec] (mean) Time per request: 214.013 [ms] (mean) Time per request: 214.013 [ms] (mean, across all concurrent requests) Transfer rate: 1.07 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 104 104 0.0 104 104 Processing: 110 110 0.0 110 110 Waiting: 110 110 0.0 110 110 Total: 214 214 0.0 214 214 

10请求全部同时发送(-n 10 -c 10)

 Requests per second: 8.81 [#/sec] (mean) Time per request: 1135.066 [ms] (mean) Time per request: 113.507 [ms] (mean, across all concurrent requests) Transfer rate: 2.02 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 98 103 3.4 102 110 Processing: 102 477 296.0 520 928 Waiting: 102 477 295.9 520 928 Total: 205 580 295.6 621 1033 

结果使用wrk

正如安德烈·西多罗夫所build议的那样。 结果更好 – 每秒2821个请求:

 Running 30s test @ http://54.200.x.xxx:8080/rest/users/jdoe 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 137.04ms 48.12ms 2.66s 98.89% Req/Sec 238.11 27.97 303.00 88.91% 84659 requests in 30.01s, 19.38MB read Socket errors: connect 0, read 0, write 0, timeout 53 Requests/sec: 2821.41 Transfer/sec: 661.27KB 

所以它看起来像罪魁祸首是ApacheBench! 难以置信的!

这可能是一个问题(也见这个问题 )。 你的服务器代码没有错。 我build议尝试使用wrk负载testing工具进行基准testing。 你在我的t1.micro上的例子:

 wrk git:master ❯ ./wrk -t12 -c400 -d30s http://some-amazon-hostname.com/rest/users/10 ✭ Running 30s test @ http://some-amazon-hostname.com/rest/users/10 12 threads and 400 connections Thread Stats Avg Stdev Max +/- Stdev Latency 333.42ms 322.01ms 3.20s 91.33% Req/Sec 135.02 59.20 283.00 65.32% 48965 requests in 30.00s, 11.95MB read Requests/sec: 1631.98 Transfer/sec: 407.99KB