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);
buff.fill(0);
function createTimedReadable(duration)
{
var EndAt = Date.now() + 1000*duration;
var rs = new Readable();
rs.EndAt = EndAt;
rs._read = function () {
if (Date.now() < rs.EndAt)
rs.push(buff);
else
rs.push(null);
}
return(rs);
}
function onHTTPrequest(request, response) {
var who = request.connection.remoteAddress + ":" + request.connection.remotePort;
console.log("Request received from " + who);
var duration = 10;
response.writeHead(200,
{
'Content-Type' : 'octet-stream',
'Cache-Control' : 'no-cache, no-transform'
});
var timedReadable = createTimedReadable(duration);
timedReadable.pipe(response);
}
http.createServer(onHTTPrequest).listen(8888);
console.log("Server has started");
To call the server within 10 seconds:
wget -O /dev/null http:
(/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).
, " "?
.