performance - nodejs http 和 redis,只有 6000req/s

标签 performance node.js http redis benchmarking

测试node_redis基准测试,它显示 incr 超过 100000 ops/s

$ node multi_bench.js   
Client count: 5, node version: 0.10.15, server version: 2.6.4, parser: hiredis  
INCR,     1/5 min/max/avg/p95:    0/   2/   0.06/   1.00   1233ms total, 16220.60 ops/sec  
INCR,    50/5 min/max/avg/p95:    0/   4/   1.61/   3.00    648ms total, 30864.20 ops/sec  
INCR,   200/5 min/max/avg/p95:    0/  14/   5.28/   9.00    529ms total, 37807.18 ops/sec    
INCR, 20000/5 min/max/avg/p95:   42/ 508/ 302.22/ 467.00    519ms total, 38535.65 ops/sec

然后我在带有http服务器的nod​​ejs中添加redis

var http = require("http"), server,        

redis_client = require("redis").createClient();

server = http.createServer(function (request, response) {
        response.writeHead(200, {
                "Content-Type": "text/plain"
            });
    
        redis_client.incr("requests", function (err, reply) {
            response.write(reply+'\n');                                                                                          
            response.end();
        });
}).listen(6666);

server.on('error', function(err){
    console.log(err);
    process.exit(1);
});

用ab命令测试,只有6000req/s

$ ab -n 10000 -c 100 localhost:6666/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        localhost
Server Port:            6666

Document Path:          /
Document Length:        7 bytes

Concurrency Level:      100
Time taken for tests:   1.667 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1080000 bytes
HTML transferred:       70000 bytes
Requests per second:    6000.38 [#/sec] (mean)
Time per request:       16.666 [ms] (mean)
Time per request:       0.167 [ms] (mean, across all concurrent requests)
Transfer rate:          632.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       2
Processing:    12   16   3.2     15      37
Waiting:       12   16   3.1     15      37
Total:         13   17   3.2     16      37

Percentage of the requests served within a certain time (ms)
  50%     16
  66%     16
  75%     16
  80%     17
  90%     20
  95%     23
  98%     28
  99%     34
 100%     37 (longest request)

上次我测试“hello world”,它达到了 7k req/s

Requests per second:    7201.18 [#/sec] (mean)

如何剖析redis在http中性能下降的原因?

最佳答案

我认为您误解了 multi_bench 基准测试的结果。

首先,此基准测试将负载分散到 5 个连接上,而您的 node.js 程序中只有一个。更多连接意味着更多通信缓冲区(基于每个套接字分配)和更好的性能。

然后,虽然 Redis 服务器能够维持 100K op/s(前提是您打开多个连接,和/或使用流水线),但 node.js 和 node_redis 无法达到这个水平。 multi_bench 的运行结果表明,当不使用流水线时,只能实现 16K op/s。

Client count: 5, node version: 0.10.15, server version: 2.6.4, parser: hiredis  
INCR,     1/5 min/max/avg/p95:    0/   2/   0.06/   1.00   1233ms total, 16220.60 ops/sec  

这个结果意味着在没有流水线和 5 个并发连接的情况下,node_redis 能够全局处理 16K op/s。请注意,测量 16K op/s 的吞吐量而仅发送 20K ops(multi_bench 的默认值)不是很准确。您应该增加 num_requests 以获得更高的准确性。

第二个基准测试的结果并不令人惊讶:您添加了一个 http 层(解析起来比 Redis 协议(protocol)本身更昂贵),仅使用 1 个到 Redis 的连接,而 ab 尝试打开 100 个到 node.js 的并发连接,最终获得 6K op/s,与“Hello world”HTTP 服务器相比,导致 1.2K op/s 的吞吐量开销。你期待什么?

您可以尝试通过利用 node.js 集群功能来获得更多性能,as described in this answer .

关于performance - nodejs http 和 redis,只有 6000req/s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17961061/

相关文章:

angularjs - 如何在一个 app.get 请求中返回多个 Mongodb 集合?

node.js - 使用 axios 调用登录 API 时,当我的凭据在 catch 中错误时,我没有收到任何响应

node.js - 在 Heroku 上部署 Nodejs 无法提供位于子文件夹中的静态文件

python - 如何在 python Flask 框架中发送 zip 文件?

jquery - 当让其他站点直接使用您站点中的脚本时需要考虑哪些事项?

multithreading - OpenMP的哪个线程正在执行最多的工作?

javascript - 如何提高 Html5 Canvas 性能

http - 接收大文件的Web API的哪种http请求方法

iphone - 适用于 Objective-C/iPhone 的良好 HTTP 库?

performance - GraphViz Dot的生成时间非常长