Skip to content

Commit

Permalink
wait for script to finish
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Feb 3, 2024
1 parent e2a1d2d commit 4bc00e4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 12 deletions.
37 changes: 37 additions & 0 deletions examples/tinybench.js
Original file line number Diff line number Diff line change
@@ -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' │
// └─────────┴───────────────────────┘
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"vitest": "^1.2.2"
},
"dependencies": {
"tinybench": "^2.6.0",
"vite": "^5.0.12",
"webdriverio": "^8.29.7"
}
Expand Down
2 changes: 1 addition & 1 deletion src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="vite/client" />

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[]) => {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export async function run (env: ExecutionEnvironment, args: RunnerArgs) {
})

browser.url(env.url)
await env.connectPromise
await new Promise<void>((resolve) => {
env.server.ws.on('bx:event', (message) => {
if (message.name === 'doneEvent') {
resolve()
}
})
})
await browser.deleteSession()

if (error) {
Expand Down
13 changes: 11 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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) => {
/**
Expand All @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface RunnerArgs {
browserName: string
browserVersion?: string
headless: boolean
rootDir: string
}

export interface ExecutionEnvironment {
Expand Down

0 comments on commit 4bc00e4

Please sign in to comment.