From a1fc19e47cf32c0a7c5f64c566c9325eaffb53e5 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 15 Aug 2024 16:51:06 -0500 Subject: [PATCH] chore(bench): Setup benchmark script for testing browser open timing (#917) --- .gitignore | 1 + scripts/benchmarks/browser-startup.patch | 26 +++++++++++++++ scripts/benchmarks/browser-startup.sh | 41 ++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 scripts/benchmarks/browser-startup.patch create mode 100755 scripts/benchmarks/browser-startup.sh diff --git a/.gitignore b/.gitignore index 60fe67a3..f8387094 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ docs/api/reference stats.html .tool-versions .cache +*-stats.txt diff --git a/scripts/benchmarks/browser-startup.patch b/scripts/benchmarks/browser-startup.patch new file mode 100644 index 00000000..cb6ae14b --- /dev/null +++ b/scripts/benchmarks/browser-startup.patch @@ -0,0 +1,26 @@ +diff --git a/packages/wxt/src/core/runners/web-ext.ts b/packages/wxt/src/core/runners/web-ext.ts +index 09819c3..a0f0df2 100644 +--- a/packages/wxt/src/core/runners/web-ext.ts ++++ b/packages/wxt/src/core/runners/web-ext.ts +@@ -3,6 +3,8 @@ import { ExtensionRunner } from '../../types'; + import { formatDuration } from '../utils/time'; + import defu from 'defu'; + import { wxt } from '../wxt'; ++import fs from 'node:fs'; ++import stream from 'node:stream/promises'; + + /** + * Create an `ExtensionRunner` backed by `web-ext`. +@@ -78,6 +80,12 @@ export function createWebExtRunner(): ExtensionRunner { + + const duration = Date.now() - startTime; + wxt.logger.success(`Opened browser in ${formatDuration(duration)}`); ++ await runner.exit(); ++ const s = fs.createWriteStream('stats.txt', { flags: 'a' }); ++ s.write(duration + ' '); ++ s.end(); ++ await stream.finished(s); ++ process.exit(0); + }, + + async closeBrowser() { diff --git a/scripts/benchmarks/browser-startup.sh b/scripts/benchmarks/browser-startup.sh new file mode 100755 index 00000000..ee5b2e11 --- /dev/null +++ b/scripts/benchmarks/browser-startup.sh @@ -0,0 +1,41 @@ +#!/bin/bash +set -e + +# +# Benchmark how long it takes for the browser to open in dev mode. +# +# To run: +# cd +# ./scripts/benchmarks/browser-startup.sh +# +# You can set N below to change the number of samples per git ref. +# + +N=20 + +function benchmark_ref() { + # Prep + git checkout $1 + pnpm buildc clean + git apply scripts/benchmarks/browser-startup.patch + pnpm i --ignore-scripts + pnpm -r --filter wxt build + echo -n "$1 " >> stats.txt + + # Run benchmark + for i in $(seq $N); do + pnpm wxt packages/wxt-demo + done + git checkout HEAD -- packages/wxt/src/core/runners/web-ext.ts pnpm-lock.yaml + echo "" >> stats.txt +} + +rm -f stats.txt + +benchmark_ref "HEAD" + +# Benchmark a commit: +# benchmark_ref "3109bba" +# +# Benchmark a version: +# benchmark_ref "v0.19.0"