Skip to content

Commit 635f904

Browse files
committed
feat: Allow main module to export a function.
1 parent eac15b7 commit 635f904

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Inside worker threads, the cronometro function invocation will return no value a
3939

4040
You can use `isMainThread` from Worker Threads API to check in which environment the script is running.
4141

42+
If your main module returns a function, cronometro will execute it before running tests. The function can also return a promise and that will be awaited.
43+
4244
## Usage
4345

4446
To run a benchmark, simply call the `cronometro` function with the set of tests you want to run, then optionally provide a options object and a Node's style callback.

src/runner.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,23 @@ if (workerData.path.endsWith('.ts')) {
2222
})
2323
}
2424
}
25-
/* c8 ignore stop */
2625

2726
// Require the script to set tests
2827
chain
2928
.then(() => {
3029
return import(workerData.path)
3130
})
31+
.then((module: any) => {
32+
if (typeof module === 'function') {
33+
return module()
34+
} else if (typeof module.default === 'function') {
35+
return module.default()
36+
}
37+
})
3238
.then(() => {
3339
// Run the worker
3440
runWorker(workerData, (value: any) => parentPort!.postMessage(value), process.exit)
3541
})
36-
/* c8 ignore start */
3742
.catch((e: Error) => {
3843
process.nextTick(() => {
3944
throw e

test/asyncImport.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable @typescript-eslint/no-floating-promises */
2+
3+
import t from 'tap'
4+
import { isMainThread } from 'worker_threads'
5+
import { cronometro, percentiles } from '../src'
6+
7+
type Test = typeof t
8+
9+
async function main(): Promise<void> {
10+
await new Promise((resolve: (value: unknown) => void) => setTimeout(resolve, 100))
11+
12+
if (!isMainThread) {
13+
cronometro(
14+
{
15+
single() {
16+
Buffer.alloc(10)
17+
},
18+
multiple() {
19+
Buffer.alloc(10)
20+
Buffer.alloc(20)
21+
}
22+
},
23+
() => false
24+
)
25+
} else {
26+
t.only('Collecting results', async (t: Test) => {
27+
const results = await cronometro(
28+
{
29+
single() {
30+
Buffer.alloc(10)
31+
},
32+
multiple() {
33+
Buffer.alloc(10)
34+
Buffer.alloc(20)
35+
}
36+
},
37+
{ iterations: 10, print: false }
38+
)
39+
40+
t.strictSame(Object.keys(results), ['single', 'multiple'])
41+
42+
for (const entry of Object.values(results)) {
43+
t.ok(entry.success)
44+
t.type(entry.error, 'undefined')
45+
t.equal(entry.size, 10)
46+
t.type(entry.min, 'number')
47+
t.type(entry.max, 'number')
48+
t.type(entry.mean, 'number')
49+
t.type(entry.stddev, 'number')
50+
t.type(entry.standardError, 'number')
51+
52+
for (const percentile of percentiles) {
53+
t.type(entry.percentiles[percentile.toString()], 'number')
54+
}
55+
}
56+
})
57+
}
58+
}
59+
60+
main()
61+
62+
export default main

0 commit comments

Comments
 (0)