From fb87a6b94cd63b7c9cd406f1f90136615689b0de Mon Sep 17 00:00:00 2001 From: Jakob Getz Date: Tue, 5 Dec 2023 13:45:49 +0900 Subject: [PATCH] add the no record option --- src/analyser.cts | 70 ++++++++++++++++++++++---------------------- src/cli/options.cts | 6 ++-- src/instrumenter.cts | 2 +- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/analyser.cts b/src/analyser.cts index b8972e27..0b4f1992 100644 --- a/src/analyser.cts +++ b/src/analyser.cts @@ -15,7 +15,7 @@ export type AnalysisResult = { wasm: number[] }[] -type Options = { extended: boolean } +type Options = { extended?: boolean, noRecord?: boolean } export default class Analyser { private analysisPath: string @@ -27,7 +27,7 @@ export default class Analyser { private p_measureUserInteraction: StopMeasure - constructor(analysisPath: string, options = { extended: false }) { + constructor(analysisPath: string, options: Options = { extended: false, noRecord: false }) { this.analysisPath = analysisPath this.options = options } @@ -40,36 +40,15 @@ export default class Analyser { this.isRunning = true this.browser = await chromium.launch({ // chromium version: 119.0.6045.9 (Developer Build) (x86_64); V8 version: V8 11.9.169.3; currently in node I run version 11.8.172.13-node.12 headless, args: [ - '--disable-web-security', + // '--disable-web-security', '--js-flags="--max_old_space_size=8192"' ] }); this.page = await this.browser.newPage(); this.page.setDefaultTimeout(120000); - const initScript = await this.constructInitScript() - - await this.page.addInitScript({ content: initScript }) - - await this.page.route(`**/*.js*`, async route => { - let response - try { - response = await route.fetch() - } catch { - // this usually only happens when the browser is already closed. Only happens when running the test suite - return - } - const script = await response.text() - try { - acorn.parse(script, { ecmaVersion: 'latest' }) - const body = `${initScript}${script}` - await route.fulfill({ response, body: body }) - } catch { - route.fulfill({ response, body: script }) - } - }) - this.page.on('worker', worker => { - this.contexts.push(worker) - }) + if (this.options.noRecord !== true) { + await this.attachRecorder() + } await this.page.goto(url, { timeout: 60000 }) p_measureStart() @@ -99,14 +78,35 @@ export default class Analyser { return traces.map((result, i) => ({ result: result, wasm: originalWasmBuffer[i] })) } - // private trim(traces: string[]) { - // return traces.map(t => { - // while (t.length >= 3 && t.slice(t.length - 3, t.length) !== 'ER;') { - // t = t.slice(0, -1) - // } - // return t - // }) - // } + private async attachRecorder() { + const initScript = await this.constructInitScript() + + await this.page.addInitScript({ content: initScript }) + + await this.page.route(`**/*.js*`, async route => { + let response + try { + response = await route.fetch() + } catch { + // this usually only happens when the browser is already closed. Only happens when running the test suite + return + } + const script = await response.text() + try { + acorn.parse(script, { ecmaVersion: 'latest' }) + const body = `${initScript}${script}` + await route.fulfill({ response, body: body }) + } catch { + route.fulfill({ response, body: script }) + } + // const script = response.text() + // const body = `${initScript}${script}` + // await route.fulfill({ response, body: body }) + }) + this.page.on('worker', worker => { + this.contexts.push(worker) + }) + } private async getResults() { const results = await Promise.all(this.contexts.map(async (c) => { diff --git a/src/cli/options.cts b/src/cli/options.cts index 8dd88bda..29bd6005 100644 --- a/src/cli/options.cts +++ b/src/cli/options.cts @@ -7,7 +7,8 @@ export type Options = { dumpTrace: boolean, benchmarkPath: string, file: string, - extended: boolean + extended: boolean, + noRecord: boolean, } export default function getOptions() { @@ -18,7 +19,8 @@ export default function getOptions() { { name: 'benchmarkPath', alias: 'b', type: String }, { name: 'headless', alias: 'h', type: Boolean }, { name: 'file', alias: 'f', type: String }, - { name: 'extended', alias: 'e', type: Boolean } + { name: 'extended', alias: 'e', type: Boolean }, + { name: 'no-record', alias: 'n', type: Boolean} ] const options: Options & { url: string } = commandLineArgs(optionDefinitions) if (options.headless === undefined) { diff --git a/src/instrumenter.cts b/src/instrumenter.cts index ff5c87ea..0365663f 100644 --- a/src/instrumenter.cts +++ b/src/instrumenter.cts @@ -13,7 +13,7 @@ export default async function run(url: string, options: Options) { code.toWriteStream(fss.createWriteStream(options.file + '.js')) return } - const analyser = new Analyser('./dist/src/tracer.cjs', { extended: options.extended }) + const analyser = new Analyser('./dist/src/tracer.cjs', { extended: options.extended, noRecord: options.noRecord }) await analyser.start(url, { headless: options.headless }) await askQuestion(`Record is running. Enter 'Stop' to stop recording: `) console.log(`Record stopped. Downloading...`)