Apachebench 请求计数和 Node.js 脚本计数器不匹配

发布于 2024-10-14 10:37:16 字数 1106 浏览 3 评论 0原文

毫无疑问我在做一些愚蠢的事情,但我在使用 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。

No doubt I'm doing something stupid, but I've been having problems running a simple node.js app using the Nerve micro-framework. Testing with apachebench, it seems that the code within my single controller is being invoked more frequently than the app itself is being called.

I've created a test script like so:

'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);
}());

Start the server. From another box, run a load test:

/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)

But the node script itself is printing all the way up to:

nc = 5003
rc = 5003    

In other words, the server is being called 5000 times but the controller code is being called 5003 times.

Any ideas what I'm doing wrong?

Updated

I changed the tone and content of this question significantly to reflect the help Colum, Alfred and GregInYEG gave me in realising that the problem did not lie with Redis or Nerve and probably lie with apachebench.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-10-21 10:37:16

程序:

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: [email protected]
npm info build Success: [email protected]
npm info build Success: [email protected]

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

$ git clone -q [email protected]: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 
gt;
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

Program:

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);
}

Conclusion

It works without any flaws on my computer:

$ 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: [email protected]
npm info build Success: [email protected]
npm info build Success: [email protected]

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

$ git clone -q [email protected]: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 
gt;
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文