angular/scripts/benchmarks/targets.mts
Paul Gschwendtner bb52edee31 build: create script to run benchmarks and perform comparisons (#50745)
This commit creates a new script that solves the following use-cases:

 - Running benchmarks. It's not trivial to figure out the benchmark
   target names, and it's also easy to mess up the right Bazel flags.

 - Performing comparisons. When e.g. working on a runtime senstive
   change, it should be trivial to run benchmarks between the current
   working stage, and a base revision (e.g. `main`).

The script takes care of both these use-cases and comes with a
prompt-based command line tool experience.

The script will also be used by a future GitHub action that can run
comparisons triggered via GitHub PR comment (by trusted team members).

PR Close #50745
2023-06-18 19:32:34 +00:00

50 lines
1.7 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import path from 'path';
import {exec} from './utils.mjs';
/** Branded string representing a resolved Bazel benchmark target. */
export type ResolvedTarget = string & {
__resolvedTarget: true;
};
/** Finds all benchmark Bazel targets in the project. */
export async function findBenchmarkTargets(): Promise<string[]> {
return (
await exec('bazel', [
'query',
'--output=label',
`'kind("^web_test", //modules/...) intersect attr("name", "perf", //modules/...)'`,
])
)
.split(/\r?\n/)
.filter((t) => t !== '');
}
/** Gets the testlog path of a given Bazel target. */
export async function getTestlogPath(target: ResolvedTarget): Promise<string> {
return path.join(await bazelTestlogDir(), target.substring(2).replace(':', '/'));
}
/** Resolves a given benchmark Bazel target to the fully expanded label. */
export async function resolveTarget(target: string): Promise<ResolvedTarget> {
// If the target does not specify an explicit browser test target, we attempt
// to automatically add the Chromium suffix. This is necessary for e.g.
// resolving testlogs which would reside under the actual test target.
if (!target.endsWith('_chromium')) {
target = `${target}_chromium`;
}
return (await exec('bazel', ['query', '--output=label', target])).trim() as ResolvedTarget;
}
let testlogDir: string | null = null;
async function bazelTestlogDir(): Promise<string> {
return testlogDir ?? (testlogDir = (await exec('bazel', ['info', 'bazel-testlogs'])).trim());
}