From 4bc00e490487ea8ae5d539e566ae920899ebd69c Mon Sep 17 00:00:00 2001 From: Christian Bromann Date: Fri, 2 Feb 2024 23:24:59 -0800 Subject: [PATCH] wait for script to finish --- examples/tinybench.js | 37 +++++++++++++++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 1 + src/browser/index.ts | 2 +- src/constants.ts | 1 + src/index.ts | 11 +++++------ src/runner.ts | 8 +++++++- src/server.ts | 13 +++++++++++-- src/types.ts | 1 + 9 files changed, 66 insertions(+), 12 deletions(-) create mode 100644 examples/tinybench.js diff --git a/examples/tinybench.js b/examples/tinybench.js new file mode 100644 index 0000000..781ce14 --- /dev/null +++ b/examples/tinybench.js @@ -0,0 +1,37 @@ +import { Bench } from 'tinybench' + +const bench = new Bench({ time: 100 }) + +bench + .add('faster task', () => { + console.log('I am faster') + }) + .add('slower task', async () => { + await new Promise(r => setTimeout(r, 1)) // we wait 1ms :) + console.log('I am slower') + }) + .todo('unimplemented bench') + +await bench.warmup() // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50 +await bench.run() + +console.table(bench.table()) + +// Output: +// ┌─────────┬───────────────┬──────────┬────────────────────┬───────────┬─────────┐ +// │ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │ +// ├─────────┼───────────────┼──────────┼────────────────────┼───────────┼─────────┤ +// │ 0 │ 'faster task' │ '41,621' │ 24025.791819761525 │ '±20.50%' │ 4257 │ +// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │ +// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘ + +console.table( + bench.table((task) => ({ 'Task name': task.name })) +) + +// Output: +// ┌─────────┬───────────────────────┐ +// │ (index) │ Task name │ +// ├─────────┼───────────────────────┤ +// │ 0 │ 'unimplemented bench' │ +// └─────────┴───────────────────────┘ diff --git a/package-lock.json b/package-lock.json index 578b858..0ebd00e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "license": "MIT", "dependencies": { + "tinybench": "^2.6.0", "vite": "^5.0.12", "webdriverio": "^8.29.7" }, @@ -3489,8 +3490,7 @@ "node_modules/tinybench": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.6.0.tgz", - "integrity": "sha512-N8hW3PG/3aOoZAN5V/NSAEDz0ZixDSSt5b/a05iqtpgfLWMSVuCo7w0k2vVvEjdrIoeGqZzweX2WlyioNIHchA==", - "dev": true + "integrity": "sha512-N8hW3PG/3aOoZAN5V/NSAEDz0ZixDSSt5b/a05iqtpgfLWMSVuCo7w0k2vVvEjdrIoeGqZzweX2WlyioNIHchA==" }, "node_modules/tinypool": { "version": "0.8.2", diff --git a/package.json b/package.json index 07f844b..f33797e 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "vitest": "^1.2.2" }, "dependencies": { + "tinybench": "^2.6.0", "vite": "^5.0.12", "webdriverio": "^8.29.7" } diff --git a/src/browser/index.ts b/src/browser/index.ts index 6ad74e1..0e0b705 100644 --- a/src/browser/index.ts +++ b/src/browser/index.ts @@ -1,6 +1,6 @@ /// -const CONSOLE_METHODS = ['log', 'info', 'warn', 'error', 'debug'] as const +const CONSOLE_METHODS = ['log', 'info', 'warn', 'error', 'debug', 'table'] as const for (const method of CONSOLE_METHODS) { const origCommand = console[method].bind(console) console[method] = (...args: unknown[]) => { diff --git a/src/constants.ts b/src/constants.ts index b7da2d1..de73749 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -4,6 +4,7 @@ export const CLI_OPTIONS = { browserName: { type: 'string', alias: 'b', default: 'chrome' }, browserVersion: { type: 'string', alias: 'v' }, headless: { type: 'boolean', alias: 'h', default: IS_CI }, + rootDir: { type: 'string', alias: 'r', default: process.cwd() } } as const export const PARSE_OPTIONS = { diff --git a/src/index.ts b/src/index.ts index 628e123..883399a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { ViteServer } from './server.js' import { run } from './runner.js' import { parseFileName } from './utils.js' import { CLI_OPTIONS, PARSE_OPTIONS } from './constants.js' +import type { RunnerArgs } from './types.js' export default async function cli () { const { values, tokens, positionals } = parseArgs(PARSE_OPTIONS) @@ -20,15 +21,13 @@ export default async function cli () { } }) + const args = values as RunnerArgs const filename = parseFileName(positionals[0]) - const server = new ViteServer({}) + const server = new ViteServer({ root: args.rootDir }) + try { const env = await server.start(filename) - await run(env, { - browserName: values.browserName!, - browserVersion: values.browserVersion, - headless: values.headless! - }) + await run(env, args) } catch (err) { console.error('Error:', (err as Error).message) process.exit(1) diff --git a/src/runner.ts b/src/runner.ts index 85d97b2..e6c4c36 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -21,7 +21,13 @@ export async function run (env: ExecutionEnvironment, args: RunnerArgs) { }) browser.url(env.url) - await env.connectPromise + await new Promise((resolve) => { + env.server.ws.on('bx:event', (message) => { + if (message.name === 'doneEvent') { + resolve() + } + }) + }) await browser.deleteSession() if (error) { diff --git a/src/server.ts b/src/server.ts index 9009a65..91cacc8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2,7 +2,7 @@ import fs from 'node:fs/promises' import path from 'node:path' import { createServer, type InlineConfig, type ViteDevServer, type Plugin } from 'vite' -import type { ExecutionEnvironment, ConsoleEvent, ErrorEvent } from './types.js' +import type { ExecutionEnvironment, ConsoleEvent } from './types.js' const __dirname = path.dirname(new URL(import.meta.url).pathname) @@ -51,6 +51,15 @@ function instrument (filename: string, onConnect: (value: ViteDevServer) => void return { name: 'instrument', + enforce: 'post', + transform: (code, id) => { + if (id === filename) { + return { + code: `${code}\nimport.meta.hot?.send('bx:event', { name: 'doneEvent' })` + } + } + return null + }, configureServer (server) { server.middlewares.use(async (req, res, next) => { /** @@ -73,7 +82,7 @@ function instrument (filename: string, onConnect: (value: ViteDevServer) => void }) server.ws.on('connection', onConnect) - server.ws.on('bx:event', (message: ConsoleEvent | ErrorEvent) => { + server.ws.on('bx:event', (message: ConsoleEvent) => { if (message.name === 'consoleEvent') { return handleConsole(message) } diff --git a/src/types.ts b/src/types.ts index 83a32c3..3221f93 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,6 +4,7 @@ export interface RunnerArgs { browserName: string browserVersion?: string headless: boolean + rootDir: string } export interface ExecutionEnvironment {