fix(bench): lower bench time budget when n is specified (#28454)

Closes #28430
This commit is contained in:
David Sherret 2025-03-10 16:17:19 -04:00 committed by GitHub
parent 9ea4f82643
commit ff28ecd91a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

View file

@ -241,7 +241,7 @@ const allMaxLength = 10_000_000;
let all = new Array(allMaxLength);
const lowPrecisionThresholdInNs = 1e4;
async function benchMeasure(timeBudget, fn, desc, context) {
async function benchMeasure(fn, desc, context) {
let n = 0;
let avg = 0;
let wavg = 0;
@ -251,7 +251,7 @@ async function benchMeasure(timeBudget, fn, desc, context) {
// warmup step
let c = 0;
let iterations = desc.warmup > 0 ? desc.warmup : 20;
let iterations = desc.warmup >= 0 ? desc.warmup : 20;
let budget = 10 * 1e6;
if (!desc.async) {
@ -298,7 +298,7 @@ async function benchMeasure(timeBudget, fn, desc, context) {
// measure step
iterations = desc.n > 0 ? desc.n : 10;
budget = timeBudget * 1e6;
budget = desc.n > 0 ? 10 * 1e6 : 500 * 1e6;
if (wavg > lowPrecisionThresholdInNs) {
if (!desc.async) {
@ -475,10 +475,8 @@ function wrapBenchmark(desc) {
});
}
const benchTimeInMs = 500;
const context = createBenchContext(desc);
const stats = await benchMeasure(
benchTimeInMs,
fn,
desc,
context,

View file

@ -1242,9 +1242,16 @@ declare namespace Deno {
/** If at least one bench has `only` set to true, only run benches that have
* `only` set to `true` and fail the bench suite. */
only?: boolean;
/** Number of iterations to perform. */
/** Number of iterations to perform.
* @remarks When the benchmark is very fast, this will only be used as a
* suggestion in order to get a more accurate measurement.
*/
n?: number;
/** Number of warmups to do before running the benchmark. */
/** Number of warmups to do before running the benchmark.
* @remarks A warmup will always be performed even if this is `0` in order to
* determine the speed of the benchmark in order to improve the measurement. When
* the benchmark is very fast, this will be used as a suggestion.
*/
warmup?: number;
/** Ensure the bench case does not prematurely cause the process to exit,
* for example via a call to {@linkcode Deno.exit}.