performance - Much slower response in node.js HTTP server with smaller files -
i developing performance test simple http server in node.js:
var http = require('http'); var fs = require('fs'); var no_cache = true; var filecache; var sendfile = function(conn, file) { conn.writehead(200, {'content-type': 'text/html', 'content-length': file.length}); conn.write(file); conn.end(); } http.createserver(function (req, res) { if (no_cache || filecache == undefined) { fs.readfile('index.html', function(err, file) { filecache = file; sendfile(res, filecache); }); } else { sendfile(res, filecache); } }).listen(8080, 'localhost');
(code can found here).
the server reads file "index.html" , returns it. realize server give result in 1-3 ms if file greater or equal 65483 bytes (very close 2^16 not exact), , if file smaller, last 38-40 ms give response (index.html file 65483 bytes can found here). headers in response 128 bytes:
content-type: text/html content-length: 65483 date: thu, 14 may 2015 13:58:21 gmt connection: keep-alive
the first 2 headers set server, , last 2 set automatically middleware. file size plus first 2 headers 65533 bytes, pretty close 65535 (2^16 - 1), difference because of carriage return in 2 headers.
this behaviour looks strange me, bigger files should last more time being read, , difference in time big.
i used node.js 0.10.38 , 0.12.2 same results. performance tests, used jmeter 2.13.
for record, behaviour not happen using vert.x same file , same test plan in jmeter (with vert.x server, bigger files take more time read).
if knows cause (and how avoid small files), curious know.
as bayou.io pointed, behaviour caused tcp nodelay. added server:
server.on("connection", function (socket) { socket.setnodelay(true); });
and giving response within 1-3 ms expected.
Comments
Post a Comment