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'
10
3
11
4
function noOp ( ) : void {
12
5
// No-op
@@ -17,30 +10,18 @@ function noSetup(cb: (err?: Error | null) => void): void {
17
10
}
18
11
19
12
function 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
-
23
13
// Handle error
24
14
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 )
36
17
return
37
18
}
38
19
39
20
// Get some parameters
40
- const { histogram , total, errorThreshold } = context
21
+ const { tracker , total, errorThreshold } = context
41
22
42
23
// Track results
43
- histogram . recordValue ( duration )
24
+ tracker . track ( context . start )
44
25
context . executed ++
45
26
46
27
// Check if stop earlier if we are below the error threshold
@@ -52,7 +33,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
52
33
53
34
// Check if abort the test earlier. It is checked every 5% after 10% of the iterations
54
35
if ( completedPercentage >= 1000 && completedPercentage % 500 === 0 ) {
55
- const standardErrorPercentage = histogram . stdDeviation / Math . sqrt ( executed ) / histogram . mean
36
+ const standardErrorPercentage = tracker . standardError / tracker . histogram . mean
56
37
57
38
if ( standardErrorPercentage < errorThreshold ) {
58
39
stop = true
@@ -62,20 +43,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
62
43
63
44
// If the test is over
64
45
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 )
79
47
return
80
48
}
81
49
@@ -204,11 +172,7 @@ export function runWorker(context: WorkerContext, notifier: (value: any) => void
204
172
errorThreshold,
205
173
total : iterations - 1 ,
206
174
executed : 0 ,
207
- histogram : buildHistogram ( {
208
- lowestDiscernibleValue : 1 ,
209
- highestTrackableValue : 1e9 ,
210
- numberOfSignificantValueDigits : 5
211
- } ) ,
175
+ tracker : new Tracker ( ) ,
212
176
start : BigInt ( 0 ) ,
213
177
handler : noOp ,
214
178
notifier,
0 commit comments