Node.js Local HTTP Response Performance

I have a performance issue with the http Node.js http module.

I am writing a little Node script to make a web server that sends data for a specified duration (in seconds). The goal is to conduct speed tests between clients and the server. And local speed tests too. For this, I use a custom Readable, which I am 'pipe' for the http response. This custom Readable stops sending data after a specified duration. Data is a circular buffer of arbitrary values.

The code is as follows:

var http = require("http");
var Readable = require('stream').Readable;
var url = require('url');

var buff =  new Buffer(16384); // change this to bigger value for better local performances
buff.fill(0); // data is filled with 0s here, change to whatever

// this function creates a Readable that will provide data for <duration> seconds and then will nd.
function createTimedReadable(duration)
{
  var EndAt = Date.now() + 1000*duration; // when to end
  var rs = new Readable();
  rs.EndAt = EndAt;
  rs._read = function () {
    if (Date.now() < rs.EndAt)
      rs.push(buff);
    else
      rs.push(null);
  }
  return(rs);
}

// each client request will call this function
function onHTTPrequest(request, response) {
  var who = request.connection.remoteAddress + ":" + request.connection.remotePort;
  console.log("Request received from " + who);
  var duration = 10; // (actually we get it from the url query part)
  // send the header: it unknown size of binary data
  response.writeHead(200,
    {
    'Content-Type'  : 'octet-stream',
    'Cache-Control' : 'no-cache, no-transform'
  });

 // link (pipe) the Readable to the response
  var timedReadable = createTimedReadable(duration);
  timedReadable.pipe(response);
}

// main - start the server
http.createServer(onHTTPrequest).listen(8888);
console.log("Server has started");

To call the server within 10 seconds:

wget -O /dev/null http://serverip:8888

(/dev/null), , .

" " (2 ) , ( ) , Buffer ( ). . , :

with a 16k buffer, we get 188 MB/s
with a 128k buffer, we get 487 MB/s
with a 1M buffer, we get 558 MB/s
with a reference code written in C using a 4K buffer, we get 626 MB/s

' , GigaEthernet (Ge), , ~ 110 /, (, 10Ge) 188 / 16- . 4K-, C, Node Ge, 69 /.

, - , , . , , Readable. Bascially, "push" 8 16k , 1 128k (16k * 8).

, " "?

.

+4
2

. , , 8 128 , 1 1 . , . , , .

-, node C. , C node. , , . http- C? C .

0

, node 3 :

, .

1KiloBytes, 1KiloBytes , .

128Bytes, 1KiloBytes , 8 8 .

user568109, .

, . ? ?

C , node. node , C . unoptimized javascript, / , C writing to socket with a file descriptor.

, . node . , .

0

Source: https://habr.com/ru/post/1524850/


All Articles