forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench-readlinkSync.js
More file actions
54 lines (49 loc) · 1.33 KB
/
bench-readlinkSync.js
File metadata and controls
54 lines (49 loc) · 1.33 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 play well with `symlinkSync`');
process.exit(0);
}
const bench = common.createBenchmark(main, {
type: ['valid', 'invalid'],
n: [1e3],
});
function main({ n, type }) {
switch (type) {
case 'valid': {
tmpdir.refresh();
const tmpfile = tmpdir.resolve(`.readlink-file-${process.pid}`);
fs.writeFileSync(tmpfile, 'data', 'utf8');
let returnValue;
for (let i = 0; i < n; i++) {
fs.symlinkSync(tmpfile, tmpdir.resolve(`.readlink-sync-${i}`), 'file');
}
bench.start();
for (let i = 0; i < n; i++) {
returnValue = fs.readlinkSync(tmpdir.resolve(`.readlink-sync-${i}`), { encoding: 'utf8' });
}
bench.end(n);
assert(returnValue);
break;
}
case 'invalid': {
let hasError = false;
bench.start();
for (let i = 0; i < n; i++) {
try {
fs.readlinkSync(tmpdir.resolve('.non-existing-file-for-readlinkSync'));
} catch {
hasError = true;
}
}
bench.end(n);
assert(hasError);
break;
}
default:
new Error('Invalid type');
}
}