毫无疑问,我在做一些愚蠢的事情,但是我在使用Nerve Micro框架运行一个简单的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。

最佳答案

程序:

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 $>
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

08-28 09:56