Skip to content

Instantly share code, notes, and snippets.

@LaurentVB
Created December 5, 2014 10:54
Show Gist options
  • Save LaurentVB/e700411d5f073b4c177d to your computer and use it in GitHub Desktop.
Save LaurentVB/e700411d5f073b4c177d to your computer and use it in GitHub Desktop.
A naive port of joyent/http-parser bench.c to javascript, in order to test the performance of creationix/http-parser-js
var HTTPParser = require('./http-parser.js').HTTPParser;
var data = "GET /joyent/http-parser HTTP/1.1\r\n" +
"Host: github.com\r\n" +
"DNT: 1\r\n" +
"Accept-Encoding: gzip, deflate, sdch\r\n" +
"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n" +
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/39.0.2171.65 Safari/537.36\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9," +
"image/webp,*/*;q=0.8\r\n" +
"Referer: https://github.com/joyent/http-parser\r\n" +
"Connection: keep-alive\r\n" +
"Cache-Control: max-age=0\r\n\r\n";
var data_len = Buffer.byteLength(data);
function bench(iter_count, silent) {
var parser;
var i;
var err;
var time = process.hrtime();
var rps;
var parsed;
for (i = 0; i < iter_count; i++) {
parser = new HTTPParser('REQUEST');
parser.execute(data, 0, data_len);
}
if (!silent) {
console.log("Benchmark result:");
time = process.hrtime(time);
rps = time[0] + (time[1] * 1e-9);
console.log("Took %d seconds to run", rps);
rps = iter_count / rps;
console.log("%d req/sec", rps);
}
return 0;
}
bench(5000000, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment