mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
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>
158 lines
6.7 KiB
TypeScript
158 lines
6.7 KiB
TypeScript
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
import { join, relative, resolve, sep } from "node:path";
|
|
|
|
// Package-owned expectations, independent of collected artifacts and human-owned budgets.
|
|
export const RUST_BENCHMARKS: Readonly<Record<string, readonly string[]>> = {
|
|
"ide-text": [
|
|
"text/open_1mb",
|
|
"text/apply_keystroke_1mb",
|
|
"text/apply_100_changes_1mb",
|
|
"text/diff_two_edits_1mb",
|
|
"text/diff_every_line_1mb",
|
|
"text/offset_to_pos_round_trip",
|
|
"text/pos_to_byte",
|
|
"text/text_for_save_crlf_1mb",
|
|
"text/diff_one_edit_5mb",
|
|
],
|
|
"ide-language": [
|
|
"syntax_2mb/outline_uncached",
|
|
"syntax_2mb/folding_uncached",
|
|
"syntax_2mb/outline_cached_hit",
|
|
"syntax_2mb/folding_cached_hit",
|
|
"syntax_2mb/selection_cached_hit",
|
|
"syntax_2mb/folding_after_keystroke_incremental",
|
|
"syntax_2mb/outline_after_keystroke_incremental",
|
|
],
|
|
"ide-index": [
|
|
"index_100k/find_files/ordcon",
|
|
"index_100k/find_files/src/user",
|
|
"index_100k/find_files/PaymentService",
|
|
"index_100k/find_files/zzzz",
|
|
"index_100k/find_files/a",
|
|
"index_100k/find_dirs/util",
|
|
"index_100k/find_dirs/src/ui",
|
|
"index_100k/find_dirs/a",
|
|
"index_100k/find_symbols/OCo",
|
|
"index_100k/find_symbols/invoiceRepo",
|
|
"index_100k/find_symbols/renderView",
|
|
"index_100k/find_symbols/q",
|
|
"index_100k/find_symbols_types/UsSe",
|
|
"startup_20k/reopen_wait_idle",
|
|
],
|
|
"ide-search": [
|
|
"search_5k/literal",
|
|
"search_5k/regex",
|
|
"search_5k/dense_limit",
|
|
"search_dense_files/24x40k_limit",
|
|
"search_excluded_subtrees/5k_dependencies",
|
|
],
|
|
"ide-host": ["plugins/load_50", "plugins/cold_activation", "plugins/syntax_highlights_2k_lines"],
|
|
"ide-heap": ["heap/index_1m_objects", "heap/dominators_1m_random"],
|
|
};
|
|
|
|
const OBSERVE_ONLY: Readonly<Record<string, true>> = {
|
|
"syntax_2mb/outline_uncached": true,
|
|
"syntax_2mb/folding_uncached": true,
|
|
"index_100k/find_files/zzzz": true,
|
|
"index_100k/find_symbols/renderView": true,
|
|
"startup_20k/reopen_wait_idle": true,
|
|
"search_excluded_subtrees/5k_dependencies": true,
|
|
};
|
|
|
|
export function selectRustPackages(packages: readonly string[] = Object.keys(RUST_BENCHMARKS)): string[] {
|
|
if (packages.length === 0) throw new Error("empty Rust package selection");
|
|
for (const p of packages) {
|
|
if (!Object.hasOwn(RUST_BENCHMARKS, p)) throw new Error(`unknown Rust package: ${JSON.stringify(p)}`);
|
|
}
|
|
return [...new Set(packages)];
|
|
}
|
|
|
|
export function rustPaths(root: string, env: { CARGO_TARGET_DIR?: string; CRITERION_HOME?: string }) {
|
|
const target = resolve(root, env.CARGO_TARGET_DIR ?? "target");
|
|
return { target, criterion: resolve(root, env.CRITERION_HOME ?? join(target, "criterion")) };
|
|
}
|
|
|
|
export function rustBudgets(packages: readonly string[], budgets: Record<string, number>): Record<string, number> {
|
|
const selected = selectRustPackages(packages);
|
|
const ids = new Set(Object.values(RUST_BENCHMARKS).flat());
|
|
for (const id of Object.keys(OBSERVE_ONLY)) {
|
|
if (!ids.has(id)) throw new Error(`unknown Rust observe-only classification: ${id}`);
|
|
}
|
|
for (const [id, value] of Object.entries(budgets)) {
|
|
if (!ids.has(id)) throw new Error(`unknown Rust budget ID: ${id}`);
|
|
if (Object.hasOwn(OBSERVE_ONLY, id)) throw new Error(`Rust observe-only ID has an absolute budget: ${id}`);
|
|
if (!Number.isFinite(value) || value < 0) throw new Error(`invalid Rust budget: ${id}`);
|
|
}
|
|
for (const id of ids) {
|
|
if (!Object.hasOwn(budgets, id) && !Object.hasOwn(OBSERVE_ONLY, id)) {
|
|
throw new Error(`missing Rust budget classification: ${id}`);
|
|
}
|
|
}
|
|
return Object.fromEntries(
|
|
selected
|
|
.flatMap((p) => RUST_BENCHMARKS[p]!)
|
|
.filter((id) => !Object.hasOwn(OBSERVE_ONLY, id))
|
|
.map((id) => [`rust.${id}`, budgets[id]!]),
|
|
);
|
|
}
|
|
|
|
/** Read the selected workload only; fresh runs require both artifact files since `started`. */
|
|
export function collectRustMetrics(dir: string, packages: readonly string[], started?: number): Record<string, number> {
|
|
const selected = new Set(selectRustPackages(packages));
|
|
const expected = new Set([...selected].flatMap((p) => RUST_BENCHMARKS[p]!));
|
|
const owners = new Map<string, string>();
|
|
const groups = new Map<string, string>();
|
|
for (const [p, ids] of Object.entries(RUST_BENCHMARKS)) {
|
|
for (const id of ids) {
|
|
owners.set(id, p);
|
|
groups.set(id.split("/")[0]!, p);
|
|
}
|
|
}
|
|
const metrics: Record<string, number> = {};
|
|
function readNew(sub: string) {
|
|
// Criterion sanitizes slashes in ungrouped function names to underscores.
|
|
const top = relative(dir, sub).split(sep)[0]!;
|
|
const pathOwner = [...groups].find(([group]) => top === group || top.startsWith(`${group}_`))?.[1];
|
|
if (pathOwner && !selected.has(pathOwner)) return;
|
|
const bench = join(sub, "new", "benchmark.json");
|
|
const estimates = join(sub, "new", "estimates.json");
|
|
if (!existsSync(bench)) {
|
|
if (existsSync(estimates) && (!pathOwner || selected.has(pathOwner))) {
|
|
throw new Error(`missing Criterion benchmark.json: ${sub}`);
|
|
}
|
|
return;
|
|
}
|
|
const metadata: unknown = JSON.parse(readFileSync(bench, "utf8"));
|
|
const id = metadata && typeof metadata === "object" && "full_id" in metadata ? metadata.full_id : undefined;
|
|
if (typeof id !== "string" || !id) {
|
|
throw new Error(`invalid Criterion full_id: ${bench}`);
|
|
}
|
|
const owner = owners.get(id) ?? groups.get(id.split("/")[0]!);
|
|
if (owner && !selected.has(owner)) return;
|
|
if (!expected.has(id)) throw new Error(`unknown selected Criterion ID: ${id}`);
|
|
if (Object.hasOwn(metrics, id)) throw new Error(`duplicate Criterion ID: ${id}`);
|
|
for (const file of [bench, estimates]) {
|
|
if (started !== undefined && statSync(file).mtimeMs < started)
|
|
throw new Error(`stale Criterion artifact: ${file}`);
|
|
}
|
|
const est: unknown = JSON.parse(readFileSync(estimates, "utf8"));
|
|
const mean = est && typeof est === "object" && "mean" in est ? est.mean : undefined;
|
|
const value = mean && typeof mean === "object" && "point_estimate" in mean ? mean.point_estimate : undefined;
|
|
if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
|
|
throw new Error(`invalid Criterion mean estimate: ${estimates}`);
|
|
}
|
|
metrics[id] = value / 1000;
|
|
}
|
|
function visit(path: string) {
|
|
if (!existsSync(path)) return;
|
|
readNew(path);
|
|
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
if (entry.isDirectory() && !["new", "base", "report"].includes(entry.name)) visit(join(path, entry.name));
|
|
}
|
|
}
|
|
visit(dir);
|
|
const missing = [...expected].filter((id) => !Object.hasOwn(metrics, id));
|
|
if (missing.length) throw new Error(`missing selected Criterion IDs: ${missing.join(", ")}`);
|
|
return metrics;
|
|
}
|