Skip to content

Commit

Permalink
add alternativeDownload option and some refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
doehyunbaek committed Aug 20, 2024
1 parent 0e5262d commit c95c5ba
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 98 deletions.
75 changes: 24 additions & 51 deletions src/start.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,44 @@
import readline from 'readline'
import commandLineArgs from 'command-line-args'
import Benchmark, { Analyser, commonOptions } from './web.ts'
import fs from 'fs';

const startOptions = [
{ name: 'url', type: String, defaultOption: true },
{ name: 'benchmarkPath', alias: 'b', type: String },
]

async function main() {
const options = getOptions()
const options = commandLineArgs([...commonOptions, ...startOptions])
await run(options.url, options)
}

export default async function run(url: string, options: Options) {
export default async function run(url: string, options) {
let analyser = new Analyser('./dist/src/tracer.cjs', options)
await analyser.start(url, { headless: options.headless })
await analyser.start(url, options)
await askQuestion(`Record is running. Enter 'Stop' to stop recording: `)
console.log(`Record stopped. Downloading...`)
const results = await analyser.stop()
console.log('Download done. Generating Benchmark...')
Benchmark.fromAnalysisResult(results).save(options.benchmarkPath, { trace: options.dumpTrace, rustBackend: options.rustBackend })
const benchmarkPath = getBenchmarkPath(options);
Benchmark.fromAnalysisResult(results).save(benchmarkPath, { trace: options.dumpTrace, rustBackend: options.rustBackend })
}

import commandLineArgs from 'command-line-args'
import fs from 'fs'
import Benchmark, { Analyser } from './web.ts'

export type Options = {
headless: boolean,
dumpPerformance: boolean,
dumpTrace: boolean,
benchmarkPath: string,
file: string,
extended: boolean,
noRecord: boolean,
rustBackend: boolean,
customInstrumentation: boolean,
}

export function getOptions() {
const optionDefinitions = [
{ name: 'performance', alias: 'p', type: Boolean },
{ name: 'trace', alias: 't', type: Boolean },
{ name: 'url', type: String, defaultOption: true },
{ 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: 'no-record', alias: 'n', type: Boolean },
{ name: 'rustBackend', alias: 'r', type: Boolean },
{ name: 'customInstrumentation', alias: 'c', type: Boolean }
]
const options: Options & { url: string } = commandLineArgs(optionDefinitions)
if (options.headless === undefined) {
options.headless = false
}
if (options.rustBackend) {
console.log('CAUTION: Using experimental Rust backend')
}
if (fs.existsSync(options.benchmarkPath)) {
throw new Error(`EEXIST: Directory at path ${options.benchmarkPath} does already exist`)
function getBenchmarkPath(options) {
if (options.benchmarkPath !== undefined) {
return options.benchmarkPath;
}
if (options.benchmarkPath === undefined || options.benchmarkPath === null) {
let i = 0
options.benchmarkPath = `benchmark`
while (fs.existsSync(options.benchmarkPath)) {
i++;
options.benchmarkPath = `benchmark_${i}`
}
let benchmarkPath = 'benchmark_0';
let i = 0;
while (fs.existsSync(benchmarkPath)) {
i++;
benchmarkPath = `benchmark_${i}`;
}
return options
return benchmarkPath;
}

export async function askQuestion(question: string) {
async function askQuestion(question: string) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
Expand All @@ -78,4 +51,4 @@ export async function askQuestion(question: string) {
});
}

main()
main()
34 changes: 12 additions & 22 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Server } from "http";
import commandLineArgs from "command-line-args";
import { execSync } from "child_process";
import { filter } from "./filter.ts";
import Benchmark, { Analyser } from "./web.ts";
import Benchmark, { Analyser, commonOptions } from "./web.ts";
import runSliceDiceTests from "./test-slice-dice.ts";
import { exit } from "process";

Expand Down Expand Up @@ -79,15 +79,14 @@ async function analyzeAndSaveBenchmark(options: any, testJsPath: string, website
}
}

const testOptions = [
{ name: "category", type: String, defaultOption: true },
{ name: "testcases", alias: "t", type: String, multiple: true },
{ name: "fidxs", alias: "i", type: Number, multiple: true },
];

(async function run() {
const optionDefinitions = [
{ name: "category", type: String, defaultOption: true },
{ name: "testcases", alias: "t", type: String, multiple: true },
{ name: "fidxs", alias: "i", type: Number, multiple: true },
{ name: "firefoxFrontend", alias: "f", type: Boolean },
{ name: "webkitFrontend", alias: "w", type: Boolean },
];
const options = commandLineArgs(optionDefinitions);
const options = commandLineArgs([...commonOptions, ...testOptions]);
if (options.category === undefined || options.category === "core" || options.category === "proxy" || options.category === "online") {
let testNames = await getDirectoryNames(
path.join(process.cwd(), "tests", options.category)
Expand All @@ -106,7 +105,7 @@ async function analyzeAndSaveBenchmark(options: any, testJsPath: string, website
exit(0);
}
}
if (options.category === ("slicedice")) {
if (options.category === "slicedice") {
let testNames = await getDirectoryNames(
path.join(process.cwd(), "benchmarks")
);
Expand Down Expand Up @@ -161,23 +160,14 @@ export async function getDirectoryNames(folderPath: string) {
const entries = await fs.readdir(folderPath, { withFileTypes: true });

const directories: string[] = entries
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name);

return directories;
}

export async function delay(ms: number) {
return new Promise(resolve => {
setTimeout(resolve, ms);
setTimeout(resolve, ms);
});
}

export function trimFromLastOccurance(str: string, substring: string) {
const lastIndex = str.lastIndexOf(substring);
if (lastIndex === -1) {
// Substring not found, return original string or handle as needed
return str;
}
return str.substring(0, lastIndex + substring.length);
}
52 changes: 28 additions & 24 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import fs from 'fs/promises'
import fss from 'fs'
import path from 'path'
import { execSync } from 'child_process';
import { firefox, webkit, chromium } from 'playwright'
import type { Browser, Frame, Worker, Page } from 'playwright'

export const commonOptions = [
{ name: "firefoxFrontend", alias: "f", type: Boolean },
{ name: "webkitFrontend", alias: "w", type: Boolean },
{ name: "alternativeDownload", type: Boolean, defaultValue: false },
]

// read port from env
const CDP_PORT = process.env.CDP_PORT || 8080
Expand All @@ -8,24 +19,23 @@ export type AnalysisResult = {
wasm: number[]
}[]

type Options = { extended?: boolean, noRecord?: boolean, evalRecord?: boolean, firefox?: boolean, webkit?: boolean }
export class Analyser {

private analysisPath: string
private options: Options
private options
private browser: Browser
private page: Page
private downloadPaths: string[]
private contexts: (Frame | Worker)[] = []
private isRunning = false


constructor(analysisPath: string, options: Options = { extended: false, noRecord: false }) {
constructor(analysisPath: string, options) {
this.analysisPath = analysisPath
this.options = options
}

async start(url, { headless } = { headless: false }) {
async start(url, options) {
if (this.isRunning === true) {
throw new Error('Analyser is already running. Stop the Analyser before starting again')
}
Expand All @@ -35,7 +45,7 @@ export class Analyser {
this.browser = await chromium.connectOverCDP(`http://localhost:${CDP_PORT}`);
} else {
this.browser = await browserType.launch({
headless, args: [
headless: options.headless, args: [
// '--disable-web-security',
'--js-flags="--max_old_space_size=8192"',
'--enable-experimental-web-platform-features',
Expand All @@ -53,7 +63,7 @@ export class Analyser {
});
this.downloadPaths = [];
this.page.on('download', async (download) => {
const suggestedFilename = './out' + download.suggestedFilename();
const suggestedFilename = './out/' + download.suggestedFilename();
await download.saveAs(suggestedFilename);
this.downloadPaths.push(suggestedFilename);
});
Expand All @@ -73,11 +83,10 @@ export class Analyser {
this.contexts = this.contexts.concat(this.page.frames())
const traces = (await this.getResults()).map(t => trimFromLastOccurance(t, 'ER'))
let originalWasmBuffer;
// make this configurable with options
if (true) {
originalWasmBuffer = await this.getBuffers()
} else {
if (this.options.alternativeDownload) {
originalWasmBuffer = await this.getBuffersThroughDownloads()
} else {
originalWasmBuffer = await this.getBuffers()
}
this.contexts = []
this.browser.close()
Expand Down Expand Up @@ -174,27 +183,22 @@ export class Analyser {
const setupScript = await fs.readFile('./third_party/wasabi/crates/wasabi/js/r3.js') + '\n'
return wasabiScript + ';' + setupScript + ';'
}

setExtended(extended: boolean) {
this.options.extended = extended
}

getExtended() {
return this.options.extended
}
}

import fss from 'fs'
import path from 'path'
import { execSync } from 'child_process';
import { trimFromLastOccurance } from './test.ts'
import { firefox, webkit, chromium } from 'playwright'
import type { Browser, Frame, Worker, Page} from 'playwright'

export type Record = { binary: number[], trace: string }[]

type WasabiRuntime = string[]

export function trimFromLastOccurance(str: string, substring: string) {
const lastIndex = str.lastIndexOf(substring);
if (lastIndex === -1) {
// Substring not found, return original string or handle as needed
return str;
}
return str.substring(0, lastIndex + substring.length);
}



export default class Benchmark {
Expand Down
2 changes: 1 addition & 1 deletion third_party/wasabi
Submodule wasabi updated 1 files
+1 −1 crates/wasabi/js/r3.js

0 comments on commit c95c5ba

Please sign in to comment.