Skip to content

Commit 53a7974

Browse files
committed
benchmark: add a few more fs benchmarks
1 parent b46177c commit 53a7974

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
if (process.platform === 'win32')
4+
throw new Error('This benchmark is not compatible with Windows');
5+
6+
const common = require('../common.js');
7+
const fs = require('fs');
8+
9+
const bench = common.createBenchmark(main, {
10+
type: ['buf', 'asc', 'utf'],
11+
dur: [5],
12+
size: [1024, 4096, 65535, 1024 * 1024]
13+
});
14+
15+
function main(conf) {
16+
const dur = +conf.dur * 1000;
17+
const hwm = +conf.size;
18+
var bytes = 0;
19+
var encoding;
20+
21+
switch (conf.type) {
22+
case 'buf':
23+
encoding = null;
24+
break;
25+
case 'asc':
26+
encoding = 'ascii';
27+
break;
28+
case 'utf':
29+
encoding = 'utf8';
30+
break;
31+
default:
32+
throw new Error('invalid type');
33+
}
34+
35+
setTimeout(() => {
36+
// MB/sec
37+
bench.end(bytes / (1024 * 1024));
38+
process.exit(0);
39+
}, dur);
40+
41+
fs.createReadStream('/dev/zero', {
42+
highWaterMark: hwm,
43+
encoding: encoding
44+
}).on('open', function() {
45+
bench.start();
46+
}).on('data', function(chunk) {
47+
bytes += chunk.length;
48+
});
49+
}

benchmark/fs/writeFile.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const common = require('../common.js');
5+
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage');
6+
const fs = require('fs');
7+
const writeFile = fs.writeFile;
8+
9+
const bench = common.createBenchmark(main, {
10+
dur: [5],
11+
file: [''],
12+
type: ['buf', 'asc', 'utf'],
13+
size: [2, 1024, 64 * 1024, 1024 * 1024]
14+
});
15+
16+
function main(conf) {
17+
const dur = +conf.dur * 1000;
18+
const size = +conf.size;
19+
const file = (conf.file.length === 0 ? filename : conf.file);
20+
var writes = 0;
21+
var ended = false;
22+
var encoding;
23+
var data;
24+
25+
try { fs.unlinkSync(file); } catch (e) {}
26+
process.on('exit', function() {
27+
try { fs.unlinkSync(file); } catch (e) {}
28+
});
29+
30+
setTimeout(function() {
31+
bench.end(writes);
32+
ended = true;
33+
}, dur);
34+
35+
switch (conf.type) {
36+
case 'buf':
37+
data = Buffer.alloc(size, 'b');
38+
(function() {
39+
function afterWrite(err) {
40+
if (err)
41+
throw err;
42+
if (ended)
43+
return;
44+
++writes;
45+
writeFile(file, data, afterWrite);
46+
}
47+
bench.start();
48+
writeFile(file, data, afterWrite);
49+
})();
50+
return;
51+
case 'asc':
52+
data = new Array(size + 1).join('a');
53+
encoding = 'ascii';
54+
break;
55+
case 'utf':
56+
data = new Array(Math.ceil(size / 2) + 1).join('ü');
57+
encoding = 'utf8';
58+
break;
59+
default:
60+
throw new Error('invalid type');
61+
}
62+
63+
(function() {
64+
function afterWrite(err) {
65+
if (err)
66+
throw err;
67+
if (ended)
68+
return;
69+
++writes;
70+
writeFile(file, data, encoding, afterWrite);
71+
}
72+
bench.start();
73+
writeFile(file, data, encoding, afterWrite);
74+
})();
75+
}

benchmark/fs/writeFileSync.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const common = require('../common.js');
5+
const filename = path.resolve(__dirname, '.removeme-benchmark-garbage');
6+
const fs = require('fs');
7+
const writeFileSync = fs.writeFileSync;
8+
9+
const bench = common.createBenchmark(main, {
10+
n: [1e5],
11+
file: [''],
12+
type: ['buf', 'asc', 'utf'],
13+
size: [2, 1024, 64 * 1024, 1024 * 1024]
14+
});
15+
16+
function main(conf) {
17+
const n = +conf.n;
18+
const size = +conf.size;
19+
const file = (conf.file.length === 0 ? filename : conf.file);
20+
var encoding;
21+
var data;
22+
var i;
23+
24+
try { fs.unlinkSync(file); } catch (e) {}
25+
process.on('exit', function() {
26+
try { fs.unlinkSync(file); } catch (e) {}
27+
});
28+
29+
switch (conf.type) {
30+
case 'buf':
31+
data = Buffer.alloc(size, 'b');
32+
bench.start();
33+
for (i = 0; i < n; ++i)
34+
writeFileSync(file, data);
35+
bench.end(n);
36+
return;
37+
case 'asc':
38+
data = new Array(size + 1).join('a');
39+
encoding = 'ascii';
40+
break;
41+
case 'utf':
42+
data = new Array(Math.ceil(size / 2) + 1).join('ü');
43+
encoding = 'utf8';
44+
break;
45+
default:
46+
throw new Error('invalid type');
47+
}
48+
49+
bench.start();
50+
for (i = 0; i < n; ++i)
51+
writeFileSync(file, data, encoding);
52+
bench.end(n);
53+
}

0 commit comments

Comments
 (0)