Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Last active February 3, 2016 21:25
Show Gist options
  • Save trevnorris/d52da08fa551c01843c0 to your computer and use it in GitHub Desktop.
Save trevnorris/d52da08fa551c01843c0 to your computer and use it in GitHub Desktop.
Output the number of setHeader() operations per second
'use strict';
const http = require('http');
const net = require('net');
const print = process._rawDebug;
const FIELDS = isNaN(process.argv[2]) ? 128 : process.argv[2] >>> 0;
const PORT = 8080;
const headers =
'GET / HTTP/1.1\r\n' +
'Host: localhost\r\n' +
'Connection: keep-alive\r\n' +
'User-Agent: node\r\n\r\n';
var req_cntr = 0;
const server = http.createServer(function(req, res) {
req_cntr++;
for (var i = 0; i < FIELDS; i++) {
res.setHeader(`X-Filler${i}`, i);
}
res.end('');
});
server.listen(PORT, function() {
print(`sending ${FIELDS} fields`);
print();
print(' header fields parsed per second:');
});
(function connect() {
const client = net.connect(PORT, function() {
this.setKeepAlive(true);
this.write(headers);
});
client.on('data', function(chunk) {
if (chunk.toString('binary').substr(chunk.length - 4) === '\r\n\r\n')
this.write(headers);
});
client.on('end', function() {
print('!!!end!!!');
process.exit();
});
}());
setInterval(function() {
print(((FIELDS * req_cntr) / 3) >>> 0);
req_cntr = 0;
}, 3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment