Skip to content

Commit

Permalink
benchmark: add a few more fs benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Apr 30, 2017
1 parent b46177c commit 53a7974
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
49 changes: 49 additions & 0 deletions benchmark/fs/read-stream-throughput-dur.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

if (process.platform === 'win32')
throw new Error('This benchmark is not compatible with Windows');

const common = require('../common.js');
const fs = require('fs');

const bench = common.createBenchmark(main, {
type: ['buf', 'asc', 'utf'],
dur: [5],
size: [1024, 4096, 65535, 1024 * 1024]
});

function main(conf) {
const dur = +conf.dur * 1000;
const hwm = +conf.size;
var bytes = 0;
var encoding;

switch (conf.type) {
case 'buf':
encoding = null;
break;
case 'asc':
encoding = 'ascii';
break;
case 'utf':
encoding = 'utf8';
break;
default:
throw new Error('invalid type');
}

setTimeout(() => {
// MB/sec
bench.end(bytes / (1024 * 1024));
process.exit(0);
}, dur);

fs.createReadStream('/dev/zero', {
highWaterMark: hwm,
encoding: encoding
}).on('open', function() {
bench.start();
}).on('data', function(chunk) {
bytes += chunk.length;
});
}
75 changes: 75 additions & 0 deletions benchmark/fs/writeFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const path = require('path');
const common = require('../common.js');
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage');
const fs = require('fs');
const writeFile = fs.writeFile;

const bench = common.createBenchmark(main, {
dur: [5],
file: [''],
type: ['buf', 'asc', 'utf'],
size: [2, 1024, 64 * 1024, 1024 * 1024]
});

function main(conf) {
const dur = +conf.dur * 1000;
const size = +conf.size;
const file = (conf.file.length === 0 ? filename : conf.file);
var writes = 0;
var ended = false;
var encoding;
var data;

try { fs.unlinkSync(file); } catch (e) {}
process.on('exit', function() {
try { fs.unlinkSync(file); } catch (e) {}
});

setTimeout(function() {
bench.end(writes);
ended = true;
}, dur);

switch (conf.type) {
case 'buf':
data = Buffer.alloc(size, 'b');
(function() {
function afterWrite(err) {
if (err)
throw err;
if (ended)
return;
++writes;
writeFile(file, data, afterWrite);
}
bench.start();
writeFile(file, data, afterWrite);
})();
return;
case 'asc':
data = new Array(size + 1).join('a');
encoding = 'ascii';
break;
case 'utf':
data = new Array(Math.ceil(size / 2) + 1).join('ü');
encoding = 'utf8';
break;
default:
throw new Error('invalid type');
}

(function() {
function afterWrite(err) {
if (err)
throw err;
if (ended)
return;
++writes;
writeFile(file, data, encoding, afterWrite);
}
bench.start();
writeFile(file, data, encoding, afterWrite);
})();
}
53 changes: 53 additions & 0 deletions benchmark/fs/writeFileSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

const path = require('path');
const common = require('../common.js');
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage');
const fs = require('fs');
const writeFileSync = fs.writeFileSync;

const bench = common.createBenchmark(main, {
n: [1e5],
file: [''],
type: ['buf', 'asc', 'utf'],
size: [2, 1024, 64 * 1024, 1024 * 1024]
});

function main(conf) {
const n = +conf.n;
const size = +conf.size;
const file = (conf.file.length === 0 ? filename : conf.file);
var encoding;
var data;
var i;

try { fs.unlinkSync(file); } catch (e) {}
process.on('exit', function() {
try { fs.unlinkSync(file); } catch (e) {}
});

switch (conf.type) {
case 'buf':
data = Buffer.alloc(size, 'b');
bench.start();
for (i = 0; i < n; ++i)
writeFileSync(file, data);
bench.end(n);
return;
case 'asc':
data = new Array(size + 1).join('a');
encoding = 'ascii';
break;
case 'utf':
data = new Array(Math.ceil(size / 2) + 1).join('ü');
encoding = 'utf8';
break;
default:
throw new Error('invalid type');
}

bench.start();
for (i = 0; i < n; ++i)
writeFileSync(file, data, encoding);
bench.end(n);
}

0 comments on commit 53a7974

Please sign in to comment.