diff options
author | 2022-08-09 05:46:18 -0700 | |
---|---|---|
committer | 2022-08-09 05:48:20 -0700 | |
commit | 16f24086f8f3cae2ac16b70d67a039ea363aed1d (patch) | |
tree | d6d1dea37f5b798c471790c7e81d6fa0bc7ea5e2 /bench/copyfile/node.mitata.mjs | |
parent | f4ab46a8bd46e253b3ca5de068df9766f85d0d62 (diff) | |
download | bun-16f24086f8f3cae2ac16b70d67a039ea363aed1d.tar.gz bun-16f24086f8f3cae2ac16b70d67a039ea363aed1d.tar.zst bun-16f24086f8f3cae2ac16b70d67a039ea363aed1d.zip |
[node compat] Improve fs.copyFileSync performance on macOS
Diffstat (limited to 'bench/copyfile/node.mitata.mjs')
-rw-r--r-- | bench/copyfile/node.mitata.mjs | 51 |
1 files changed, 34 insertions, 17 deletions
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(); |