11import assert from 'node:assert' ;
22import path from 'node:path' ;
3+ import readline from 'node:readline' ;
34
5+ import type { Runtime } from './args.js' ;
46import { getArguments } from './args.js' ;
57import {
68 maxTime ,
@@ -9,6 +11,7 @@ import {
911} from './config.js' ;
1012import {
1113 cyan ,
14+ grey ,
1215 printBenchmarkResults ,
1316 printPairedComparisons ,
1417 red ,
@@ -28,32 +31,46 @@ import {
2831
2932export function runBenchmarks ( ) : void {
3033 // Get the revisions and make things happen!
31- const { benchmarks, revisions } = getArguments ( process . argv . slice ( 2 ) ) ;
34+ const { benchmarks, revisions, runtime } = getArguments (
35+ process . argv . slice ( 2 ) ,
36+ ) ;
3237 const benchmarkProjects = prepareBenchmarkProjects ( revisions ) ;
3338
39+ console . log ( '' ) ;
40+ console . log ( '\u2699\uFE0F Runtime: ' + runtime ) ;
41+ console . log ( '' ) ;
42+
3443 for ( const benchmark of benchmarks ) {
35- runBenchmark ( benchmark , benchmarkProjects ) ;
44+ runBenchmark ( benchmark , benchmarkProjects , runtime ) ;
3645 }
3746}
3847
3948// Prepare all revisions and run benchmarks matching a pattern against them.
4049function runBenchmark (
4150 benchmark : string ,
4251 benchmarkProjects : ReadonlyArray < BenchmarkProject > ,
52+ runtime : Runtime ,
4353) : void {
44- const memorySamples : Array < Array < number > > = [ ] ;
54+ const memorySamples : Array < Array < number > | undefined > = [ ] ;
55+ const includesMemory = runtime !== 'bun' ;
56+
4557 for ( let i = 0 ; i < benchmarkProjects . length ; ++ i ) {
4658 const modulePath = path . join ( benchmarkProjects [ i ] . projectPath , benchmark ) ;
4759
4860 if ( i === 0 ) {
49- console . log ( '\u23F1 ' + getBenchmarkName ( modulePath ) ) ;
61+ console . log ( '\u23F1 ' + getBenchmarkName ( modulePath , runtime ) ) ;
62+ if ( includesMemory ) {
63+ writeProgress ( ' completed ' + cyan ( 0 ) + ' memory tests...' ) ;
64+ }
65+ }
66+
67+ if ( ! includesMemory ) {
68+ continue ;
5069 }
5170
5271 try {
53- memorySamples [ i ] = collectMemorySamples ( modulePath ) ;
54- process . stdout . write (
55- ' completed ' + cyan ( i + 1 ) + ' memory tests...\u000D' ,
56- ) ;
72+ memorySamples [ i ] = collectMemorySamples ( modulePath , runtime ) ;
73+ writeProgress ( ' completed ' + cyan ( i + 1 ) + ' memory tests...' ) ;
5774 } catch ( error ) {
5875 const errorMessage =
5976 error instanceof Error ? error . message : String ( error ) ;
@@ -63,11 +80,15 @@ function runBenchmark(
6380 return ;
6481 }
6582 }
66- process . stdout . write ( '\n' ) ;
83+ if ( includesMemory ) {
84+ endProgressLine ( ) ;
85+ } else {
86+ console . log ( ' ' + grey ( 'skipping memory benchmarks for bun runtime' ) ) ;
87+ }
6788
6889 let timingSamples : Array < Array < number > > ;
6990 try {
70- timingSamples = collectTimingSamples ( benchmark , benchmarkProjects ) ;
91+ timingSamples = collectTimingSamples ( benchmark , benchmarkProjects , runtime ) ;
7192 } catch {
7293 console . log ( ' ' + red ( 'timing samples collection failed' ) ) ;
7394 return ;
@@ -80,7 +101,7 @@ function runBenchmark(
80101 result = computeStats (
81102 benchmarkProjects [ i ] . revision ,
82103 timingSamples [ i ] ,
83- memorySamples [ i ] ,
104+ includesMemory ? memorySamples [ i ] : undefined ,
84105 ) ;
85106 } catch ( error ) {
86107 const errorMessage =
@@ -96,7 +117,7 @@ function runBenchmark(
96117
97118 console . log ( '\n' ) ;
98119
99- printBenchmarkResults ( results ) ;
120+ printBenchmarkResults ( results , includesMemory ) ;
100121 printPairedComparisons (
101122 getPairedComparisons (
102123 benchmarkProjects . map ( ( { revision } ) => revision ) ,
@@ -109,6 +130,7 @@ function runBenchmark(
109130function collectTimingSamples (
110131 benchmark : string ,
111132 benchmarkProjects : ReadonlyArray < BenchmarkProject > ,
133+ runtime : Runtime ,
112134) : Array < Array < number > > {
113135 const sampleGroups = benchmarkProjects . map ( ( project ) => ( {
114136 revision : project . revision ,
@@ -122,14 +144,15 @@ function collectTimingSamples(
122144 // pairwise revision comparison has stabilized.
123145 const start = Date . now ( ) ;
124146 let round = 0 ;
147+ writeProgress ( ' completed ' + cyan ( 0 ) + ' timing rounds...' ) ;
125148 while (
126149 ( Date . now ( ) - start ) / 1e3 < maxTime &&
127150 ( round < minTimingSamplesPerBenchmark ||
128151 ! havePairwiseComparisonsStabilized ( timingSamples ) )
129152 ) {
130153 for ( const sampleGroup of shuffled ( sampleGroups ) ) {
131154 try {
132- const sample = sampleTimingModule ( sampleGroup . modulePath ) ;
155+ const sample = sampleTimingModule ( sampleGroup . modulePath , runtime ) ;
133156
134157 assert ( sample > 0 ) ;
135158 sampleGroup . samples . push ( sample ) ;
@@ -142,13 +165,21 @@ function collectTimingSamples(
142165 }
143166
144167 ++ round ;
145- process . stdout . write (
146- ' completed ' + cyan ( round ) + ' timing rounds...\u000D' ,
147- ) ;
168+ writeProgress ( ' completed ' + cyan ( round ) + ' timing rounds...' ) ;
148169 }
149170 return timingSamples ;
150171}
151172
173+ function writeProgress ( message : string ) : void {
174+ readline . clearLine ( process . stdout , 0 ) ;
175+ readline . cursorTo ( process . stdout , 0 ) ;
176+ process . stdout . write ( message ) ;
177+ }
178+
179+ function endProgressLine ( ) : void {
180+ process . stdout . write ( '\n' ) ;
181+ }
182+
152183function shuffled < T > ( array : ReadonlyArray < T > ) : Array < T > {
153184 const shuffledArray = [ ...array ] ;
154185 // Fisher-Yates shuffle: walk backward and swap each slot with a random
@@ -163,14 +194,17 @@ function shuffled<T>(array: ReadonlyArray<T>): Array<T> {
163194 return shuffledArray ;
164195}
165196
166- export function collectMemorySamples ( modulePath : string ) : Array < number > {
197+ export function collectMemorySamples (
198+ modulePath : string ,
199+ runtime : Runtime ,
200+ ) : Array < number > {
167201 const samples : Array < number > = [ ] ;
168202 for (
169203 let sampleIndex = 0 ;
170204 sampleIndex < memorySamplesPerBenchmark ;
171205 ++ sampleIndex
172206 ) {
173- const sample = sampleMemoryModule ( modulePath ) ;
207+ const sample = sampleMemoryModule ( modulePath , runtime ) ;
174208 assert ( sample > 0 ) ;
175209 samples . push ( sample ) ;
176210 }
0 commit comments