forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbench-chownSync.js
More file actions
54 lines (49 loc) · 1.29 KB
/
bench-chownSync.js
File metadata and controls
54 lines (49 loc) · 1.29 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
'use strict';
const common = require('../common');
const fs = require('fs');
const assert = require('assert');
const tmpdir = require('../../test/common/tmpdir');
if (process.platform === 'win32') {
console.log('Skipping: Windows does not have `getuid` or `getgid`');
process.exit(0);
}
const bench = common.createBenchmark(main, {
type: ['existing', 'non-existing'],
method: ['chownSync', 'lchownSync'],
n: [1e4],
});
function main({ n, type, method }) {
const uid = process.getuid();
const gid = process.getgid();
const fsMethod = fs[method];
switch (type) {
case 'existing': {
tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.existing-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'this-is-for-a-benchmark', 'utf8');
bench.start();
for (let i = 0; i < n; i++) {
fsMethod(tmpfile, uid, gid);
}
bench.end(n);
break;
}
case 'non-existing': {
const path = tmpdir.resolve(`.non-existing-file-${Date.now()}`);
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs[method](path, uid, gid);
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}