-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: fs optimizations - part 1 (#15873)
- Loading branch information
1 parent
11ced3c
commit 698a340
Showing
7 changed files
with
595 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 />), | ||
})); |
Oops, something went wrong.