Skip to content

Commit a2c9191

Browse files
committed
refactor and add cmdline args for benchmark driver
1 parent 31f2b5e commit a2c9191

18 files changed

+451
-633
lines changed

webdriver-ts/src/benchmarkConfiguration.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

webdriver-ts/src/benchmarkRunner.ts

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
import { fork } from "child_process";
22
import * as fs from "fs";
33
import * as yargs from "yargs";
4-
import { BenchmarkInfo, BenchmarkType, TBenchmark } from "./benchmarksCommon";
5-
import { CPUBenchmarkPuppeteer, MemBenchmarkPuppeteer, TBenchmarkPuppeteer } from "./benchmarksPuppeteer";
6-
import { CPUBenchmarkPlaywright, MemBenchmarkPlaywright, TBenchmarkPlaywright } from "./benchmarksPlaywright";
7-
import { CPUBenchmarkWebdriver } from "./benchmarksWebdriver";
8-
import { BenchmarkDriverOptions, BenchmarkOptions, config, ErrorAndWarning, FrameworkData, initializeFrameworks } from "./common";
4+
import { BenchmarkInfo, benchmarkInfos, BenchmarkType, CPUBenchmarkInfo, MemBenchmarkInfo, StartupBenchmarkInfo } from "./benchmarksCommon";
5+
import { StartupBenchmarkResult } from "./benchmarksLighthouse";
6+
import { BenchmarkOptions, BENCHMARK_RUNNER, config, ErrorAndWarning, FrameworkData, initializeFrameworks } from "./common";
97
import { writeResults } from "./writeResults";
10-
import {benchmarks, TBenchmarkImplementation} from "./benchmarkConfiguration";
11-
import { BenchmarkLighthouse, StartupBenchmarkResult } from "./benchmarksLighthouse";
12-
import { CPUBenchmarkWebdriverCDP } from "./benchmarksWebdriverCDP";
138

149
function forkAndCallBenchmark(
1510
framework: FrameworkData,
16-
benchmark: CPUBenchmarkWebdriver|TBenchmarkPuppeteer|BenchmarkLighthouse,
11+
benchmarkInfo: BenchmarkInfo,
1712
benchmarkOptions: BenchmarkOptions
1813
): Promise<ErrorAndWarning> {
1914
return new Promise((resolve, reject) => {
2015
let forkedRunner = null;
21-
if (benchmark instanceof BenchmarkLighthouse) {
16+
if (benchmarkInfo.type === BenchmarkType.STARTUP_MAIN) {
2217
forkedRunner = "dist/forkedBenchmarkRunnerLighthouse.js";
23-
} else if (benchmark instanceof CPUBenchmarkWebdriverCDP) {
18+
} else if (config.BENCHMARK_RUNNER == BENCHMARK_RUNNER.WEBDRIVER_CDP) {
2419
forkedRunner = "dist/forkedBenchmarkRunnerWebdriverCDP.js";
25-
} else if (benchmark instanceof CPUBenchmarkPlaywright || benchmark instanceof MemBenchmarkPlaywright) {
20+
} else if (config.BENCHMARK_RUNNER == BENCHMARK_RUNNER.PLAYWRIGHT) {
2621
forkedRunner = "dist/forkedBenchmarkRunnerPlaywright.js";
27-
} else if (benchmark instanceof CPUBenchmarkPuppeteer || benchmark instanceof MemBenchmarkPuppeteer) {
28-
forkedRunner = "dist/forkedBenchmarkRunnerPuppeteer.js";
29-
} else {
22+
} else if (config.BENCHMARK_RUNNER == BENCHMARK_RUNNER.WEBDRIVER) {
3023
forkedRunner = "dist/forkedBenchmarkRunnerWebdriver.js";
24+
} else {
25+
forkedRunner = "dist/forkedBenchmarkRunnerPuppeteer.js";
3126
}
3227
console.log("forking ",forkedRunner);
3328
const forked = fork(forkedRunner);
3429
if (config.LOG_DETAILS) console.log("FORKING: forked child process");
3530
forked.send({
3631
config,
3732
framework,
38-
benchmarkId: benchmark.benchmarkInfo.id,
33+
benchmarkId: benchmarkInfo.id,
3934
benchmarkOptions,
4035
});
4136
forked.on("message", async (msg: ErrorAndWarning) => {
@@ -57,7 +52,7 @@ function forkAndCallBenchmark(
5752

5853
async function runBenchmakLoopStartup(
5954
framework: FrameworkData,
60-
benchmark: BenchmarkLighthouse,
55+
benchmarkInfo: StartupBenchmarkInfo,
6156
benchmarkOptions: BenchmarkOptions
6257
): Promise<{ errors: String[]; warnings: String[] }> {
6358
let warnings: String[] = [];
@@ -70,12 +65,12 @@ async function runBenchmakLoopStartup(
7065
let retries = 0;
7166
let done = 0;
7267

73-
console.log("runBenchmakLoopStartup", framework, benchmark);
68+
console.log("runBenchmakLoopStartup", framework, benchmarkInfo);
7469

7570

7671
while (done < count) {
77-
console.log("FORKING: ", benchmark.benchmarkInfo.id, " BatchSize ", benchmarkOptions.batchSize);
78-
let res = await forkAndCallBenchmark(framework, benchmark, benchmarkOptions);
72+
console.log("FORKING: ", benchmarkInfo.id, " BatchSize ", benchmarkOptions.batchSize);
73+
let res = await forkAndCallBenchmark(framework, benchmarkInfo, benchmarkOptions);
7974
if (Array.isArray(res.result)) {
8075
results = results.concat(res.result as StartupBenchmarkResult[]);
8176
} else results.push(res.result);
@@ -86,7 +81,7 @@ async function runBenchmakLoopStartup(
8681
retries++;
8782
if (retries == 3) break;
8883
} else {
89-
errors.push(`Executing ${framework.uri} and benchmark ${benchmark.benchmarkInfo.id} failed: ` + res.error);
84+
errors.push(`Executing ${framework.uri} and benchmark ${benchmarkInfo.id} failed: ` + res.error);
9085
break;
9186
}
9287
}
@@ -95,7 +90,7 @@ async function runBenchmakLoopStartup(
9590
console.log("******* result ", results);
9691
await writeResults(config, {
9792
framework: framework,
98-
benchmark: benchmark,
93+
benchmark: benchmarkInfo,
9994
results: results,
10095
type: BenchmarkType.STARTUP
10196
});
@@ -106,7 +101,7 @@ async function runBenchmakLoopStartup(
106101

107102
async function runBenchmakLoop(
108103
framework: FrameworkData,
109-
benchmark: CPUBenchmarkWebdriver | TBenchmarkPuppeteer,
104+
benchmarkInfo: CPUBenchmarkInfo|MemBenchmarkInfo,
110105
benchmarkOptions: BenchmarkOptions
111106
): Promise<{ errors: String[]; warnings: String[] }> {
112107
let warnings: String[] = [];
@@ -115,23 +110,23 @@ async function runBenchmakLoop(
115110
let results: Array<number> = [];
116111
let count = 0;
117112

118-
if (benchmark.type == BenchmarkType.CPU) {
113+
if (benchmarkInfo.type == BenchmarkType.CPU) {
119114
count = benchmarkOptions.numIterationsForCPUBenchmarks;
120115
// FIXME
121-
benchmarkOptions.batchSize = config.ALLOW_BATCHING && (benchmark.benchmarkInfo as any).allowBatching ? count : 1;
122-
} else if (benchmark.type == BenchmarkType.MEM) {
116+
benchmarkOptions.batchSize = config.ALLOW_BATCHING && benchmarkInfo.allowBatching ? count : 1;
117+
} else if (benchmarkInfo.type == BenchmarkType.MEM) {
123118
count = benchmarkOptions.numIterationsForMemBenchmarks;
124119
benchmarkOptions.batchSize = 1;
125120
}
126121

127122
let retries = 0;
128123

129-
console.log("runBenchmakLoop", framework, benchmark);
124+
console.log("runBenchmakLoop", framework, benchmarkInfo);
130125

131126
while (results.length < count) {
132127
benchmarkOptions.batchSize = Math.min(benchmarkOptions.batchSize, count - results.length);
133-
console.log("FORKING: ", benchmark.benchmarkInfo.id, " BatchSize ", benchmarkOptions.batchSize);
134-
let res = await forkAndCallBenchmark(framework, benchmark, benchmarkOptions);
128+
console.log("FORKING: ", benchmarkInfo.id, " BatchSize ", benchmarkOptions.batchSize);
129+
let res = await forkAndCallBenchmark(framework, benchmarkInfo, benchmarkOptions);
135130
if (Array.isArray(res.result)) {
136131
results = results.concat(res.result as number[]);
137132
} else results.push(res.result);
@@ -142,12 +137,12 @@ async function runBenchmakLoop(
142137
retries++;
143138
if (retries == 3) break;
144139
} else {
145-
errors.push(`Executing ${framework.uri} and benchmark ${benchmark.benchmarkInfo.id} failed: ` + res.error);
140+
errors.push(`Executing ${framework.uri} and benchmark ${benchmarkInfo.id} failed: ` + res.error);
146141
break;
147142
}
148143
}
149144
}
150-
if (benchmark.type == BenchmarkType.CPU) {
145+
if (benchmarkInfo.type == BenchmarkType.CPU) {
151146
console.log("CPU results before: ", results);
152147
(results as number[]).sort((a: number, b: number) => a - b);
153148
results = results.slice(0, config.NUM_ITERATIONS_FOR_BENCHMARK_CPU);
@@ -157,23 +152,19 @@ async function runBenchmakLoop(
157152
console.log("******* result ", results);
158153
await writeResults(config, {
159154
framework: framework,
160-
benchmark: benchmark.benchmarkInfo,
155+
benchmark: benchmarkInfo,
161156
results: results,
162-
type: benchmark.type as typeof BenchmarkType.CPU|BenchmarkType.MEM
157+
type: benchmarkInfo.type as typeof BenchmarkType.CPU|BenchmarkType.MEM
163158
});
164159
return { errors, warnings };
165160
// } else {
166161
// return executeBenchmark(frameworks, keyed, frameworkName, benchmarkName, benchmarkOptions);
167162
}
168163

169-
async function runBench(runFrameworks: FrameworkData[], benchmarkNames: string[]) {
164+
async function runBench(runFrameworks: FrameworkData[], benchmarkInfos: BenchmarkInfo[]) {
170165
let errors: String[] = [];
171166
let warnings: String[] = [];
172167

173-
let runBenchmarks: Array<TBenchmarkImplementation> = benchmarks.filter((b) =>
174-
benchmarkNames.some((name) => b.benchmarkInfo.id.toLowerCase().indexOf(name) > -1)
175-
);
176-
177168
let restart: string = undefined;
178169
let index = runFrameworks.findIndex((f) => f.fullNameWithKeyedAndVersion === restart);
179170
if (index > -1) {
@@ -186,13 +177,14 @@ async function runBench(runFrameworks: FrameworkData[], benchmarkNames: string[]
186177
);
187178
console.log(
188179
"Benchmarks that will be run",
189-
runBenchmarks.map((b) => b.benchmarkInfo.id)
180+
benchmarkInfos.map((b) => b.id)
190181
);
191182

192183
console.log("HEADLESS*** ", args.headless);
193184

194185
let benchmarkOptions: BenchmarkOptions = {
195186
port: config.PORT.toFixed(),
187+
HOST: config.HOST,
196188
remoteDebuggingPort: config.REMOTE_DEBUGGING_PORT,
197189
chromePort: config.CHROME_PORT,
198190
headless: args.headless,
@@ -203,13 +195,20 @@ async function runBench(runFrameworks: FrameworkData[], benchmarkNames: string[]
203195
batchSize: 1,
204196
};
205197

198+
console.log("benchmarkOptions", benchmarkOptions);
199+
206200
for (let i = 0; i < runFrameworks.length; i++) {
207-
for (let j = 0; j < runBenchmarks.length; j++) {
201+
for (let j = 0; j < benchmarkInfos.length; j++) {
208202
try {
209-
console.log("****** runBenchmarks[j].type", runBenchmarks[j].type, runBenchmarks[j].type == BenchmarkType.STARTUP_MAIN)
210-
let result = (runBenchmarks[j].type == BenchmarkType.STARTUP_MAIN) ?
211-
await runBenchmakLoopStartup(runFrameworks[i], runBenchmarks[j] as BenchmarkLighthouse, benchmarkOptions)
212-
: await runBenchmakLoop(runFrameworks[i], runBenchmarks[j] as TBenchmarkPuppeteer|CPUBenchmarkWebdriver, benchmarkOptions);
203+
let result;
204+
205+
if (benchmarkInfos[j].type == BenchmarkType.STARTUP_MAIN) {
206+
result = await runBenchmakLoopStartup(runFrameworks[i], benchmarkInfos[j] as StartupBenchmarkInfo, benchmarkOptions)
207+
} else if (benchmarkInfos[j].type == BenchmarkType.CPU) {
208+
result = await runBenchmakLoop(runFrameworks[i], benchmarkInfos[j] as CPUBenchmarkInfo, benchmarkOptions);
209+
} else {
210+
result = await runBenchmakLoop(runFrameworks[i], benchmarkInfos[j] as MemBenchmarkInfo, benchmarkOptions);
211+
}
213212
errors = errors.concat(result.errors);
214213
warnings = warnings.concat(result.warnings);
215214
} catch (e) {
@@ -252,18 +251,37 @@ let args: any = yargs(process.argv)
252251
.help("help")
253252
.boolean("headless").default("headless", false)
254253
.boolean("smoketest")
254+
.string("runner").default("runner","playwright")
255255
.array("framework")
256256
.array("benchmark")
257257
.string("chromeBinary").argv;
258258

259+
let runner = args.runner;
260+
if ([BENCHMARK_RUNNER.WEBDRIVER_CDP,BENCHMARK_RUNNER.WEBDRIVER,BENCHMARK_RUNNER.PLAYWRIGHT,BENCHMARK_RUNNER.PUPPETEER].includes(runner)) {
261+
console.log(`INFO: Using ${runner} benchmark runner`)
262+
config.BENCHMARK_RUNNER = runner;
263+
} else {
264+
console.log("ERROR: argument driver has illegal value "+runner, [BENCHMARK_RUNNER.WEBDRIVER_CDP,BENCHMARK_RUNNER.WEBDRIVER,BENCHMARK_RUNNER.PLAYWRIGHT,BENCHMARK_RUNNER.PUPPETEER]);
265+
process.exit(1);
266+
}
267+
259268
let allArgs = args._.length <= 2 ? [] : args._.slice(2, args._.length);
260269
let frameworkArgument = !args.framework ? allArgs : args.framework;
261270
console.log("args", args, "allArgs", allArgs);
262271

272+
if (process.env.HOST) {
273+
config.HOST = process.env.HOST;
274+
console.log(`INFO: Using host ${config.HOST} instead of localhost`);
275+
}
276+
263277
async function main() {
264-
let runBenchmarks = (args.benchmark && args.benchmark.length > 0 ? args.benchmark : [""]).map((v: string) => v.toString());
278+
let runBenchmarksArgs: string[] = (args.benchmark && args.benchmark.length > 0) ? args.benchmark : [""];
279+
let runBenchmarks: Array<BenchmarkInfo> = Object.values(benchmarkInfos).filter((b) =>
280+
runBenchmarksArgs.some((name) => b.id.toLowerCase().indexOf(name) > -1)
281+
);
282+
283+
265284
let runFrameworks: FrameworkData[];
266-
267285
let matchesDirectoryArg = (directoryName: string) =>
268286
frameworkArgument.length == 0 || frameworkArgument.some((arg: string) => arg == directoryName);
269287
runFrameworks = await initializeFrameworks(matchesDirectoryArg);
@@ -297,4 +315,4 @@ main()
297315
.catch((error) => {
298316
console.log("run was not completely sucessful", error);
299317
process.exit(1);
300-
});
318+
});

0 commit comments

Comments
 (0)