From 698a340ad7cbbea5d6782c4f410fb50a6d09dc4d Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 22 Sep 2022 14:39:25 +0530 Subject: perf: fs optimizations - part 1 (#15873) --- cli/bench/fs/.gitignore | 1 + cli/bench/fs/README.md | 26 +++++++++++++++++++ cli/bench/fs/run.mjs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ cli/bench/fs/serve.jsx | 57 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100644 cli/bench/fs/.gitignore create mode 100644 cli/bench/fs/README.md create mode 100644 cli/bench/fs/run.mjs create mode 100644 cli/bench/fs/serve.jsx (limited to 'cli') diff --git a/cli/bench/fs/.gitignore b/cli/bench/fs/.gitignore new file mode 100644 index 000000000..94a2dd146 --- /dev/null +++ b/cli/bench/fs/.gitignore @@ -0,0 +1 @@ +*.json \ No newline at end of file diff --git a/cli/bench/fs/README.md b/cli/bench/fs/README.md new file mode 100644 index 000000000..122a70cfd --- /dev/null +++ b/cli/bench/fs/README.md @@ -0,0 +1,26 @@ +## `fs` benchmarks + +### adding new benchmarks + +```js +const copyFileSync = getFunction("copyFileSync"); +bench(() => copyFileSync("test", "test2")); + +// For functions with side-effects, clean up after `bench` like so: +const removeSync = getFunction("removeSync"); +removeSync("test2"); +``` + +### running + +```bash +deno run -A --unstable run.mjs +node run.js +``` + +### view report + +```bash +deno run --allow-net=127.0.0.1:9000 serve.jsx +# View rendered report at http://127.0.0.1:9000/ +``` diff --git a/cli/bench/fs/run.mjs b/cli/bench/fs/run.mjs new file mode 100644 index 000000000..92471d638 --- /dev/null +++ b/cli/bench/fs/run.mjs @@ -0,0 +1,66 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +let total = 5; +let current = ""; +const values = {}; +const runtime = typeof Deno !== "undefined" ? "deno" : "node"; + +function bench(fun, count = 100000) { + if (total === 5) console.log(fun.toString()); + const start = Date.now(); + for (let i = 0; i < count; i++) fun(); + const elapsed = Date.now() - start; + const rate = Math.floor(count / (elapsed / 1000)); + console.log(`time ${elapsed} ms rate ${rate}`); + values[current] = values[current] || []; + values[current].push(rate); + if (--total) bench(fun, count); + else total = 5; +} + +let fs; +if (runtime === "node") { + fs = await import("fs"); +} + +const getFunction = runtime === "deno" + ? (name) => { + current = name; + return Deno[name]; + } + : (name) => { + current = name; + return fs[name]; + }; + +const writeFileSync = getFunction("writeFileSync"); +writeFileSync("test", new Uint8Array(1024 * 1024), { truncate: true }); + +const copyFileSync = getFunction("copyFileSync"); +bench(() => copyFileSync("test", "test2"), 10000); + +const truncateSync = getFunction("truncateSync"); +bench(() => truncateSync("test", 0)); + +const lstatSync = getFunction("lstatSync"); +bench(() => lstatSync("test")); + +const { uid, gid } = lstatSync("test"); + +const chownSync = getFunction("chownSync"); +bench(() => chownSync("test", uid, gid)); + +const chmodSync = getFunction("chmodSync"); +bench(() => chmodSync("test", 0o666)); + +// const cwd = getFunction("cwd"); +// bench(() => cwd()); + +// const chdir = getFunction("chdir"); +// bench(() => chdir("/")); + +const readFileSync = getFunction("readFileSync"); +writeFileSync("test", new Uint8Array(1024), { truncate: true }); +bench(() => readFileSync("test")); + +writeFileSync(new URL(`./${runtime}.json`, import.meta.url), new TextEncoder().encode(JSON.stringify(values, null, 2)), { truncate: true }); \ No newline at end of file diff --git a/cli/bench/fs/serve.jsx b/cli/bench/fs/serve.jsx new file mode 100644 index 000000000..3ef07c6a0 --- /dev/null +++ b/cli/bench/fs/serve.jsx @@ -0,0 +1,57 @@ +// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. + +/** @jsx h */ +import results from "./deno.json" assert { type: "json" }; +import nodeResults from "./node.json" assert { type: "json" }; +import { h, ssr } from "https://crux.land/nanossr@0.0.4"; +import { router } from "https://crux.land/router@0.0.11"; + +function once(fn) { + let called = false; + let result; + return function () { + if (!called) { + called = true; + result = fn(); + return result; + } + return result; + }; +} + +const body = once(() => + Object.entries(results).map(([name, data]) => ( + + {name} + + {data.reduce((a, b) => a + b, 0) / data.length} ops/sec + + + {nodeResults[name].reduce((a, b) => a + b, 0) / + nodeResults[name].length} ops/sec + + + )) +); + +function App() { + return ( + + + + + + + + + + {body()} + +
BenchmarkDenoNode
+ ); +} + +const { serve } = Deno; +serve(router({ + "/": () => ssr(() => ), +})); -- cgit v1.2.3