@@ -52,6 +52,8 @@ export default class Bench extends EventTarget {
5252
5353 throws = false ;
5454
55+ warmup = true ;
56+
5557 warmupTime = defaultMinimumWarmupTime ;
5658
5759 warmupIterations = defaultMinimumWarmupIterations ;
@@ -70,6 +72,7 @@ export default class Bench extends EventTarget {
7072 super ( ) ;
7173 this . name = options . name ;
7274 this . now = options . now ?? this . now ;
75+ this . warmup = options . warmup ?? this . warmup ;
7376 this . warmupTime = options . warmupTime ?? this . warmupTime ;
7477 this . warmupIterations = options . warmupIterations ?? this . warmupIterations ;
7578 this . time = options . time ?? this . time ;
@@ -90,6 +93,25 @@ export default class Bench extends EventTarget {
9093 }
9194 }
9295
96+ /**
97+ * warmup the benchmark tasks.
98+ */
99+ private async warmupTasks ( ) : Promise < void > {
100+ this . dispatchEvent ( createBenchEvent ( 'warmup' ) ) ;
101+ if ( this . concurrency === 'bench' ) {
102+ const limit = pLimit ( this . threshold ) ;
103+ const promises : Promise < void > [ ] = [ ] ;
104+ for ( const task of this . _tasks . values ( ) ) {
105+ promises . push ( limit ( ( ) => task . warmup ( ) ) ) ;
106+ }
107+ await Promise . all ( promises ) ;
108+ } else {
109+ for ( const task of this . _tasks . values ( ) ) {
110+ await task . warmup ( ) ;
111+ }
112+ }
113+ }
114+
93115 private async runTask ( task : Task ) : Promise < Task > {
94116 if ( this . signal ?. aborted ) {
95117 return task ;
@@ -99,9 +121,11 @@ export default class Bench extends EventTarget {
99121
100122 /**
101123 * run the added tasks that were registered using the {@link add} method.
102- * Note: This method does not do any warmup. Call {@link warmup} for that.
103124 */
104125 async run ( ) : Promise < Task [ ] > {
126+ if ( this . warmup ) {
127+ await this . warmupTasks ( ) ;
128+ }
105129 let values : Task [ ] = [ ] ;
106130 this . dispatchEvent ( createBenchEvent ( 'start' ) ) ;
107131 if ( this . concurrency === 'bench' ) {
@@ -120,26 +144,6 @@ export default class Bench extends EventTarget {
120144 return values ;
121145 }
122146
123- /**
124- * warmup the benchmark tasks.
125- * This is not run by default by the {@link run} method.
126- */
127- async warmup ( ) : Promise < void > {
128- this . dispatchEvent ( createBenchEvent ( 'warmup' ) ) ;
129- if ( this . concurrency === 'bench' ) {
130- const limit = pLimit ( this . threshold ) ;
131- const promises : Promise < void > [ ] = [ ] ;
132- for ( const task of this . _tasks . values ( ) ) {
133- promises . push ( limit ( ( ) => task . warmup ( ) ) ) ;
134- }
135- await Promise . all ( promises ) ;
136- } else {
137- for ( const task of this . _tasks . values ( ) ) {
138- await task . warmup ( ) ;
139- }
140- }
141- }
142-
143147 /**
144148 * reset each task and remove its result
145149 */
@@ -196,37 +200,24 @@ export default class Bench extends EventTarget {
196200 ) : ( Record < string , string | number > | undefined | null ) [ ] {
197201 return this . tasks . map ( ( task ) => {
198202 if ( task . result ) {
199- if ( task . result . error ) {
200- throw task . result . error ;
201- }
202- return (
203- // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
204- convert ?.( task ) || {
203+ return task . result . error
204+ ? ( convert ?.( task ) ?? {
205+ 'Task name' : task . name ,
206+ Error : task . result . error . message ,
207+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
208+ Stack : task . result . error . stack ! ,
209+ Samples : task . result . latency . samples . length ,
210+ } )
211+ : ( convert ?.( task ) ?? {
205212 'Task name' : task . name ,
206- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
207- 'Throughput average (ops/s)' : task . result . error
208- ? 'NaN'
209- : `${ task . result . throughput . mean . toFixed ( 0 ) } \xb1 ${ task . result . throughput . rme . toFixed ( 2 ) } %` ,
210- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
211- 'Throughput median (ops/s)' : task . result . error
212- ? 'NaN'
213- : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
214- `${ task . result . throughput . p50 ! . toFixed ( 0 ) } ${ Number . parseInt ( task . result . throughput . mad ! . toFixed ( 0 ) , 10 ) > 0 ? ` \xb1 ${ task . result . throughput . mad ! . toFixed ( 0 ) } ` : '' } ` ,
215- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
216- 'Latency average (ns)' : task . result . error
217- ? 'NaN'
218- : `${ ( task . result . latency . mean * 1e6 ) . toFixed ( 2 ) } \xb1 ${ task . result . latency . rme . toFixed ( 2 ) } %` ,
219- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
220- 'Latency median (ns)' : task . result . error
221- ? 'NaN'
222- : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223- `${ ( task . result . latency . p50 ! * 1e6 ) . toFixed ( 2 ) } ${ Number . parseFloat ( ( task . result . latency . mad ! * 1e6 ) . toFixed ( 2 ) ) > 0 ? ` \xb1 ${ ( task . result . latency . mad ! * 1e6 ) . toFixed ( 2 ) } ` : '' } ` ,
224- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
225- Samples : task . result . error
226- ? 'NaN'
227- : task . result . latency . samples . length ,
228- }
229- ) ;
213+ 'Throughput average (ops/s)' : `${ task . result . throughput . mean . toFixed ( 0 ) } \xb1 ${ task . result . throughput . rme . toFixed ( 2 ) } %` ,
214+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
215+ 'Throughput median (ops/s)' : `${ task . result . throughput . p50 ! . toFixed ( 0 ) } ${ Number . parseInt ( task . result . throughput . mad ! . toFixed ( 0 ) , 10 ) > 0 ? ` \xb1 ${ task . result . throughput . mad ! . toFixed ( 0 ) } ` : '' } ` ,
216+ 'Latency average (ns)' : `${ ( task . result . latency . mean * 1e6 ) . toFixed ( 2 ) } \xb1 ${ task . result . latency . rme . toFixed ( 2 ) } %` ,
217+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
218+ 'Latency median (ns)' : `${ ( task . result . latency . p50 ! * 1e6 ) . toFixed ( 2 ) } ${ Number . parseFloat ( ( task . result . latency . mad ! * 1e6 ) . toFixed ( 2 ) ) > 0 ? ` \xb1 ${ ( task . result . latency . mad ! * 1e6 ) . toFixed ( 2 ) } ` : '' } ` ,
219+ Samples : task . result . latency . samples . length ,
220+ } ) ;
230221 }
231222 return null ;
232223 } ) ;
0 commit comments