|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { killPortProcess } from 'kill-port-process'; |
| 4 | + |
| 5 | +import fs from 'fs-extra'; |
| 6 | + |
| 7 | +import { getOrBuildControlTarball } from './control.mjs'; |
| 8 | +import { buildExperimentTarball } from './experiment.mjs'; |
| 9 | +import { run, prepareApp, sleep, startVitePreview, lsof } from './utils.mjs'; |
| 10 | + |
| 11 | +const { ensureDir, remove, writeFile } = fs; |
| 12 | + |
| 13 | +function buildMarkersString(markers) { |
| 14 | + return markers |
| 15 | + .reduce((acc, marker) => { |
| 16 | + return acc + ',' + marker + 'Start,' + marker + 'End'; |
| 17 | + }, '') |
| 18 | + .split(',') |
| 19 | + .map((s) => s.trim()) |
| 20 | + .filter(Boolean) |
| 21 | + .join(','); |
| 22 | +} |
| 23 | + |
| 24 | +// Default configuration for runBenchmark |
| 25 | +const DEFAULT_CONTROL_BRANCH_NAME = 'main'; |
| 26 | +const DEFAULT_CONTROL_APP_FROM_MAIN = false; |
| 27 | +const DEFAULT_CONTROL_PORT = 4500; |
| 28 | +const DEFAULT_EXPERIMENT_PORT = 4501; |
| 29 | +const DEFAULT_FIDELITY = process.env['RUNS'] || '20'; |
| 30 | +const DEFAULT_THROTTLE = '1'; |
| 31 | +const DEFAULT_REGRESSION_THRESHOLD = '25'; |
| 32 | +const DEFAULT_SAMPLE_TIMEOUT = '60'; |
| 33 | +const DEFAULT_MARKERS = [ |
| 34 | + // Copied from glimmer-vm/bin/setup-bench.mts (krausest benchmark) |
| 35 | + 'render', |
| 36 | + 'render1000Items1', |
| 37 | + 'clearItems1', |
| 38 | + 'render1000Items2', |
| 39 | + 'clearItems2', |
| 40 | + 'render5000Items1', |
| 41 | + 'clearManyItems1', |
| 42 | + 'render5000Items2', |
| 43 | + 'clearManyItems2', |
| 44 | + 'render1000Items3', |
| 45 | + 'append1000Items1', |
| 46 | + 'append1000Items2', |
| 47 | + 'updateEvery10thItem1', |
| 48 | + 'updateEvery10thItem2', |
| 49 | + 'selectFirstRow1', |
| 50 | + 'selectSecondRow1', |
| 51 | + 'removeFirstRow1', |
| 52 | + 'removeSecondRow1', |
| 53 | + 'swapRows1', |
| 54 | + 'swapRows2', |
| 55 | + 'clearItems4', |
| 56 | +]; |
| 57 | + |
| 58 | +import { REPO_ROOT, BENCH_ROOT } from './utils.mjs'; |
| 59 | + |
| 60 | +export async function runBenchmark({ force = false, reuse = false } = {}) { |
| 61 | + // Use config constants directly; no local re-assignment |
| 62 | + |
| 63 | + await ensureDir(BENCH_ROOT); |
| 64 | + |
| 65 | + const CONTROL_DIRS = { |
| 66 | + repo: join(BENCH_ROOT, 'ember-source-control'), |
| 67 | + app: join(BENCH_ROOT, 'control'), |
| 68 | + }; |
| 69 | + const EXPERIMENT_DIRS = { |
| 70 | + app: join(BENCH_ROOT, 'experiment'), |
| 71 | + repo: REPO_ROOT, |
| 72 | + }; |
| 73 | + |
| 74 | + const controlUrl = `http://127.0.0.1:${DEFAULT_CONTROL_PORT}`; |
| 75 | + const experimentUrl = `http://127.0.0.1:${DEFAULT_EXPERIMENT_PORT}`; |
| 76 | + const markersString = buildMarkersString(DEFAULT_MARKERS); |
| 77 | + |
| 78 | + if (force) { |
| 79 | + await killPortProcess([DEFAULT_CONTROL_PORT, DEFAULT_EXPERIMENT_PORT]); |
| 80 | + await remove(CONTROL_DIRS.repo); |
| 81 | + await remove(CONTROL_DIRS.app); |
| 82 | + await remove(EXPERIMENT_DIRS.app); |
| 83 | + } |
| 84 | + |
| 85 | + await ensureDir(BENCH_ROOT); |
| 86 | + await ensureDir(EXPERIMENT_DIRS.app); |
| 87 | + await ensureDir(CONTROL_DIRS.app); |
| 88 | + |
| 89 | + const controlTarball = await getOrBuildControlTarball({ |
| 90 | + repoRoot: REPO_ROOT, |
| 91 | + controlRepoDir: CONTROL_DIRS.repo, |
| 92 | + controlBranchName: DEFAULT_CONTROL_BRANCH_NAME, |
| 93 | + }); |
| 94 | + |
| 95 | + const experimentTarball = await buildExperimentTarball({ |
| 96 | + repoDir: EXPERIMENT_DIRS.repo, |
| 97 | + reuse, |
| 98 | + }); |
| 99 | + |
| 100 | + const experimentAppSource = join(REPO_ROOT, 'smoke-tests/benchmark-app'); |
| 101 | + const controlAppSource = DEFAULT_CONTROL_APP_FROM_MAIN |
| 102 | + ? join(CONTROL_DIRS.repo, 'smoke-tests/benchmark-app') |
| 103 | + : experimentAppSource; |
| 104 | + |
| 105 | + await Promise.all([ |
| 106 | + prepareApp({ |
| 107 | + sourceAppDir: controlAppSource, |
| 108 | + destAppDir: CONTROL_DIRS.app, |
| 109 | + emberSourceTarball: controlTarball, |
| 110 | + reuse, |
| 111 | + }), |
| 112 | + prepareApp({ |
| 113 | + sourceAppDir: experimentAppSource, |
| 114 | + destAppDir: EXPERIMENT_DIRS.app, |
| 115 | + emberSourceTarball: experimentTarball, |
| 116 | + reuse, |
| 117 | + }), |
| 118 | + ]); |
| 119 | + |
| 120 | + // These will error if the parts are occupied (--strict-port) |
| 121 | + startVitePreview({ appDir: CONTROL_DIRS.app, port: DEFAULT_CONTROL_PORT }); |
| 122 | + startVitePreview({ |
| 123 | + appDir: EXPERIMENT_DIRS.app, |
| 124 | + port: DEFAULT_EXPERIMENT_PORT, |
| 125 | + }); |
| 126 | + |
| 127 | + async function cleanup() { |
| 128 | + console.log(`\n\tCleaning up servers...`); |
| 129 | + |
| 130 | + await killPortProcess([DEFAULT_CONTROL_PORT, DEFAULT_EXPERIMENT_PORT]); |
| 131 | + } |
| 132 | + |
| 133 | + process.on('exit', cleanup); |
| 134 | + process.on('SIGINT', () => { |
| 135 | + cleanup(); |
| 136 | + // eslint-disable-next-line n/no-process-exit |
| 137 | + process.exit(1); |
| 138 | + }); |
| 139 | + |
| 140 | + // give servers a moment to start |
| 141 | + await sleep(5000); |
| 142 | + |
| 143 | + /** |
| 144 | + * We need to make sure both servers are running before starting the benchmark. |
| 145 | + */ |
| 146 | + let controlLsof = await lsof(DEFAULT_CONTROL_PORT); |
| 147 | + let experimentLsof = await lsof(DEFAULT_EXPERIMENT_PORT); |
| 148 | + |
| 149 | + if (!controlLsof || !experimentLsof) { |
| 150 | + throw new Error( |
| 151 | + `One of the servers failed to start. Control server lsof:\n${controlLsof}\n\nExperiment server lsof:\n${experimentLsof}` |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + const tracerbenchBin = join(REPO_ROOT, 'node_modules/tracerbench/bin/run'); |
| 156 | + |
| 157 | + const args = [ |
| 158 | + '--single-threaded-gc', |
| 159 | + tracerbenchBin, |
| 160 | + 'compare', |
| 161 | + '--regressionThreshold', |
| 162 | + DEFAULT_REGRESSION_THRESHOLD, |
| 163 | + '--sampleTimeout', |
| 164 | + DEFAULT_SAMPLE_TIMEOUT, |
| 165 | + '--fidelity', |
| 166 | + DEFAULT_FIDELITY, |
| 167 | + '--controlURL', |
| 168 | + controlUrl, |
| 169 | + '--experimentURL', |
| 170 | + experimentUrl, |
| 171 | + '--report', |
| 172 | + '--headless', |
| 173 | + '--cpuThrottleRate', |
| 174 | + DEFAULT_THROTTLE, |
| 175 | + '--markers', |
| 176 | + markersString, |
| 177 | + '--debug', |
| 178 | + '--browserArgs', |
| 179 | + `"--incognito,--disable-gpu,--mute-audio,--log-level=3,--headless=new"`, |
| 180 | + ]; |
| 181 | + |
| 182 | + const output = await run('node', args, { cwd: EXPERIMENT_DIRS.app }); |
| 183 | + const msgFile = join(BENCH_ROOT, 'msg.txt'); |
| 184 | + |
| 185 | + if (!process.env.CI) { |
| 186 | + await writeFile( |
| 187 | + msgFile, |
| 188 | + output.stdout.split('Benchmark Results Summary').pop() ?? output.stdout, |
| 189 | + 'utf8' |
| 190 | + ); |
| 191 | + } |
| 192 | + |
| 193 | + await cleanup(); |
| 194 | + |
| 195 | + return { |
| 196 | + benchRoot: BENCH_ROOT, |
| 197 | + msgFile, |
| 198 | + controlUrl, |
| 199 | + experimentUrl, |
| 200 | + }; |
| 201 | +} |
0 commit comments