Files
openidea/evals/lib/selection.ts
T
Mr0Bread d080ca28f8 fix(bench): require selected workloads and finite metrics (#4488)
Benchmark verdicts could pass with selected workloads or required metrics missing, empty or non-finite, and pnpm perf:rust could accept stale or partial criterion results. Selections and counts are now validated before any work starts, every selected workload must report finite values for its required metrics, and Rust results are collected only for the selected packages from fresh, well-formed criterion artifacts, against a list of the benchmarks each package owns.

Co-authored-by: Mr0Bread <55754540+Mr0Bread@users.noreply.github.com>
2026-11-19 19:34:55 +08:00

43 lines
1.8 KiB
TypeScript

import { countOption } from "../../perf/lib/options.ts";
import type { Condition } from "./verdict.ts";
interface Scenario {
id: string;
title: string;
conditions: readonly Condition[];
}
export interface ExpectedOutcome {
scenario: string;
title: string;
condition: Condition;
}
export function planSelection<T extends Scenario>(
scenarios: readonly T[],
args: { only?: string; condition?: string; runs?: string; warmup?: string },
) {
const runs = countOption("runs", args.runs ?? "3", 1);
const warmup = countOption("warmup", args.warmup ?? "1", 0);
if (args.condition !== undefined && args.condition !== "warm" && args.condition !== "cold")
throw new Error("--condition must be warm or cold");
const condition = args.condition as Condition | undefined;
const selectors = args.only?.split(",").map((s) => s.trim());
const matches = (s: T, selector: string) => s.id === selector || s.id.startsWith(`${selector}.`);
for (const selector of selectors ?? []) {
if (!selector) throw new Error("--only contains an empty selector");
const matching = scenarios.filter((s) => matches(s, selector));
if (!matching.length) throw new Error(`no scenario matches ${selector} (see --list)`);
if (!matching.some((s) => s.conditions.some((c) => !condition || c === condition)))
throw new Error(`no compatible scenario for selector ${selector} under ${condition}`);
}
const selected = scenarios.filter((s) => !selectors || selectors.some((selector) => matches(s, selector)));
const expected: ExpectedOutcome[] = selected.flatMap((s) =>
s.conditions
.filter((c) => !condition || c === condition)
.map((c) => ({ scenario: s.id, title: s.title, condition: c })),
);
if (!expected.length) throw new Error("no compatible scenarios selected");
return { selected, expected, runs, warmup, condition };
}