File tree Expand file tree Collapse file tree 5 files changed +34
-1
lines changed
Expand file tree Collapse file tree 5 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,7 @@ A `Suite` manages and executes benchmark functions. It provides two methods: `ad
110110 * ` 'time' ` - Measures actual execution time for a single run.
111111 * ` useWorkers ` {boolean} Whether to run benchmarks in worker threads. ** Default:** ` false ` .
112112 * ` plugins ` {Array} Array of plugin instances to use.
113+ * ` minSamples ` {number} Minimum number of samples per round for all benchmarks in the suite. Can be overridden per benchmark. ** Default:** ` 10 ` samples.
113114
114115If no ` reporter ` is provided, results are printed to the console.
115116
Original file line number Diff line number Diff line change @@ -28,6 +28,7 @@ export declare namespace BenchNode {
2828 benchmarkMode ?: "ops" | "time" ;
2929 useWorkers ?: boolean ;
3030 plugins ?: Plugin [ ] ;
31+ minSamples ?: number ; // Minimum number of samples per round for all benchmarks
3132 }
3233
3334 interface BenchmarkOptions {
Original file line number Diff line number Diff line change @@ -109,6 +109,7 @@ class Suite {
109109 #useWorkers;
110110 #benchmarkMode;
111111 #reporterOptions;
112+ #minSamples;
112113
113114 constructor ( options = { } ) {
114115 this . #benchmarks = [ ] ;
@@ -140,17 +141,29 @@ class Suite {
140141 this . #reporterOptions = options . reporterOptions || {
141142 printHeader : true ,
142143 } ;
144+
145+ // Suite-level minSamples option
146+ if ( options . minSamples !== undefined ) {
147+ validateNumber ( options . minSamples , "options.minSamples" , 1 ) ;
148+ this . #minSamples = options . minSamples ;
149+ } else {
150+ this . #minSamples = defaultBenchOptions . minSamples ;
151+ }
143152 }
144153
145154 add ( name , options , fn ) {
146155 validateString ( name , "name" ) ;
147156 if ( typeof options === "function" ) {
148157 fn = options ;
149- options = defaultBenchOptions ;
158+ options = {
159+ ...defaultBenchOptions ,
160+ minSamples : this . #minSamples,
161+ } ;
150162 } else {
151163 validateObject ( options , "options" ) ;
152164 options = {
153165 ...defaultBenchOptions ,
166+ minSamples : this . #minSamples,
154167 ...options ,
155168 } ;
156169 // Enforce strict minimum (> 1e-6s). Using EPSILON to make boundary exclusive.
Original file line number Diff line number Diff line change @@ -44,6 +44,21 @@ describe("API Interface", () => {
4444 }
4545 } ) ;
4646
47+ it ( "suite-level minSamples should be a valid number" , ( ) => {
48+ for ( const r of [ "ds" , { } , ( ) => { } ] ) {
49+ assert . throws (
50+ ( ) => {
51+ new Suite ( { minSamples : r } ) ;
52+ } ,
53+ {
54+ code : "ERR_INVALID_ARG_TYPE" ,
55+ } ,
56+ ) ;
57+ }
58+ // doesNotThrow
59+ new Suite ( { minSamples : 20 } ) ;
60+ } ) ;
61+
4762 describe ( "suite.add" , ( ) => {
4863 const bench = new Suite ( { reporter : noop } ) ;
4964 it ( "name should be an string" , ( ) => {
Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ expectType<BenchNode.Suite>(
2727) ;
2828expectType < BenchNode . Suite > ( new Suite ( { reporter : false } ) ) ;
2929expectType < BenchNode . Suite > ( new Suite ( { reporter : null } ) ) ;
30+ expectType < BenchNode . Suite > ( new Suite ( { minSamples : 20 } ) ) ;
3031
3132expectAssignable < BenchNode . SuiteOptions > ( { } ) ;
3233expectAssignable < BenchNode . SuiteOptions > ( { reporter : chartReport } ) ;
@@ -35,8 +36,10 @@ expectAssignable<BenchNode.SuiteOptions>({ useWorkers: false });
3536expectAssignable < BenchNode . SuiteOptions > ( {
3637 plugins : [ new V8GetOptimizationStatus ( ) ] ,
3738} ) ;
39+ expectAssignable < BenchNode . SuiteOptions > ( { minSamples : 20 } ) ;
3840expectNotAssignable < BenchNode . SuiteOptions > ( { unknownOption : "test" } ) ;
3941expectNotAssignable < BenchNode . SuiteOptions > ( { reporter : "not-a-function" } ) ;
42+ expectNotAssignable < BenchNode . SuiteOptions > ( { minSamples : "not-a-number" } ) ;
4043
4144expectAssignable < BenchNode . BenchmarkOptions > ( { } ) ;
4245expectAssignable < BenchNode . BenchmarkOptions > ( { minTime : 0.1 } ) ;
You can’t perform that action at this time.
0 commit comments