angular/scripts/benchmarks/utils.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

51 lines
1.4 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 {Log} from '@angular/ng-dev';
import childProcess from 'child_process';
import path from 'path';
import url from 'url';
const scriptDir = path.dirname(url.fileURLToPath(import.meta.url));
/** Absolute disk path to the project directory. */
export const projectDir = path.join(scriptDir, '../..');
/**
* Executes the given command, forwarding stdin, stdout and stderr while
* still capturing stdout in order to return it.
*/
export function exec(cmd: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
Log.info('Running command:', cmd, args.join(' '));
const proc = childProcess.spawn(cmd, args, {
shell: true,
cwd: projectDir,
// Only capture `stdout`. Forward the rest to the parent TTY.
stdio: ['inherit', 'pipe', 'inherit'],
});
let stdout = '';
proc.stdout.on('data', (chunk) => {
stdout += chunk.toString('utf8');
process.stdout.write(chunk);
});
proc.on('close', (status, signal) => {
if (status !== 0 || signal !== null) {
reject(`Command failed. Status code: ${status}. Signal: ${signal}`);
}
resolve(stdout);
});
proc.on('error', (err) => {
reject(`Command failed: ${err}`);
});
});
}