Skip to content

Commit ff28ecd

Browse files
authored
fix(bench): lower bench time budget when n is specified (#28454)
Closes #28430
1 parent 9ea4f82 commit ff28ecd

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

cli/js/40_bench.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ const allMaxLength = 10_000_000;
241241
let all = new Array(allMaxLength);
242242
const lowPrecisionThresholdInNs = 1e4;
243243

244-
async function benchMeasure(timeBudget, fn, desc, context) {
244+
async function benchMeasure(fn, desc, context) {
245245
let n = 0;
246246
let avg = 0;
247247
let wavg = 0;
@@ -251,7 +251,7 @@ async function benchMeasure(timeBudget, fn, desc, context) {
251251

252252
// warmup step
253253
let c = 0;
254-
let iterations = desc.warmup > 0 ? desc.warmup : 20;
254+
let iterations = desc.warmup >= 0 ? desc.warmup : 20;
255255
let budget = 10 * 1e6;
256256

257257
if (!desc.async) {
@@ -298,7 +298,7 @@ async function benchMeasure(timeBudget, fn, desc, context) {
298298

299299
// measure step
300300
iterations = desc.n > 0 ? desc.n : 10;
301-
budget = timeBudget * 1e6;
301+
budget = desc.n > 0 ? 10 * 1e6 : 500 * 1e6;
302302

303303
if (wavg > lowPrecisionThresholdInNs) {
304304
if (!desc.async) {
@@ -475,10 +475,8 @@ function wrapBenchmark(desc) {
475475
});
476476
}
477477

478-
const benchTimeInMs = 500;
479478
const context = createBenchContext(desc);
480479
const stats = await benchMeasure(
481-
benchTimeInMs,
482480
fn,
483481
desc,
484482
context,

cli/tsc/dts/lib.deno.ns.d.ts

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

0 commit comments

Comments
 (0)