Skip to content

benchmark: add dereference and force options to fs.cpSync #58278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions benchmark/fs/bench-cpSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,53 @@ const common = require('../common');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../../test/common/tmpdir');
tmpdir.refresh();

const bench = common.createBenchmark(main, {
n: [1, 100, 10_000],
dereference: ['true', 'false'],
force: ['true', 'false'],
});

function main({ n }) {
function prepareTestDirectory() {
const testDir = tmpdir.resolve(`test-cp-${process.pid}`);
fs.mkdirSync(testDir, { recursive: true });

fs.writeFileSync(path.join(testDir, 'source.txt'), 'test content');

fs.symlinkSync(
path.join(testDir, 'source.txt'),
path.join(testDir, 'link.txt'),
);

return testDir;
}

function main({ n, dereference, force }) {
tmpdir.refresh();
const options = { recursive: true };
const src = path.join(__dirname, '../../test/fixtures/copy');

const src = prepareTestDirectory();
const dest = tmpdir.resolve(`${process.pid}/subdir/cp-bench-${process.pid}`);

const options = {
recursive: true,
dereference: dereference === 'true',
force: force === 'true',
};

if (options.force) {
fs.cpSync(src, dest, { recursive: true });
}

bench.start();
for (let i = 0; i < n; i++) {
fs.cpSync(src, dest, options);
if (options.force) {
fs.cpSync(src, dest, options);
} else {
const uniqueDest = tmpdir.resolve(
`${process.pid}/subdir/cp-bench-${process.pid}-${i}`,
);
fs.cpSync(src, uniqueDest, options);
}
}
bench.end(n);
}
Loading