forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bench suite of common Deno functions (denoland#9878)
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 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,53 @@ | ||
// Run with: deno run -A ./cli/bench/deno_common.js | ||
function benchSync(name, n, innerLoop) { | ||
const t1 = Date.now(); | ||
for (let i = 0; i < n; i++) { | ||
innerLoop(i); | ||
} | ||
const t2 = Date.now(); | ||
const dt = (t2 - t1) / 1e3; | ||
const r = n / dt; | ||
console.log( | ||
`${name}:${" ".repeat(20 - name.length)}\t` + | ||
`n = ${n}, dt = ${dt.toFixed(3)}s, r = ${r.toFixed(0)}/s`, | ||
); | ||
} | ||
|
||
function benchUrlParse() { | ||
benchSync("url_parse", 5e4, (i) => { | ||
new URL(`https://www.google.com/${i}`); | ||
}); | ||
} | ||
|
||
function benchNow() { | ||
benchSync("now", 5e5, () => { | ||
performance.now(); | ||
}); | ||
} | ||
|
||
function benchWriteNull() { | ||
// Not too large since we want to measure op-overhead not sys IO | ||
const dataChunk = new Uint8Array(100); | ||
const file = Deno.openSync("/dev/null", { write: true }); | ||
benchSync("write_null", 5e5, () => { | ||
Deno.writeSync(file.rid, dataChunk); | ||
}); | ||
Deno.close(file.rid); | ||
} | ||
|
||
function benchReadZero() { | ||
const buf = new Uint8Array(100); | ||
const file = Deno.openSync("/dev/zero"); | ||
benchSync("read_zero", 5e5, () => { | ||
Deno.readSync(file.rid, buf); | ||
}); | ||
Deno.close(file.rid); | ||
} | ||
|
||
function main() { | ||
benchUrlParse(); | ||
benchNow(); | ||
benchWriteNull(); | ||
benchReadZero(); | ||
} | ||
main(); |