bench: report stats in benchmark/net-pipe

This commit is contained in:
Ben Noordhuis
2012-12-20 14:02:38 +01:00
parent 79ae8b7ae2
commit d1556fbdd2

View File

@@ -1,6 +1,7 @@
// test the speed of .pipe() with sockets
var net = require('net');
var N = parseInt(process.argv[2]) || 100;
var start;
function Writer() {
@@ -30,6 +31,7 @@ Writer.prototype.on = function() {};
Writer.prototype.once = function() {};
Writer.prototype.emit = function() {};
var rates = [];
var statCounter = 0;
Writer.prototype.printStats = function() {
if (!this.start || !this.received)
@@ -38,18 +40,35 @@ Writer.prototype.printStats = function() {
elapsed = elapsed[0] * 1E9 + elapsed[1];
var bits = this.received * 8;
var gbits = bits / (1024 * 1024 * 1024);
var rate = (gbits / elapsed * 1E9).toFixed(4);
console.log('%s Gbits/sec (%d bits / %d ns)', rate, bits, elapsed);
var rate = gbits / elapsed * 1E9;
rates.push(rate);
console.log('%s Gbits/sec (%d bits / %d ns)', rate.toFixed(4), bits, elapsed);
// reset to keep getting instant time.
this.start = process.hrtime();
this.received = 0;
// 100 seconds is enough time to get a few GC runs and stabilize.
if (statCounter++ === 100)
if (++statCounter === N) {
report();
process.exit(0);
}
};
function report() {
rates.sort();
var min = rates[0];
var max = rates[rates.length - 1];
var median = rates[rates.length >> 1];
var avg = 0;
rates.forEach(function(rate) { avg += rate });
avg /= rates.length;
console.error('min:%s avg:%s max:%s median:%s',
min.toFixed(2),
avg.toFixed(2),
max.toFixed(2),
median.toFixed(2));
}
var len = process.env.LENGTH || 16 * 1024 * 1024;
var chunk = new Buffer(len);
for (var i = 0; i < len; i++) {