Skip to content

Commit 3572299

Browse files
LinkgoronMylesBorins
authored andcommitted
fs: add promisified readFile benchmark
add a benchmark for fs.promises.readFile PR-URL: #37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent b277776 commit 3572299

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

benchmark/fs/readfile-promises.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Call fs.promises.readFile over and over again really fast.
2+
// Then see how many times it got called.
3+
// Yes, this is a silly benchmark. Most benchmarks are silly.
4+
'use strict';
5+
6+
const path = require('path');
7+
const common = require('../common.js');
8+
const fs = require('fs');
9+
const assert = require('assert');
10+
11+
const tmpdir = require('../../test/common/tmpdir');
12+
tmpdir.refresh();
13+
const filename = path.resolve(tmpdir.path,
14+
`.removeme-benchmark-garbage-${process.pid}`);
15+
16+
const bench = common.createBenchmark(main, {
17+
duration: [5],
18+
len: [1024, 16 * 1024 * 1024],
19+
concurrent: [1, 10]
20+
});
21+
22+
function main({ len, duration, concurrent }) {
23+
try { fs.unlinkSync(filename); } catch { }
24+
let data = Buffer.alloc(len, 'x');
25+
fs.writeFileSync(filename, data);
26+
data = null;
27+
28+
let writes = 0;
29+
let benchEnded = false;
30+
bench.start();
31+
setTimeout(() => {
32+
benchEnded = true;
33+
bench.end(writes);
34+
try { fs.unlinkSync(filename); } catch { }
35+
process.exit(0);
36+
}, duration * 1000);
37+
38+
function read() {
39+
fs.promises.readFile(filename)
40+
.then((res) => afterRead(undefined, res))
41+
.catch((err) => afterRead(err));
42+
}
43+
44+
function afterRead(er, data) {
45+
if (er) {
46+
if (er.code === 'ENOENT') {
47+
// Only OK if unlinked by the timer from main.
48+
assert.ok(benchEnded);
49+
return;
50+
}
51+
throw er;
52+
}
53+
54+
if (data.length !== len)
55+
throw new Error('wrong number of bytes returned');
56+
57+
writes++;
58+
if (!benchEnded)
59+
read();
60+
}
61+
62+
while (concurrent--) read();
63+
}

0 commit comments

Comments
 (0)