forked from tinylibs/tinypool
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
040927a
commit e2aba05
Showing
6 changed files
with
102 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { bench } from 'vitest' | ||
import { cpus } from 'node:os' | ||
import { Worker } from 'node:worker_threads' | ||
|
||
import Tinypool, { Options } from '../dist/index' | ||
import { fork } from 'node:child_process' | ||
|
||
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: new URL('./fixtures/add.mjs', import.meta.url).href, | ||
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters