nodejs response speed and nginx

后端 未结 2 773
难免孤独
难免孤独 2021-01-04 22:27

Just started testing nodejs, and wanted to get some help in understanding following behavior:

Example #1:

var http = require(\'http\');
http.create         


        
相关标签:
2条回答
  • 2021-01-04 23:05

    Peeking into http.js reveals that case #1 has special handling in nodejs itself, some kind of a shortcut optimization I guess.

    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;
    
    0 讨论(0)
  • 2021-01-04 23:07

    I've took you examples files and used ab (Apache Benchmark) as a proper tool for benchmarking HTTP server performance:

    Example 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
    

    Example 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
    

    Note:

    The second example is as fast as the first one. The small differences are probably caused by the the additional function call in the code and the fact that the document size is larger then with the first one.

    0 讨论(0)
提交回复
热议问题