Skip to content

Commit

Permalink
perf: fs optimizations - part 1 (#15873)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Sep 22, 2022
1 parent 11ced3c commit 698a340
Show file tree
Hide file tree
Showing 7 changed files with 595 additions and 320 deletions.
1 change: 1 addition & 0 deletions cli/bench/fs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
26 changes: 26 additions & 0 deletions cli/bench/fs/README.md
Original file line number Diff line number Diff line change
@@ -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/
```
66 changes: 66 additions & 0 deletions cli/bench/fs/run.mjs
Original file line number Diff line number Diff line change
@@ -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 });
57 changes: 57 additions & 0 deletions cli/bench/fs/serve.jsx
Original file line number Diff line number Diff line change
@@ -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/[email protected]";
import { router } from "https://crux.land/[email protected]";

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 />),
}));
Loading

0 comments on commit 698a340

Please sign in to comment.