forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench-timesSync.js
More file actions
61 lines (52 loc) · 1.32 KB
/
bench-timesSync.js
File metadata and controls
61 lines (52 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const common = require('../common');
const fs = require('fs');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();
const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
func: [ 'utimes', 'futimes', 'lutimes' ],
n: [1e3],
});
function main({ n, type, func }) {
const useFds = func === 'futimes';
const fsFunc = fs[func + 'Sync'];
switch (type) {
case 'existing': {
const files = [];
// Populate tmpdir with mock files
for (let i = 0; i < n; i++) {
const path = tmpdir.resolve(`timessync-bench-file-${i}`);
fs.writeFileSync(path, 'bench');
files.push(useFds ? fs.openSync(path, 'r+') : path);
}
bench.start();
for (let i = 0; i < n; i++) {
fsFunc(files[i], i, i);
}
bench.end(n);
if (useFds) files.forEach((x) => {
try {
fs.closeSync(x);
} catch {
// do nothing
}
});
break;
}
case 'non-existing': {
bench.start();
for (let i = 0; i < n; i++) {
try {
fsFunc(useFds ? (1 << 30) : tmpdir.resolve(`.non-existing-file-${Date.now()}`), i, i);
} catch {
// do nothing
}
}
bench.end(n);
break;
}
default:
new Error('Invalid type');
}
}