1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

chore(cli/bench): use deno bench for deno_common.js (#15063)

This commit is contained in:
Divy Srivastava 2022-07-04 16:53:02 +05:30 committed by GitHub
parent 8941a39fe5
commit 4ee7216858
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,142 +1,68 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// 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();
console.log(benchStats(name, n, t1, t2));
}
async function benchAsync(name, n, innerLoop) { // v8 builtin that's close to the upper bound non-NOPs
const t1 = Date.now(); Deno.bench("date_now", { n: 5e5 }, () => {
for (let i = 0; i < n; i++) { Date.now();
await innerLoop(i); });
}
const t2 = Date.now();
console.log(benchStats(name, n, t1, t2));
}
// Parallel version benchAsync // Void ops measure op-overhead
async function benchAsyncP(name, n, p, innerLoop) { Deno.bench("op_void_sync", { n: 1e7 }, () => Deno.core.opSync("op_void_sync"));
const range = new Array(p).fill(null);
const t1 = Date.now();
for (let i = 0; i < n / p; i++) {
await Promise.all(range.map(() => innerLoop()));
}
const t2 = Date.now();
console.log(benchStats(name, n, t1, t2));
}
function benchStats(name, n, t1, t2) { Deno.bench(
const dt = (t2 - t1) / 1e3; "op_void_async",
const r = n / dt; { n: 1e6 },
const ns = Math.floor(dt / n * 1e9); () => Deno.core.opAsync("op_void_async"),
return `${name}:${" ".repeat(20 - name.length)}\t` + );
`n = ${n}, dt = ${dt.toFixed(3)}s, r = ${r.toFixed(0)}/s, t = ${ns}ns/op`;
}
function benchB64RtLong() { // A very lightweight op, that should be highly optimizable
const input = "long-string".repeat(99999); Deno.bench("perf_now", { n: 5e5 }, () => {
benchSync("b64_rt_long", 100, () => { performance.now();
atob(btoa(input)); });
});
}
function benchB64RtShort() { // A common "language feature", that should be fast
benchSync("b64_rt_short", 1e6, () => { // also a decent representation of a non-trivial JSON-op
atob(btoa("123")); {
}); let i = 0;
} Deno.bench("url_parse", { n: 5e4 }, () => {
function benchUrlParse() {
benchSync("url_parse", 5e4, (i) => {
new URL(`http://www.google.com/${i}`); new URL(`http://www.google.com/${i}`);
i++;
}); });
} }
function benchLargeBlobText() { Deno.bench("blob_text_large", { n: 100 }, () => {
const input = "long-string".repeat(999_999); new Blob([input]).text();
benchSync("blob_text_large", 100, () => { });
new Blob([input]).text();
const input = "long-string".repeat(99999);
Deno.bench("b64_rt_long", { n: 100 }, () => {
atob(btoa(input));
});
Deno.bench("b64_rt_short", { n: 1e6 }, () => {
atob(btoa("123"));
});
{
const buf = new Uint8Array(100);
const file = Deno.openSync("/dev/zero");
Deno.bench("read_zero", { n: 5e5 }, () => {
Deno.readSync(file.rid, buf);
}); });
} }
function benchDateNow() { {
benchSync("date_now", 5e5, () => {
Date.now();
});
}
function benchPerfNow() {
benchSync("perf_now", 5e5, () => {
performance.now();
});
}
function benchWriteNull() {
// Not too large since we want to measure op-overhead not sys IO // Not too large since we want to measure op-overhead not sys IO
const dataChunk = new Uint8Array(100); const dataChunk = new Uint8Array(100);
const file = Deno.openSync("/dev/null", { write: true }); const file = Deno.openSync("/dev/null", { write: true });
benchSync("write_null", 5e5, () => { Deno.bench("write_null", { n: 5e5 }, () => {
Deno.writeSync(file.rid, dataChunk); Deno.writeSync(file.rid, dataChunk);
}); });
Deno.close(file.rid);
} }
function benchReadZero() { Deno.bench(
const buf = new Uint8Array(100); "read_128k",
const file = Deno.openSync("/dev/zero"); { n: 5e4 },
benchSync("read_zero", 5e5, () => { () => Deno.readFile("./cli/bench/testdata/128k.bin"),
Deno.readSync(file.rid, buf); );
});
Deno.close(file.rid);
}
function benchRead128k() { Deno.bench("request_new", { n: 5e5 }, () => new Request("https://deno.land"));
return benchAsync(
"read_128k",
5e4,
() => Deno.readFile("./cli/bench/testdata/128k.bin"),
);
}
function benchRequestNew() {
return benchSync("request_new", 5e5, () => new Request("https://deno.land"));
}
function benchOpVoidSync() {
return benchSync("op_void_sync", 1e7, () => Deno.core.opSync("op_void_sync"));
}
function benchOpVoidAsync() {
return benchAsyncP(
"op_void_async",
1e6,
1e3,
() => Deno.core.opAsync("op_void_async"),
);
}
async function main() {
// v8 builtin that's close to the upper bound non-NOPs
benchDateNow();
// Void ops measure op-overhead
benchOpVoidSync();
await benchOpVoidAsync();
// A very lightweight op, that should be highly optimizable
benchPerfNow();
// A common "language feature", that should be fast
// also a decent representation of a non-trivial JSON-op
benchUrlParse();
benchLargeBlobText();
benchB64RtLong();
benchB64RtShort();
// IO ops
benchReadZero();
benchWriteNull();
await benchRead128k();
benchRequestNew();
}
await main();