summaryrefslogtreecommitdiff
path: root/cli/bench/fs
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-09-22 14:39:25 +0530
committerGitHub <noreply@github.com>2022-09-22 14:39:25 +0530
commit698a340ad7cbbea5d6782c4f410fb50a6d09dc4d (patch)
tree316164ab9ef00f6fda6f0ac642ddde0d4bc52981 /cli/bench/fs
parent11ced3c10e5e9f896b3e06370e390635ab4f12c8 (diff)
perf: fs optimizations - part 1 (#15873)
Diffstat (limited to 'cli/bench/fs')
-rw-r--r--cli/bench/fs/.gitignore1
-rw-r--r--cli/bench/fs/README.md26
-rw-r--r--cli/bench/fs/run.mjs66
-rw-r--r--cli/bench/fs/serve.jsx57
4 files changed, 150 insertions, 0 deletions
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]) => (
+ <tr>
+ <td class="border px-4 py-2">{name}</td>
+ <td class="border px-4 py-2">
+ {data.reduce((a, b) => a + b, 0) / data.length} ops/sec
+ </td>
+ <td class="border px-4 py-2">
+ {nodeResults[name].reduce((a, b) => a + b, 0) /
+ nodeResults[name].length} ops/sec
+ </td>
+ </tr>
+ ))
+);
+
+function App() {
+ return (
+ <table class="table-auto">
+ <thead>
+ <tr>
+ <th class="px-4 py-2">Benchmark</th>
+ <th class="px-4 py-2">Deno</th>
+ <th class="px-4 py-2">Node</th>
+ </tr>
+ </thead>
+ <tbody>
+ {body()}
+ </tbody>
+ </table>
+ );
+}
+
+const { serve } = Deno;
+serve(router({
+ "/": () => ssr(() => <App />),
+}));