Число запросов Apachebench и счетчик скриптов Node.js не совпадают - PullRequest
2 голосов
/ 26 января 2011

Без сомнения, я делаю что-то глупое, но у меня были проблемы с запуском простого приложения node.js с использованием микро-фреймворка Nerve.При тестировании с помощью 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 раза.

Есть идеи, что я делаю не так?

Обновлено

Я изменил тон исодержание этого вопроса в значительной степени отражает помощь, оказанную мне Колумом, Альфредом и GregInYEG в понимании того, что проблема не в Redis или Nerve и, вероятно, в apachebench.

1 Ответ

3 голосов
/ 26 января 2011

Программа:

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