@@ -114,12 +114,25 @@ function prepareBenchmarkProjects(
114
114
}
115
115
116
116
async function collectSamples ( modulePath : string ) {
117
+ let numOfConsequentlyRejectedSamples = 0 ;
117
118
const samples = [ ] ;
118
119
119
120
// If time permits, increase sample size to reduce the margin of error.
120
121
const start = Date . now ( ) ;
121
122
while ( samples . length < minSamples || ( Date . now ( ) - start ) / 1e3 < maxTime ) {
122
123
const sample = await sampleModule ( modulePath ) ;
124
+
125
+ if ( sample . involuntaryContextSwitches > 0 ) {
126
+ numOfConsequentlyRejectedSamples ++ ;
127
+ if ( numOfConsequentlyRejectedSamples > 5 ) {
128
+ throw Error (
129
+ 'Can not sample benchmark due to 5 consequent runs beings rejected because of context switching' ,
130
+ ) ;
131
+ }
132
+ continue ;
133
+ }
134
+ numOfConsequentlyRejectedSamples = 0 ;
135
+
123
136
assert ( sample . clocked > 0 ) ;
124
137
assert ( sample . memUsed > 0 ) ;
125
138
samples . push ( sample ) ;
@@ -356,6 +369,7 @@ interface BenchmarkSample {
356
369
name : string ;
357
370
clocked : number ;
358
371
memUsed : number ;
372
+ involuntaryContextSwitches : number ;
359
373
}
360
374
361
375
function sampleModule ( modulePath : string ) : Promise < BenchmarkSample > {
@@ -377,16 +391,20 @@ function sampleModule(modulePath: string): Promise<BenchmarkSample> {
377
391
378
392
const memBaseline = process.memoryUsage().heapUsed;
379
393
394
+ const resourcesStart = process.resourceUsage();
380
395
const startTime = process.hrtime.bigint();
381
396
for (let i = 0; i < benchmark.count; ++i) {
382
397
benchmark.measure();
383
398
}
384
399
const timeDiff = Number(process.hrtime.bigint() - startTime);
400
+ const resourcesEnd = process.resourceUsage();
385
401
386
402
process.send({
387
403
name: benchmark.name,
388
404
clocked: timeDiff / benchmark.count,
389
405
memUsed: (process.memoryUsage().heapUsed - memBaseline) / benchmark.count,
406
+ involuntaryContextSwitches:
407
+ resourcesEnd.involuntaryContextSwitches - resourcesStart.involuntaryContextSwitches,
390
408
});
391
409
` ;
392
410
0 commit comments