How can I benchmark streams in Node.js?
I've tried benchmark.js:
var fs = require('fs');
var Transform = require('readable-stream').Transform;
var util = require('util');
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
// my super uppercase stream
function Uppercase(options) {
if (!(this instanceof Uppercase))
return new Uppercase(options);
Transform.call(this, options);
}
Uppercase.prototype = Object.create(
Transform.prototype, { constructor: { value: Uppercase }});
Uppercase.prototype._transform = function(chunk, encoding, done) {
chunk = chunk.toString().toUpperCase();
this.push(chunk)
};
// start benchmarking
suite.add('stream test', function() {
var reader = fs.createReadStream('in.txt');
var parser = new Uppercase();
var writer = fs.createWriteStream('out.txt');
reader.pipe(parser).pipe(writer);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
// run async
.run();
suite.run();
But I get the error
Unhandled stream error in pipe
Your code encounter this error, you could see it by listen to error on reader and writer :
This is caused by the fact that streams are asynchronous and without explicit callback on when they end. So you are pretty much creating thousand and thousands of pipe stream between in.txt and out.txt untill the system tells you they are too many files descriptor open.
So, I'll guess that by "benchmarking streams in Node", what you want to want to calculate is the time it takes to do synchronously perform this operation :
In that case you will need to :
The code for this answer has been tested with node 0.10.0, but I guess that the only difference should be in the name of the module which holds
Transform
: