mirror of
https://github.com/denoland/deno.git
synced 2025-08-31 07:47:46 +00:00
refactor(bench): align ops to testing ops (#23038)
Internal refactor that changes how we use ops in `deno bench` subcommand. This brings it in line to what we do in `deno test` subcommand.
This commit is contained in:
parent
86cdf37033
commit
c940205353
3 changed files with 78 additions and 48 deletions
|
@ -9,7 +9,12 @@ import {
|
|||
} from "ext:cli/40_test_common.js";
|
||||
import { Console } from "ext:deno_console/01_console.js";
|
||||
import { setExitHandler } from "ext:runtime/30_os.js";
|
||||
const ops = core.ops;
|
||||
const {
|
||||
op_register_bench,
|
||||
op_bench_get_origin,
|
||||
op_dispatch_bench_event,
|
||||
op_bench_now,
|
||||
} = core.ops;
|
||||
const {
|
||||
ArrayPrototypePush,
|
||||
Error,
|
||||
|
@ -30,6 +35,12 @@ let currentBenchUserExplicitEnd = null;
|
|||
|
||||
let registeredWarmupBench = false;
|
||||
|
||||
const registerBenchIdRetBuf = new Uint32Array(1);
|
||||
const registerBenchIdRetBufU8 = new Uint8Array(registerBenchIdRetBuf.buffer);
|
||||
|
||||
// As long as we're using one isolate per test, we can cache the origin since it won't change
|
||||
let cachedOrigin = undefined;
|
||||
|
||||
// Main bench function provided by Deno.
|
||||
function bench(
|
||||
nameOrFnOrOptions,
|
||||
|
@ -49,10 +60,22 @@ function bench(
|
|||
permissions: null,
|
||||
warmup: true,
|
||||
};
|
||||
if (cachedOrigin == undefined) {
|
||||
cachedOrigin = op_bench_get_origin();
|
||||
}
|
||||
warmupBenchDesc.fn = wrapBenchmark(warmupBenchDesc);
|
||||
const { id, origin } = ops.op_register_bench(warmupBenchDesc);
|
||||
warmupBenchDesc.id = id;
|
||||
warmupBenchDesc.origin = origin;
|
||||
op_register_bench(
|
||||
warmupBenchDesc.fn,
|
||||
warmupBenchDesc.name,
|
||||
warmupBenchDesc.baseline,
|
||||
warmupBenchDesc.group,
|
||||
warmupBenchDesc.ignore,
|
||||
warmupBenchDesc.only,
|
||||
warmupBenchDesc.warmup,
|
||||
registerBenchIdRetBufU8,
|
||||
);
|
||||
warmupBenchDesc.id = registerBenchIdRetBufU8[0];
|
||||
warmupBenchDesc.origin = cachedOrigin;
|
||||
}
|
||||
|
||||
let benchDesc;
|
||||
|
@ -139,10 +162,21 @@ function bench(
|
|||
benchDesc.fn = wrapBenchmark(benchDesc);
|
||||
benchDesc.warmup = false;
|
||||
benchDesc.name = escapeName(benchDesc.name);
|
||||
|
||||
const { id, origin } = ops.op_register_bench(benchDesc);
|
||||
benchDesc.id = id;
|
||||
benchDesc.origin = origin;
|
||||
if (cachedOrigin == undefined) {
|
||||
cachedOrigin = op_bench_get_origin();
|
||||
}
|
||||
op_register_bench(
|
||||
benchDesc.fn,
|
||||
benchDesc.name,
|
||||
benchDesc.baseline,
|
||||
benchDesc.group,
|
||||
benchDesc.ignore,
|
||||
benchDesc.only,
|
||||
false,
|
||||
registerBenchIdRetBufU8,
|
||||
);
|
||||
benchDesc.id = registerBenchIdRetBufU8[0];
|
||||
benchDesc.origin = cachedOrigin;
|
||||
}
|
||||
|
||||
function compareMeasurements(a, b) {
|
||||
|
@ -381,7 +415,7 @@ function wrapBenchmark(desc) {
|
|||
|
||||
try {
|
||||
globalThis.console = new Console((s) => {
|
||||
ops.op_dispatch_bench_event({ output: s });
|
||||
op_dispatch_bench_event({ output: s });
|
||||
});
|
||||
|
||||
if (desc.permissions) {
|
||||
|
@ -420,7 +454,7 @@ function wrapBenchmark(desc) {
|
|||
}
|
||||
|
||||
function benchNow() {
|
||||
return ops.op_bench_now();
|
||||
return op_bench_now();
|
||||
}
|
||||
|
||||
globalThis.Deno.bench = bench;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue