11import assert from 'node:assert' ;
22
33import { NS_PER_SEC } from './config.js' ;
4- import type { BenchmarkResult , BenchmarkSample } from './types.js' ;
4+ import type { BenchmarkResult , BenchmarkTimingSample } from './types.js' ;
55
66// T-Distribution two-tailed critical values for 95% confidence.
77// See http://www.itl.nist.gov/div898/handbook/eda/section3/eda3672.htm.
@@ -18,35 +18,40 @@ const tTableInfinity = 1.96;
1818// Computes stats on benchmark results.
1919export function computeStats (
2020 name : string ,
21- samples : ReadonlyArray < BenchmarkSample > ,
21+ timingSamples : ReadonlyArray < BenchmarkTimingSample > ,
22+ memorySamples : ReadonlyArray < number > ,
2223) : BenchmarkResult {
23- assert ( samples . length > 1 ) ;
24+ assert ( timingSamples . length > 1 ) ;
25+ assert ( memorySamples . length > 0 ) ;
2426
2527 // Compute the sample mean (estimate of the population mean).
2628 let mean = 0 ;
27- let meanMemUsed = 0 ;
28- for ( const { clocked, memUsed } of samples ) {
29+ for ( const { clocked } of timingSamples ) {
2930 mean += clocked ;
31+ }
32+ mean /= timingSamples . length ;
33+
34+ let meanMemUsed = 0 ;
35+ for ( const memUsed of memorySamples ) {
3036 meanMemUsed += memUsed ;
3137 }
32- mean /= samples . length ;
33- meanMemUsed /= samples . length ;
38+ meanMemUsed /= memorySamples . length ;
3439
3540 // Compute the sample variance (estimate of the population variance).
3641 let variance = 0 ;
37- for ( const { clocked } of samples ) {
42+ for ( const { clocked } of timingSamples ) {
3843 variance += ( clocked - mean ) ** 2 ;
3944 }
40- variance /= samples . length - 1 ;
45+ variance /= timingSamples . length - 1 ;
4146
4247 // Compute the sample standard deviation (estimate of the population standard deviation).
4348 const sd = Math . sqrt ( variance ) ;
4449
4550 // Compute the standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean).
46- const sem = sd / Math . sqrt ( samples . length ) ;
51+ const sem = sd / Math . sqrt ( timingSamples . length ) ;
4752
4853 // Compute the degrees of freedom.
49- const df = samples . length - 1 ;
54+ const df = timingSamples . length - 1 ;
5055
5156 // Compute the critical value.
5257 const critical = tTable [ df ] ?? tTableInfinity ;
@@ -62,6 +67,6 @@ export function computeStats(
6267 memPerOp : Math . floor ( meanMemUsed ) ,
6368 ops : NS_PER_SEC / mean ,
6469 deviation : rme ,
65- numSamples : samples . length ,
70+ numSamples : timingSamples . length ,
6671 } ;
6772}
0 commit comments