的NodeJS响应速度和nginx的(nodejs response speed and nginx

2019-06-26 01:33发布

刚开始测试的NodeJS,并希望得到理解以下行为有所帮助:

实施例#1:

var http = require('http');
http.createServer(function(req, res){
    res.writeHeader(200, {'Content-Type': 'text/plain'});
    res.end('foo');
}).listen(1001, '0.0.0.0');

实施例#2:

var http = require('http');
http.createServer(function(req, res){
    res.writeHeader(200, {'Content-Type': 'text/plain'});
    res.write('foo');
    res.end('bar');
}).listen(1001, '0.0.0.0');

当测试响应时间在Chrome中:

示例#1 - 6-10ms
示例#2 - 200-220ms

但是,如果测试均通过nginx的proxy_pass例子

server{
    listen 1011;
    location / {
        proxy_pass http://127.0.0.1:1001;
    }
}

我得到这个:

示例#1 - 4-8ms
示例#2 - 4-8ms

我不是在任或的NodeJS nginx的专家,并询问是否有人能解释一下吗?

的NodeJS - v.0.8.1
nginx的 - V.1.2.2

更新:

感谢河马,我在我的使用和不使用nginx的服务器上进行测试与AB公司,并得到了相反的结果。

也加入到nginx的配置proxy_cache断

server{
    listen 1011;
    location / {
        proxy_pass http://127.0.0.1:1001;
        proxy_cache off;
    }
}

示例#1直接:

AB -n 1000 -c 50的http:// 127.0.0.1:1001/


    Server Software:        
    Server Hostname:        127.0.0.1
    Server Port:            1001

    Document Path:          /
    Document Length:        65 bytes

    Concurrency Level:      50
    Time taken for tests:   1.018 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Total transferred:      166000 bytes
    HTML transferred:       65000 bytes
    Requests per second:    981.96 [#/sec] (mean)
    Time per request:       50.919 [ms] (mean)
    Time per request:       1.018 [ms] (mean, across all concurrent requests)
    Transfer rate:          159.18 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.6      0       3
    Processing:     0   50  44.9     19     183
    Waiting:        0   49  44.8     17     183
    Total:          1   50  44.7     19     183

示例#1 nginx的:

AB -n 1000 -c 50的http:// 127.0.0.1:1011/


    Server Software:        nginx/1.2.2
    Server Hostname:        127.0.0.1
    Server Port:            1011

    Document Path:          /
    Document Length:        65 bytes

    Concurrency Level:      50
    Time taken for tests:   1.609 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Total transferred:      187000 bytes
    HTML transferred:       65000 bytes
    Requests per second:    621.40 [#/sec] (mean)
    Time per request:       80.463 [ms] (mean)
    Time per request:       1.609 [ms] (mean, across all concurrent requests)
    Transfer rate:          113.48 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.6      0       3
    Processing:     2   77  44.9     96     288
    Waiting:        2   77  44.8     96     288
    Total:          3   78  44.7     96     288

示例#2直接:

AB -n 1000 -c 50的http:// 127.0.0.1:1001/


    Server Software:        
    Server Hostname:        127.0.0.1
    Server Port:            1001

    Document Path:          /
    Document Length:        76 bytes

    Concurrency Level:      50
    Time taken for tests:   1.257 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Total transferred:      177000 bytes
    HTML transferred:       76000 bytes
    Requests per second:    795.47 [#/sec] (mean)
    Time per request:       62.856 [ms] (mean)
    Time per request:       1.257 [ms] (mean, across all concurrent requests)
    Transfer rate:          137.50 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.3      0       2
    Processing:     0   60  47.8     88     193
    Waiting:        0   60  47.8     87     193
    Total:          0   61  47.7     88     193

示例#2 nginx的:

AB -n 1000 -c 50的http:// 127.0.0.1:1011/


    Server Software:        nginx/1.2.2
    Server Hostname:        127.0.0.1
    Server Port:            1011

    Document Path:          /
    Document Length:        76 bytes

    Concurrency Level:      50
    Time taken for tests:   1.754 seconds
    Complete requests:      1000
    Failed requests:        0
    Write errors:           0
    Total transferred:      198000 bytes
    HTML transferred:       76000 bytes
    Requests per second:    570.03 [#/sec] (mean)
    Time per request:       87.715 [ms] (mean)
    Time per request:       1.754 [ms] (mean, across all concurrent requests)
    Transfer rate:          110.22 [Kbytes/sec] received

    Connection Times (ms)
                  min  mean[+/-sd] median   max
    Connect:        0    0   0.4      0       2
    Processing:     1   87  42.1     98     222
    Waiting:        1   86  42.3     98     222
    Total:          1   87  42.0     98     222


现在的结果看起来更加的逻辑,但仍有调用,当奇怪的延迟res.write()

我想这是(肯定看起来像)一个愚蠢的问题,但我仍然得到响应时间在浏览器与服务器配置(CentOS 6的),这具体的服务器(VPS)巨大差异。

在我家的电脑(Ubuntu的12),但与旧版本从本地主机的一切工作正常测试。


Answer 1:

偷看http.js揭示这种情况下,#1在自身的NodeJS特殊处理,某种捷径优化我猜的。

var hot = this._headerSent === false &&
            typeof(data) === 'string' &&
            data.length > 0 &&
            this.output.length === 0 &&
            this.connection &&
            this.connection.writable &&
            this.connection._httpMessage === this;

      if (hot) {
        // Hot path. They're doing
        //   res.writeHead();
        //   res.end(blah);
        // HACKY.

        if (this.chunkedEncoding) {
          var l = Buffer.byteLength(data, encoding).toString(16);
          ret = this.connection.write(this._header + l + CRLF +
                                      data + '\r\n0\r\n' +
                                      this._trailer + '\r\n', encoding);
        } else {
          ret = this.connection.write(this._header + data, encoding);
        }
        this._headerSent = true;

      } else if (data) {
        // Normal body write.
        ret = this.write(data, encoding);
      }

      if (!hot) {
        if (this.chunkedEncoding) {
          ret = this._send('0\r\n' + this._trailer + '\r\n'); // Last chunk.
        } else {
          // Force a flush, HACK.
          ret = this._send('');
        }
      }

      this.finished = true;


Answer 2:

我已经把你的示例文件,使用ab (阿帕奇基准)为基准HTTP服务器的性能合适的工具:

实施例1:

Concurrency Level:      50
Time taken for tests:   0.221 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      104000 bytes
HTML transferred:       3000 bytes
Requests per second:    4525.50 [#/sec] (mean)
Time per request:       11.049 [ms] (mean)
Time per request:       0.221 [ms] (mean, across all concurrent requests)
Transfer rate:          459.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.7      0       4
Processing:     1   11   6.4     10      32
Waiting:        1   11   6.4     10      32
Total:          1   11   6.7     10      33

实施例2:

Concurrency Level:      50
Time taken for tests:   0.256 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      107000 bytes
HTML transferred:       6000 bytes
Requests per second:    3905.27 [#/sec] (mean)
Time per request:       12.803 [ms] (mean)
Time per request:       0.256 [ms] (mean, across all concurrent requests)
Transfer rate:          408.07 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.6      0       4
Processing:     1   12   7.0     12      34
Waiting:        1   12   6.9     12      34
Total:          1   12   7.1     12      34

注意:

第二个例子是与第一个一样快。 小差异可能是由在代码中的附加功能调用,事实上,文件尺寸较大则与第一个引起的。



文章来源: nodejs response speed and nginx
标签: node.js nginx