-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.mjs
78 lines (64 loc) · 2.13 KB
/
benchmark.mjs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { join } from 'path';
import { mkdir, rm, writeFile, link } from 'fs/promises';
import { linkSync } from 'fs';
import { randomUUID } from 'crypto';
import { performance } from 'perf_hooks';
import chalk from 'chalk';
import { reflinkFileSync, reflinkFile } from './index.js';
const sandboxDir = join(process.cwd(), `__link-tests-${randomUUID()}`);
const testFilePath = join(sandboxDir, 'testFile.txt');
const results = {};
async function setup() {
await rm(sandboxDir, { recursive: true, force: true });
await mkdir(sandboxDir, { recursive: true });
await writeFile(testFilePath, 'Hello, world!');
}
async function teardown() {
await rm(sandboxDir, { recursive: true, force: true });
}
async function runBenchmark(name, fn) {
await setup();
const start = performance.now();
for (let i = 0; i < 1000; i++) {
const destPath = join(sandboxDir, `clone-${i}.txt`);
await fn(destPath);
}
const end = performance.now();
const time = end - start;
results[name] = time.toFixed(2);
console.log(chalk.green(`${name}: ${chalk.blue(time)} ms`));
await teardown();
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function main() {
console.log(chalk.bold('Running Benchmarks...'));
await runBenchmark('Node fs.linkSync', (destPath) => {
linkSync(testFilePath, destPath);
});
await delay(2000);
await runBenchmark('Node fs.promises.link', async (destPath) => {
await link(testFilePath, destPath);
});
await delay(2000);
await runBenchmark('reflinkFileSync', (destPath) => {
reflinkFileSync(testFilePath, destPath);
});
await delay(2000);
await runBenchmark('reflinkFile', async (destPath) => {
await reflinkFile(testFilePath, destPath);
});
console.log(chalk.bold('\nBenchmark Summary:'));
for (const [name, time] of Object.entries(results)) {
console.log(`${name}: ${time} ms`);
}
const fastest = Object.entries(results).sort((a, b) => a[1] - b[1])[0];
console.log(
chalk.green.bold(`\nFastest is ${fastest[0]} with ${fastest[1]} ms`)
);
}
main().catch((err) => {
console.error(chalk.red('An error occurred:', err));
process.exit(1);
});