【问题标题】:Apachebench request count and Node.js script counter don't matchApachebench 请求计数和 Node.js 脚本计数器不匹配
【发布时间】:2011-01-25 23:39:58
【问题描述】:

毫无疑问,我在做一些愚蠢的事情,但是我在使用 Nerve 微框架运行一个简单的 node.js 应用程序时遇到了问题。使用 apachebench 进行测试,似乎我的单个控制器中的代码被调用的频率高于应用程序本身的调用频率。

我已经创建了一个这样的测试脚本:

'use strict';

(function () {
    var path = require('path');
    var sys = require('sys');
    var nerve = require('/var/www/libraries/nerve/nerve');
    var nerveCounter = 0;

    r_server.on("error", function (err) {
        console.log("Error " + err);
    });

    var app = [
        ["/", function(req, res) {
            console.log("nc = " + ++nerveCounter);
       }]
    ];

    nerve.create(app).listen(80);
}());

启动服务器。从另一个盒子运行负载测试:

/usr/sbin/ab -n 5000 -c 50 http://<snip>.com/
...
Complete requests:      5000
...
Percentage of the requests served within a certain time (ms)
...
 100%    268 (longest request)

但节点脚本本身一直打印到:

nc = 5003
rc = 5003    

换句话说,服务器被调用了 5000 次,但控制器代码被调用了 5003 次。

任何想法我做错了什么?

更新

我显着改变了这个问题的语气和内容,以反映 Colum、Alfred 和 GregInYEG 给我的帮助,让我意识到问题不在于 Redis 或 Nerve,而可能在于 apachebench。

【问题讨论】:

  • 您能否添加一些代码来增加文件,因为您的脚本可能会发生一些奇怪的事情。不是 Redis。
  • 感谢 Colum 的想法 - 我添加了几个全局计数器(见上文)。计数器也运行到 5003 - 证明问题不在于 Redis。
  • p.s:我认为您应该完全删除 node_redis 代码,因为即使您更新了标题,它仍然可能看起来像 node_redis 代码是罪魁祸首。
  • 看起来 Apache Bench 可以发送比您请求的更多的请求。 “'ab' 工具实际上确实可以正确衡量其请求的完成情况,但它实际上可能发送的请求比您要求的要多。这是因为它计算的是回复而不是请求。”有关更多信息,请参阅此博客文章:adrianotto.com/2010/05/apache-benchmark-ab-is-not-exact
  • 感谢 GregIn - 确认这似乎是问题所在。我用 Express 而不是 Nerve 重新运行了测试,问题再次出现。

标签: node.js apachebench


【解决方案1】:

程序:

const PORT = 3000;
const HOST = 'localhost';
const express = require('express');
const app = module.exports = express.createServer();
const redis = require('redis');
const client = redis.createClient();

app.get('/incr', function(req, res) {
    client.incr('counter', function(err, reply) {
        res.send('incremented counter to:' + reply.toString() + '\n');
    });
});

app.get('/reset', function(req, res) {
    client.del('counter', function(err, reply) {
        res.send('resetted counter\n');
    });
});

app.get('/count', function(req, res) {
    client.get('counter', function(err, reply) {
        res.send('counter: ' + reply.toString() + '\n');
    });
});

if (!module.parent) {
    app.listen(PORT, HOST);
    console.log("Express server listening on port %d", app.address().port);
}

结论

它在我的电脑上运行没有任何缺陷:

$ cat /etc/issue
Ubuntu 10.10 \n \l

$ uname -a
Linux alfred-laptop 2.6.35-24-generic #42-Ubuntu SMP Thu Dec 2 01:41:57 UTC 2010 i686 GNU/Linux

$ node -v
v0.2.6

$ npm install express hiredis redis
npm info build Success: redis@0.5.2
npm info build Success: express@1.0.3
npm info build Success: hiredis@0.1.6

$  ./redis-server --version
Redis server version 2.1.11 (00000000:0)

$ git clone -q git@gist.github.com:02a3f7e79220ea69c9e1.git gist-02a3f7e7; cd gist-02a3f7e7; node index.js

$ #from another tab

$ clear; curl http://localhost:3000/reset; ab -n 5000 -c 50 -q http://127.0.0.1:3000/incr > /dev/null; curl http://localhost:3000/count;

resetted counter
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 127.0.0.1 (be patient)
Completed 500 requests
Completed 1000 requests
Completed 1500 requests
Completed 2000 requests
Completed 2500 requests
Completed 3000 requests
Completed 3500 requests
Completed 4000 requests
Completed 4500 requests
Completed 5000 requests
Finished 5000 requests


Server Software:        
Server Hostname:        127.0.0.1
Server Port:            3000

Document Path:          /incr
Document Length:        25 bytes

Concurrency Level:      50
Time taken for tests:   1.172 seconds
Complete requests:      5000
Failed requests:        4991
   (Connect: 0, Receive: 0, Length: 4991, Exceptions: 0)
Write errors:           0
Total transferred:      743893 bytes
HTML transferred:       138893 bytes
Requests per second:    4264.61 [#/sec] (mean)
Time per request:       11.724 [ms] (mean)
Time per request:       0.234 [ms] (mean, across all concurrent requests)
Transfer rate:          619.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       7
Processing:     4   11   3.3     11      30
Waiting:        4   11   3.3     11      30
Total:          5   12   3.2     11      30

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     13
  75%     14
  80%     14
  90%     15
  95%     17
  98%     19
  99%     24
 100%     30 (longest request)
counter: 5000

【讨论】:

  • 非常感谢 Alfred 抽出宝贵时间查看此内容。问题绝对不在于 Redis - 我添加的全局计数器显示“/”代码以某种方式被调用超过 5000 次。当我换出 Nerve 代码并添加到您的 Express 代码中时,应用程序开始自行运行 - 所以问题肯定出在我的应用程序或 Nerve 库本身。我将更新我的问题的标题,以便更清楚问题所在 - 也许神经作者会看看它并告诉我我做错了什么......
  • 非常gppd!我认为无论如何你都应该使用 express 。它是最受欢迎的维护 node.js 开发框架!你为什么用神经?最新提交是 6 月 11 日。 P.S: Nerve 甚至不在 npm 中!
  • 之所以使用它,是因为我读了一些关于 Nerve 的博客文章,但你是对的 - 它显然没有保持最新状态。现在搬到快递。我在 Nerve GitHub 项目中留下了一个指向这个问题的问题,以防 grutter 想要回复/解决问题。
  • 好的很好:)。 Express.js 是一个非常好的库:P。 PS:感谢您的批准:)
  • 我是来自 github 的 gjritter。正如 Alfred 所说,您最好使用 express。它得到了很好的维护和更新,而我已经有一段时间没有更新神经了。有机会我会尝试调查这个问题。
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多