From 16f24086f8f3cae2ac16b70d67a039ea363aed1d Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Tue, 9 Aug 2022 05:46:18 -0700 Subject: [node compat] Improve fs.copyFileSync performance on macOS --- bench/copyfile/node.mitata.mjs | 51 ++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'bench/copyfile/node.mitata.mjs') diff --git a/bench/copyfile/node.mitata.mjs b/bench/copyfile/node.mitata.mjs index 93833cfcf..aa77245e1 100644 --- a/bench/copyfile/node.mitata.mjs +++ b/bench/copyfile/node.mitata.mjs @@ -1,23 +1,40 @@ -import { copyFileSync, writeFileSync } from "node:fs"; +import { copyFileSync, writeFileSync, readFileSync, statSync } from "node:fs"; import { bench, run } from "mitata"; -const size = parseInt(process.env.FILE_SIZE, 10) || 1024 * 16; -const rand = new Float64Array(size); -for (let i = 0; i < size; i++) { - rand[i] = Math.random(); +function runner(ready) { + for (let size of [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]) { + const rand = new Int32Array(size); + for (let i = 0; i < size; i++) { + rand[i] = (Math.random() * 1024 * 1024) | 0; + } + const dest = `/tmp/fs-test-copy-file-${( + (Math.random() * 10000000 + 100) | + 0 + ).toString(32)}`; + const src = `/tmp/fs-test-copy-file-${( + (Math.random() * 10000000 + 100) | + 0 + ).toString(32)}`; + writeFileSync(src, Buffer.from(rand.buffer), { encoding: "buffer" }); + const { size: fileSize } = statSync(src); + if (fileSize !== rand.byteLength) { + throw new Error("size mismatch"); + } + ready(src, dest, new Uint8Array(rand.buffer)); + } } -const dest = `/tmp/fs-test-copy-file-${(Math.random() * 100000 + 100).toString( - 32 -)}`; -const src = `/tmp/fs-test-copy-file-${(Math.random() * 100000 + 100).toString( - 32 -)}`; -writeFileSync(src, new Buffer(rand.buffer)); +runner((src, dest, rand) => + bench(`copyFileSync(${rand.buffer.byteLength} bytes)`, () => { + copyFileSync(src, dest); + // const output = readFileSync(dest).buffer; -const srcBuf = new TextEncoder().encode(src); -const destBuf = new TextEncoder().encode(dest); -bench(`copyFileSync(${rand.buffer.byteLength} bytes)`, () => - copyFileSync(srcBuf, destBuf) + // for (let i = 0; i < output.length; i++) { + // if (output[i] !== rand[i]) { + // throw new Error( + // "Files are not equal" + " " + output[i] + " " + rand[i] + " " + i + // ); + // } + // } + }) ); - await run(); -- cgit v1.2.3