File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111 . add ( 'Using import with node: prefix' , function ( ) {
1212 return import ( 'node:fs' ) ;
1313 } )
14+ . add ( 'async test' , async function ( timer ) {
15+ timer . start ( ) ;
16+ let i = 0 ;
17+ while ( i ++ < timer . count ) {
18+ await import ( "node:fs" ) ;
19+ }
20+ timer . end ( timer . count ) ;
21+ } )
1422 . run ( ) ;
Original file line number Diff line number Diff line change 11const { parentPort } = require ( "node:worker_threads" ) ;
22const { runBenchmark } = require ( "./lifecycle" ) ;
3+ const AsyncFunction = Object . getPrototypeOf ( async ( ) => { } ) . constructor ;
34
45// Deserialize the benchmark function
56function deserializeBenchmark ( benchmark ) {
6- benchmark . fn = new Function ( benchmark . fn ) ;
7+ const { isAsync, hasArg } = benchmark ;
8+ const fnPrototype = isAsync ? AsyncFunction : Function ;
9+
10+ if ( hasArg ) {
11+ benchmark . fn = new fnPrototype ( "timer" , benchmark . fn ) ;
12+ } else {
13+ benchmark . fn = new fnPrototype ( benchmark . fn ) ;
14+ }
715}
816
917parentPort . on (
Original file line number Diff line number Diff line change @@ -21,15 +21,27 @@ describe("Using worker_threads", () => {
2121 } )
2222 . add ( "Import without node: prefix" , ( ) => {
2323 return import ( "node:fs" ) ;
24+ } )
25+ . add ( "async test" , async ( ) => {
26+ return import ( "node:fs" ) ;
27+ } )
28+ . add ( "async with timer" , async ( timer ) => {
29+ timer . start ( ) ;
30+ let i = 0 ;
31+ while ( i ++ < timer . count ) {
32+ await import ( "node:fs" ) ;
33+ }
34+ timer . end ( timer . count ) ;
2435 } ) ;
36+
2537 await bench . run ( ) ;
2638 } ) ;
2739
2840 after ( ( ) => {
2941 mock . restoreAll ( ) ;
3042 } ) ;
3143
32- it ( "should create a new Worker 2 times" , ( ) => {
33- assert . strictEqual ( workerThreads . Worker . mock . calls . length , 2 ) ;
44+ it ( "should create a new Worker 4 times" , ( ) => {
45+ assert . strictEqual ( workerThreads . Worker . mock . calls . length , 4 ) ;
3446 } ) ;
3547} ) ;
You can’t perform that action at this time.
0 commit comments