From d1556fbdd298b58774f8560ac990cebb3e2875a6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 20 Dec 2012 14:02:38 +0100 Subject: [PATCH] bench: report stats in benchmark/net-pipe --- benchmark/net-pipe.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/benchmark/net-pipe.js b/benchmark/net-pipe.js index 0890e292eb..e981cc3b4e 100644 --- a/benchmark/net-pipe.js +++ b/benchmark/net-pipe.js @@ -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++) {