Skip to content

Commit

Permalink
test: convert benchmarks to vitest (tinylibs#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored May 9, 2024
1 parent 040927a commit d1ccdf4
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 93 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on: [workflow_dispatch]

name: Benchmark

jobs:
test:
name: Test
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x, 20.x]

runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- uses: pnpm/action-setup@v2

- name: Install Dependencies
run: pnpm install

- name: Build
run: pnpm build

- name: Benchmark
run: pnpm bench
5 changes: 5 additions & 0 deletions benchmark/fixtures/add-process.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import add from './add.mjs'

process.on('message', (message) => {
process.send(add(message))
})
File renamed without changes.
91 changes: 91 additions & 0 deletions benchmark/isolate-benchmark.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { bench } from 'vitest'
import { cpus } from 'node:os'
import { Worker } from 'node:worker_threads'
import { fork } from 'node:child_process'
import Tinypool, { Options } from '../dist/index'

const THREADS = cpus().length - 1
const ROUNDS = THREADS * 10
const ITERATIONS = 100

for (const runtime of [
'worker_threads',
'child_process',
] as Options['runtime'][]) {
bench(
`Tinypool { runtime: '${runtime}' }`,
async () => {
const pool = new Tinypool({
runtime,
filename: './benchmark/fixtures/add.mjs',
isolateWorkers: true,
minThreads: THREADS,
maxThreads: THREADS,
})

await Promise.all(
Array(ROUNDS)
.fill(0)
.map(() => pool.run({ a: 1, b: 2 }))
)

await pool.destroy()
},
{ iterations: ITERATIONS }
)
}

for (const { task, name } of [
{ name: 'worker_threads', task: workerThreadTask },
{ name: 'child_process', task: childProcessTask },
] as const) {
bench(
`node:${name}`,
async () => {
const pool = Array(ROUNDS).fill(task)

await Promise.all(
Array(THREADS)
.fill(execute)
.map((_task) => _task())
)

async function execute() {
const _task = pool.shift()

if (_task) {
await _task()
return execute()
}
}
},
{ iterations: ITERATIONS }
)
}

async function workerThreadTask() {
const worker = new Worker('./benchmark/fixtures/add-worker.mjs')
const onMessage = new Promise<void>((resolve, reject) =>
worker.on('message', (sum) => (sum === 3 ? resolve() : reject('Not 3')))
)

worker.postMessage({ a: 1, b: 2 })
await onMessage

await worker.terminate()
}

async function childProcessTask() {
const subprocess = fork('./benchmark/fixtures/add-process.mjs')

const onExit = new Promise((resolve) => subprocess.on('exit', resolve))
const onMessage = new Promise<void>((resolve, reject) =>
subprocess.on('message', (sum) => (sum === 3 ? resolve() : reject('Not 3')))
)

subprocess.send({ a: 1, b: 2 })
await onMessage

subprocess.kill()
await onExit
}
65 changes: 0 additions & 65 deletions benchmark/isolate-benchmark.mjs

This file was deleted.

28 changes: 0 additions & 28 deletions benchmark/simple-benchmark.mjs

This file was deleted.

21 changes: 21 additions & 0 deletions benchmark/simple.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bench } from 'vitest'
import Tinypool from '../dist/index'

bench(
'simple',
async () => {
const pool = new Tinypool({
filename: './benchmark/fixtures/add.mjs',
})

const tasks: Promise<void>[] = []

while (pool.queueSize === 0) {
tasks.push(pool.run({ a: 4, b: 6 }))
}

await Promise.all(tasks)
await pool.destroy()
},
{ time: 10_000 }
)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"scripts": {
"test": "vitest",
"bench": "vitest bench",
"dev": "tsup --watch",
"build": "tsup",
"publish": "clean-publish",
Expand Down
4 changes: 4 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ export default defineConfig({

// simple.test.ts expects to be run in main thread
poolMatchGlobs: [['**/simple.test.ts', 'forks']],

benchmark: {
include: ['**/**.bench.ts'],
},
},
})

0 comments on commit d1ccdf4

Please sign in to comment.