1- import { build as buildHistogram } from 'hdr-histogram-js'
2- import {
3- percentiles ,
4- type Result ,
5- type SetupFunction ,
6- type TestContext ,
7- type TestFunction ,
8- type WorkerContext
9- } from './models.ts'
1+ import { type Result , type SetupFunction , type TestContext , type TestFunction , type WorkerContext } from './models.ts'
2+ import { Tracker } from './tracker.ts'
103
114function noOp ( ) : void {
125 // No-op
@@ -17,30 +10,18 @@ function noSetup(cb: (err?: Error | null) => void): void {
1710}
1811
1912function handleTestIteration ( context : TestContext , error ?: Error | null ) : void {
20- // Grab duration even in case of error to make sure we don't add any overhead to the benchmark
21- const duration = Number ( process . hrtime . bigint ( ) - context . start )
22-
2313 // Handle error
2414 if ( error ) {
25- context . callback ( {
26- success : false ,
27- error,
28- size : 0 ,
29- min : 0 ,
30- max : 0 ,
31- mean : 0 ,
32- stddev : 0 ,
33- percentiles : { } ,
34- standardError : 0
35- } )
15+ context . tracker . error = error
16+ context . callback ( context . tracker . results )
3617 return
3718 }
3819
3920 // Get some parameters
40- const { histogram , total, errorThreshold } = context
21+ const { tracker , total, errorThreshold } = context
4122
4223 // Track results
43- histogram . recordValue ( duration )
24+ tracker . track ( context . start )
4425 context . executed ++
4526
4627 // Check if stop earlier if we are below the error threshold
@@ -52,7 +33,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
5233
5334 // Check if abort the test earlier. It is checked every 5% after 10% of the iterations
5435 if ( completedPercentage >= 1000 && completedPercentage % 500 === 0 ) {
55- const standardErrorPercentage = histogram . stdDeviation / Math . sqrt ( executed ) / histogram . mean
36+ const standardErrorPercentage = tracker . standardError / tracker . histogram . mean
5637
5738 if ( standardErrorPercentage < errorThreshold ) {
5839 stop = true
@@ -62,20 +43,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
6243
6344 // If the test is over
6445 if ( stop || executed > total ) {
65- const stdDev = histogram . stdDeviation
66-
67- context . callback ( {
68- success : true ,
69- size : executed ,
70- min : histogram . minNonZeroValue ,
71- max : histogram . maxValue ,
72- mean : histogram . mean ,
73- stddev : stdDev ,
74- percentiles : Object . fromEntries (
75- percentiles . map ( percentile => [ percentile , histogram . getValueAtPercentile ( percentile ) ] )
76- ) ,
77- standardError : stdDev / Math . sqrt ( executed )
78- } )
46+ context . callback ( tracker . results )
7947 return
8048 }
8149
@@ -204,11 +172,7 @@ export function runWorker(context: WorkerContext, notifier: (value: any) => void
204172 errorThreshold,
205173 total : iterations - 1 ,
206174 executed : 0 ,
207- histogram : buildHistogram ( {
208- lowestDiscernibleValue : 1 ,
209- highestTrackableValue : 1e9 ,
210- numberOfSignificantValueDigits : 5
211- } ) ,
175+ tracker : new Tracker ( ) ,
212176 start : BigInt ( 0 ) ,
213177 handler : noOp ,
214178 notifier,
0 commit comments