Skip to content

Commit a7ef140

Browse files
author
pluris
committed
add benchmark for fdatasyncSync
1 parent 78b27c0 commit a7ef140

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fs = require('fs');
5+
const tmpdir = require('../../test/common/tmpdir');
6+
tmpdir.refresh();
7+
8+
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
9+
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
10+
11+
const bench = common.createBenchmark(main, {
12+
type: ['existing', 'non-existing'],
13+
n: [1e4],
14+
});
15+
16+
function main({ n, type }) {
17+
let fd;
18+
19+
switch (type) {
20+
case 'existing':
21+
fd = fs.openSync(tmpfile, 'r', 0o666);
22+
break;
23+
case 'non-existing':
24+
fd = 1 << 30;
25+
break;
26+
default:
27+
new Error('Invalid type');
28+
}
29+
30+
bench.start();
31+
for (let i = 0; i < n; i++) {
32+
try {
33+
fs.fdatasyncSync(fd);
34+
} catch {
35+
// do nothing
36+
}
37+
}
38+
39+
bench.end(n);
40+
41+
if (type === 'existing') fs.closeSync(fd);
42+
}

0 commit comments

Comments
 (0)