1
1
import type {
2
- Fn , TaskEvents , TaskResult , TaskEventsMap , FnOptions ,
2
+ Fn ,
3
+ TaskEvents ,
4
+ TaskResult ,
5
+ TaskEventsMap ,
6
+ FnOptions ,
3
7
} from '../types/index' ;
4
8
import Bench from './bench' ;
5
9
import tTable from './constants' ;
@@ -61,35 +65,33 @@ export default class Task extends EventTarget {
61
65
await this . opts . beforeAll . call ( this ) ;
62
66
}
63
67
64
- while (
65
- ( totalTime < this . bench . time || this . runs < this . bench . iterations )
66
- && ! this . bench . signal ?. aborted
67
- ) {
68
- if ( this . opts . beforeEach != null ) {
69
- await this . opts . beforeEach . call ( this ) ;
70
- }
71
-
72
- let taskStart = 0 ;
68
+ try {
69
+ while (
70
+ ( totalTime < this . bench . time || this . runs < this . bench . iterations )
71
+ && ! this . bench . signal ?. aborted
72
+ ) {
73
+ if ( this . opts . beforeEach != null ) {
74
+ await this . opts . beforeEach . call ( this ) ;
75
+ }
73
76
74
- try {
75
- taskStart = this . bench . now ( ) ;
77
+ const taskStart = this . bench . now ( ) ;
76
78
if ( isAsync ) {
77
79
await this . fn ( ) ;
78
80
} else {
79
81
this . fn ( ) ;
80
82
}
81
- } catch ( e ) {
82
- this . setResult ( { error : e } ) ;
83
- }
83
+ const taskTime = this . bench . now ( ) - taskStart ;
84
84
85
- const taskTime = this . bench . now ( ) - taskStart ;
86
- this . runs += 1 ;
87
- samples . push ( taskTime ) ;
88
- totalTime += taskTime ;
85
+ samples . push ( taskTime ) ;
86
+ this . runs += 1 ;
87
+ totalTime += taskTime ;
89
88
90
- if ( this . opts . afterEach != null ) {
91
- await this . opts . afterEach . call ( this ) ;
89
+ if ( this . opts . afterEach != null ) {
90
+ await this . opts . afterEach . call ( this ) ;
91
+ }
92
92
}
93
+ } catch ( e ) {
94
+ this . setResult ( { error : e } ) ;
93
95
}
94
96
95
97
if ( this . opts . afterAll != null ) {
@@ -100,7 +102,7 @@ export default class Task extends EventTarget {
100
102
101
103
samples . sort ( ( a , b ) => a - b ) ;
102
104
103
- {
105
+ if ( ! this . result ?. error ) {
104
106
const min = samples [ 0 ] ! ;
105
107
const max = samples [ samples . length - 1 ] ! ;
106
108
const period = totalTime / this . runs ;
@@ -169,24 +171,46 @@ export default class Task extends EventTarget {
169
171
*/
170
172
async warmup ( ) {
171
173
this . dispatchEvent ( createBenchEvent ( 'warmup' , this ) ) ;
174
+ const isAsync = isAsyncFunction ( this . fn ) ;
172
175
const startTime = this . bench . now ( ) ;
173
176
let totalTime = 0 ;
174
177
175
178
await this . bench . setup ( this , 'warmup' ) ;
179
+
180
+ if ( this . opts . beforeAll != null ) {
181
+ await this . opts . beforeAll . call ( this ) ;
182
+ }
183
+
176
184
while (
177
185
( totalTime < this . bench . warmupTime
178
186
|| this . runs < this . bench . warmupIterations )
179
187
&& ! this . bench . signal ?. aborted
180
188
) {
189
+ if ( this . opts . beforeEach != null ) {
190
+ await this . opts . beforeEach . call ( this ) ;
191
+ }
192
+
181
193
try {
182
194
// eslint-disable-next-line no-await-in-loop
183
- await Promise . resolve ( ) . then ( this . fn ) ;
195
+ if ( isAsync ) {
196
+ await this . fn ( ) ;
197
+ } else {
198
+ this . fn ( ) ;
199
+ }
184
200
} catch {
185
201
// todo
186
202
}
187
203
188
204
this . runs += 1 ;
189
205
totalTime = this . bench . now ( ) - startTime ;
206
+
207
+ if ( this . opts . afterEach != null ) {
208
+ await this . opts . afterEach . call ( this ) ;
209
+ }
210
+ }
211
+
212
+ if ( this . opts . afterAll != null ) {
213
+ await this . opts . afterAll . call ( this ) ;
190
214
}
191
215
this . bench . teardown ( this , 'warmup' ) ;
192
216
0 commit comments