2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-03-26 09:13:53 -04:00
|
|
|
// 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();
|
2021-08-28 19:05:32 -04:00
|
|
|
console.log(benchStats(name, n, t1, t2));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function benchAsync(name, n, innerLoop) {
|
|
|
|
const t1 = Date.now();
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
await innerLoop(i);
|
|
|
|
}
|
|
|
|
const t2 = Date.now();
|
|
|
|
console.log(benchStats(name, n, t1, t2));
|
|
|
|
}
|
|
|
|
|
2021-10-10 10:36:55 -04:00
|
|
|
// Parallel version benchAsync
|
|
|
|
async function benchAsyncP(name, n, p, innerLoop) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:05:32 -04:00
|
|
|
function benchStats(name, n, t1, t2) {
|
2021-03-26 09:13:53 -04:00
|
|
|
const dt = (t2 - t1) / 1e3;
|
|
|
|
const r = n / dt;
|
2021-03-27 23:17:06 -04:00
|
|
|
const ns = Math.floor(dt / n * 1e9);
|
2021-08-28 19:05:32 -04:00
|
|
|
return `${name}:${" ".repeat(20 - name.length)}\t` +
|
|
|
|
`n = ${n}, dt = ${dt.toFixed(3)}s, r = ${r.toFixed(0)}/s, t = ${ns}ns/op`;
|
2021-03-26 09:13:53 -04:00
|
|
|
}
|
|
|
|
|
2022-03-07 05:12:16 -05:00
|
|
|
function benchB64RtLong() {
|
2022-03-04 15:45:24 -05:00
|
|
|
const input = "long-string".repeat(99999);
|
2022-03-07 05:12:16 -05:00
|
|
|
benchSync("b64_rt_long", 1e2, () => {
|
2022-03-04 15:45:24 -05:00
|
|
|
atob(btoa(input));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-07 05:12:16 -05:00
|
|
|
function benchB64RtShort() {
|
|
|
|
benchSync("b64_rt_short", 1e6, () => {
|
|
|
|
atob(btoa("123"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:13:53 -04:00
|
|
|
function benchUrlParse() {
|
|
|
|
benchSync("url_parse", 5e4, (i) => {
|
|
|
|
new URL(`http://www.google.com/${i}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-28 19:12:19 -04:00
|
|
|
function benchDateNow() {
|
|
|
|
benchSync("date_now", 5e5, () => {
|
|
|
|
Date.now();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function benchPerfNow() {
|
|
|
|
benchSync("perf_now", 5e5, () => {
|
2021-03-26 09:13:53 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:05:32 -04:00
|
|
|
function benchRead128k() {
|
|
|
|
return benchAsync(
|
|
|
|
"read_128k",
|
|
|
|
5e4,
|
2022-01-13 10:25:20 -05:00
|
|
|
() => Deno.readFile("./cli/bench/testdata/128k.bin"),
|
2021-08-28 19:05:32 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-21 06:34:27 -04:00
|
|
|
function benchRequestNew() {
|
|
|
|
return benchSync("request_new", 5e5, () => new Request("https://deno.land"));
|
|
|
|
}
|
|
|
|
|
2021-10-10 10:36:55 -04:00
|
|
|
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"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-28 19:05:32 -04:00
|
|
|
async function main() {
|
2021-03-28 19:12:19 -04:00
|
|
|
// v8 builtin that's close to the upper bound non-NOPs
|
|
|
|
benchDateNow();
|
2021-10-10 10:36:55 -04:00
|
|
|
// Void ops measure op-overhead
|
|
|
|
benchOpVoidSync();
|
|
|
|
await benchOpVoidAsync();
|
2021-03-28 19:12:19 -04:00
|
|
|
// 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
|
2021-03-26 09:13:53 -04:00
|
|
|
benchUrlParse();
|
2022-03-07 05:12:16 -05:00
|
|
|
benchB64RtLong();
|
|
|
|
benchB64RtShort();
|
2021-03-28 19:12:19 -04:00
|
|
|
// IO ops
|
2021-03-26 09:13:53 -04:00
|
|
|
benchReadZero();
|
2021-03-28 19:12:19 -04:00
|
|
|
benchWriteNull();
|
2021-08-28 19:05:32 -04:00
|
|
|
await benchRead128k();
|
2021-09-21 06:34:27 -04:00
|
|
|
benchRequestNew();
|
2021-03-26 09:13:53 -04:00
|
|
|
}
|
2021-08-28 19:05:32 -04:00
|
|
|
await main();
|