benchmark: add more options to cp-sync · nodejs/node@a57b05e
@@ -4,20 +4,53 @@ const common = require('../common');
44const fs = require('fs');
55const path = require('path');
66const tmpdir = require('../../test/common/tmpdir');
7-tmpdir.refresh();
8798const bench = common.createBenchmark(main, {
109n: [1, 100, 10_000],
10+dereference: ['true', 'false'],
11+force: ['true', 'false'],
1112});
121313-function main({ n }) {
14+function prepareTestDirectory() {
15+const testDir = tmpdir.resolve(`test-cp-${process.pid}`);
16+fs.mkdirSync(testDir, { recursive: true });
17+18+fs.writeFileSync(path.join(testDir, 'source.txt'), 'test content');
19+20+fs.symlinkSync(
21+path.join(testDir, 'source.txt'),
22+path.join(testDir, 'link.txt'),
23+);
24+25+return testDir;
26+}
27+28+function main({ n, dereference, force }) {
1429tmpdir.refresh();
15- const options = { recursive: true };
16-const src = path.join(__dirname, '../../test/fixtures/copy');
30+31+const src = prepareTestDirectory();
1732const dest = tmpdir.resolve(`${process.pid}/subdir/cp-bench-${process.pid}`);
33+34+const options = {
35+recursive: true,
36+dereference: dereference === 'true',
37+force: force === 'true',
38+};
39+40+if (options.force) {
41+fs.cpSync(src, dest, { recursive: true });
42+}
43+1844bench.start();
1945for (let i = 0; i < n; i++) {
20-fs.cpSync(src, dest, options);
46+if (options.force) {
47+fs.cpSync(src, dest, options);
48+} else {
49+const uniqueDest = tmpdir.resolve(
50+`${process.pid}/subdir/cp-bench-${process.pid}-${i}`,
51+);
52+fs.cpSync(src, uniqueDest, options);
53+}
2154}
2255bench.end(n);
2356}