Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created February 5, 2013 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/4717966 to your computer and use it in GitHub Desktop.
Save isaacs/4717966 to your computer and use it in GitHub Desktop.
// If there are no args, then this is the root. Run all the benchmarks!
if (!process.argv[2]) {
parent();
} else {
runTest(+process.argv[2], +process.argv[3], process.argv[4]);
}
function parent() {
var types = [ 'string', 'buffer' ];
var durs = [ 1, 5 ];
var sizes = [ 1, 10, 100, 2048 ];
var queue = [];
types.forEach(function(t) {
durs.forEach(function(d) {
sizes.forEach(function(s) {
queue.push([__filename, d, s, t]);
});
});
});
var spawn = require('child_process').spawn;
var node = process.execPath;
run();
function run() {
var args = queue.shift();
if (!args)
return;
var child = spawn(node, args, { stdio: 'inherit' });
child.on('close', function(code, signal) {
if (code)
throw new Error('Benchmark failed: ' + args.slice(1));
run();
});
}
}
function runTest(dur, size, type) {
if (type !== 'string')
type = 'buffer';
switch (type) {
case 'string':
var chunk = new Array(size + 1).join('a');
break;
case 'buffer':
var chunk = new Buffer(size);
chunk.fill('a');
break;
}
var writes = 0;
var fs = require('fs');
try { fs.unlinkSync('write_stream_throughput'); } catch (e) {}
var start
var end;
function done() {
var time = end[0] + end[1]/1E9;
var written = fs.statSync('write_stream_throughput').size / 1024;
var rate = (written / time).toFixed(2);
var version = process.version.match(/v[0-9]+\.[0-9]+/)[0];
console.log('fs_write_stream_dur_%d_size_%d_type_%s_%s: %d',
dur, size, type, version, rate);
try { fs.unlinkSync('write_stream_throughput'); } catch (e) {}
}
var f = require('fs').createWriteStream('write_stream_throughput');
f.on('drain', write);
f.on('open', write);
f.on('close', done);
// streams2 fs.WriteStreams will let you send a lot of writes into the
// buffer before returning false, so capture the *actual* end time when
// all the bytes have been written to the disk, indicated by 'finish'
f.on('finish', function() {
end = process.hrtime(start);
});
var ending = false;
function write() {
// don't try to write after we end, even if a 'drain' event comes.
// v0.8 streams are so sloppy!
if (ending)
return;
start = start || process.hrtime();
while (false !== f.write(chunk));
end = process.hrtime(start);
if (end[0] >= dur) {
ending = true;
f.end();
}
}
}
@isaacs
Copy link
Author

isaacs commented Feb 5, 2013

Output:

fs_write_stream_dur_1_size_1_type_string_v0.9: 42.49
fs_write_stream_dur_1_size_10_type_string_v0.9: 540.48
fs_write_stream_dur_1_size_100_type_string_v0.9: 5471.2
fs_write_stream_dur_1_size_2048_type_string_v0.9: 91129.2
fs_write_stream_dur_5_size_1_type_string_v0.9: 43.6
fs_write_stream_dur_5_size_10_type_string_v0.9: 568.57
fs_write_stream_dur_5_size_100_type_string_v0.9: 5636.19
fs_write_stream_dur_5_size_2048_type_string_v0.9: 81421.16
fs_write_stream_dur_1_size_1_type_buffer_v0.9: 42.8
fs_write_stream_dur_1_size_10_type_buffer_v0.9: 526.47
fs_write_stream_dur_1_size_100_type_buffer_v0.9: 5262.15
fs_write_stream_dur_1_size_2048_type_buffer_v0.9: 92393.02
fs_write_stream_dur_5_size_1_type_buffer_v0.9: 43.31
fs_write_stream_dur_5_size_10_type_buffer_v0.9: 566.25
fs_write_stream_dur_5_size_100_type_buffer_v0.9: 5917.18
fs_write_stream_dur_5_size_2048_type_buffer_v0.9: 96324.03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment