|
| 1 | +import { Browser, Page } from "puppeteer-core"; |
| 2 | +import { CPUBenchmarkResult, SizeInfoJSON, sizeBenchmarkInfos, slowDownFactor } from "./benchmarksCommon.js"; |
| 3 | +import { BenchmarkOptions, config as defaultConfig, ErrorAndWarning, FrameworkData, Config } from "./common.js"; |
| 4 | +import { checkElementContainsText, checkElementExists, clickElement, startBrowser } from "./puppeteerAccess.js"; |
| 5 | +import { fileNameTrace } from "./timeline.js"; |
| 6 | +import { BenchmarkSize, SizeBenchmarkResult, benchmarks } from "./benchmarksSize.js"; |
| 7 | + |
| 8 | +let config: Config = defaultConfig; |
| 9 | + |
| 10 | +const wait = (delay = 1000) => new Promise((res) => setTimeout(res, delay)); |
| 11 | + |
| 12 | +function convertError(error: any): string { |
| 13 | + console.log( |
| 14 | + "ERROR in run Benchmark: |", |
| 15 | + error, |
| 16 | + "| type:", |
| 17 | + typeof error, |
| 18 | + "instance of Error", |
| 19 | + error instanceof Error, |
| 20 | + "Message:", |
| 21 | + error.message |
| 22 | + ); |
| 23 | + if (typeof error === "string") { |
| 24 | + console.log("Error is string"); |
| 25 | + return error; |
| 26 | + } else if (error instanceof Error) { |
| 27 | + console.log("Error is instanceof Error"); |
| 28 | + return error.message; |
| 29 | + } else { |
| 30 | + console.log("Error is unknown type"); |
| 31 | + return error.toString(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +async function runSizeBenchmark( |
| 37 | + framework: FrameworkData, |
| 38 | + benchmarks: BenchmarkSize, |
| 39 | + benchmarkOptions: BenchmarkOptions |
| 40 | +): Promise<ErrorAndWarning<SizeBenchmarkResult>> { |
| 41 | + let warnings: string[] = []; |
| 42 | + let results: SizeBenchmarkResult[] = []; |
| 43 | + |
| 44 | + console.log("size benchmarking", framework); |
| 45 | + let browser: Browser = null; |
| 46 | + let page: Page = null; |
| 47 | + try { |
| 48 | + browser = await startBrowser(benchmarkOptions); |
| 49 | + page = await browser.newPage(); |
| 50 | + // if (config.LOG_DETAILS) { |
| 51 | + page.on("console", (msg) => { |
| 52 | + for (let i = 0; i < msg.args().length; ++i) console.log(`BROWSER: ${msg.args()[i]}`); |
| 53 | + }); |
| 54 | + // } |
| 55 | + for (let i = 0; i < benchmarkOptions.batchSize; i++) { |
| 56 | + let enableCompressionResponse = await fetch(`http://${benchmarkOptions.host}:${benchmarkOptions.port}/enableCompression`); |
| 57 | + if (enableCompressionResponse.status !== 200) throw new Error("Could not enable compression"); |
| 58 | + if (await enableCompressionResponse.text() !== "OK") throw new Error("Could not enable compression - OK missing"); |
| 59 | + |
| 60 | + await page.goto(`http://${benchmarkOptions.host}:${benchmarkOptions.port}/${framework.uri}/index.html`, { |
| 61 | + waitUntil: "networkidle0", |
| 62 | + }); |
| 63 | + |
| 64 | + await checkElementExists(page, "pierce/#run"); |
| 65 | + await clickElement(page, "pierce/#run"); |
| 66 | + await checkElementContainsText(page, "pierce/tbody>tr:nth-of-type(1)>td:nth-of-type(1)", (i*1000+1).toFixed()); |
| 67 | + |
| 68 | + let sizeInfoResponse = await fetch(`http://${benchmarkOptions.host}:${benchmarkOptions.port}/sizeInfo`); |
| 69 | + if (sizeInfoResponse.status !== 200) throw new Error("Could not enable compression"); |
| 70 | + let sizeInfo = (await sizeInfoResponse.json()) as SizeInfoJSON; |
| 71 | + console.log("sizeInfo", sizeInfo); |
| 72 | + |
| 73 | + results = benchmarks.subbenchmarks.map((b) => ({ |
| 74 | + benchmark: b, |
| 75 | + result: b.fn(sizeInfo) |
| 76 | + })); |
| 77 | + } |
| 78 | + return { error: undefined, warnings, result: results }; |
| 79 | + } catch (error) { |
| 80 | + console.log("ERROR", error); |
| 81 | + return { error: convertError(error), warnings }; |
| 82 | + } finally { |
| 83 | + let disableCompressionResponse = await fetch(`http://${benchmarkOptions.host}:${benchmarkOptions.port}/disableCompression`); |
| 84 | + if (disableCompressionResponse.status !== 200) console.log("ERROR - Could not disable compression"); |
| 85 | + if (await disableCompressionResponse.text() !== "OK") console.log("ERROR - Could not disable compression - OK missing"); |
| 86 | + try { |
| 87 | + if (browser) { |
| 88 | + console.log("*** browser close"); |
| 89 | + await browser.close(); |
| 90 | + console.log("*** browser closed"); |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + console.log("ERROR cleaning up driver", error); |
| 94 | + } |
| 95 | + console.log("*** browser has been shutting down"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +export async function executeBenchmark( |
| 100 | + framework: FrameworkData, |
| 101 | + benchmarkId: string, |
| 102 | + benchmarkOptions: BenchmarkOptions |
| 103 | +): Promise<ErrorAndWarning<any>> { |
| 104 | + let runBenchmarks: Array<BenchmarkSize> = benchmarks.filter( |
| 105 | + (b) => |
| 106 | + benchmarkId === b.benchmarkInfo.id && (b instanceof BenchmarkSize) |
| 107 | + ) as Array<BenchmarkSize>; |
| 108 | + if (runBenchmarks.length != 1) throw `Benchmark name ${benchmarkId} is not unique (size)`; |
| 109 | + |
| 110 | + let benchmark = runBenchmarks[0]; |
| 111 | + |
| 112 | + let errorAndWarnings: ErrorAndWarning<any>; |
| 113 | + errorAndWarnings = await runSizeBenchmark(framework, benchmark, benchmarkOptions); |
| 114 | + |
| 115 | + if (config.LOG_DEBUG) console.log("benchmark finished - got errors promise", errorAndWarnings); |
| 116 | + return errorAndWarnings; |
| 117 | +} |
| 118 | + |
| 119 | +process.on("message", (msg: any) => { |
| 120 | + config = msg.config; |
| 121 | + console.log("START BENCHMARK. Write results?", config.WRITE_RESULTS); |
| 122 | + |
| 123 | + let { |
| 124 | + framework, |
| 125 | + benchmarkId, |
| 126 | + benchmarkOptions, |
| 127 | + }: { |
| 128 | + framework: FrameworkData; |
| 129 | + benchmarkId: string; |
| 130 | + benchmarkOptions: BenchmarkOptions; |
| 131 | + } = msg; |
| 132 | + executeBenchmark(framework, benchmarkId, benchmarkOptions) |
| 133 | + .then((result) => { |
| 134 | + process.send(result); |
| 135 | + process.exit(0); |
| 136 | + }) |
| 137 | + .catch((error) => { |
| 138 | + console.log("CATCH: Error in forkedBenchmarkRunnerSize"); |
| 139 | + process.send({ failure: convertError(error) }); |
| 140 | + process.exit(0); |
| 141 | + }); |
| 142 | +}); |
0 commit comments