11import { add , complete , cycle , suite } from 'benny' ;
2- import { readFileSync , writeFileSync } from 'fs' ;
2+ import { readFileSync , writeFileSync , existsSync , unlinkSync } from 'fs' ;
33import { join } from 'path' ;
44import { writePreviewGraph } from './graph' ;
55import { getRegisteredBenchmarks } from './register' ;
@@ -10,15 +10,18 @@ const NODE_VERSION = process.env.NODE_VERSION || process.version;
1010const NODE_VERSION_FOR_PREVIEW = 17 ;
1111const TEST_PREVIEW_GENERATION = false ;
1212
13- export async function main ( ) {
13+ /**
14+ * Run all registered benchmarks and append the results to a file.
15+ */
16+ export async function runAllBenchmarks ( ) {
1417 if ( TEST_PREVIEW_GENERATION ) {
15- // just generate the preview without using benchmark data from a previous run
18+ // during development: generate the preview using benchmark data from a previous run
1619 const allResults : BenchmarkResult [ ] = JSON . parse (
1720 readFileSync ( join ( DOCS_DIR , 'results' , 'node-17.json' ) ) . toString ( )
1821 ) . results ;
1922
2023 await writePreviewGraph ( {
21- filename : join ( DOCS_DIR , 'results' , 'preview.svg' ) ,
24+ filename : previewSvgFilename ( ) ,
2225 values : allResults ,
2326 } ) ;
2427
@@ -42,24 +45,40 @@ export async function main() {
4245 } ) ;
4346 }
4447
45- writeFileSync (
46- join ( DOCS_DIR , 'results' , `node-${ majorVersion } .json` ) ,
48+ // collect results of isolated benchmark runs into a single file
49+ appendResults ( allResults ) ;
50+ }
4751
48- JSON . stringify ( {
49- results : allResults ,
50- } ) ,
52+ /**
53+ * Remove the results json file.
54+ */
55+ export function deleteResults ( ) {
56+ const fileName = resultsJsonFilename ( ) ;
5157
52- { encoding : 'utf8' }
53- ) ;
58+ if ( existsSync ( fileName ) ) {
59+ unlinkSync ( fileName ) ;
60+ }
61+ }
62+
63+ /**
64+ * Generate the preview svg shown in the readme.
65+ */
66+ export async function createPreviewGraph ( ) {
67+ const majorVersion = getNodeMajorVersion ( ) ;
5468
5569 if ( majorVersion === NODE_VERSION_FOR_PREVIEW ) {
70+ const allResults : BenchmarkResult [ ] = JSON . parse (
71+ readFileSync ( resultsJsonFilename ( ) ) . toString ( )
72+ ) . results ;
73+
5674 await writePreviewGraph ( {
57- filename : join ( DOCS_DIR , 'results' , 'preview.svg' ) ,
75+ filename : previewSvgFilename ( ) ,
5876 values : allResults ,
5977 } ) ;
6078 }
6179}
6280
81+ // run a benchmark fn with benny
6382async function runBenchmarks ( name : string , cases : BenchmarkCase [ ] ) {
6483 const fns = cases . map ( c => add ( c . moduleName , ( ) => c . run ( ) ) ) ;
6584
@@ -74,6 +93,52 @@ async function runBenchmarks(name: string, cases: BenchmarkCase[]) {
7493 ) ;
7594}
7695
96+ // append results to an existing file or create a new one
97+ function appendResults ( results : BenchmarkResult [ ] ) {
98+ const fileName = resultsJsonFilename ( ) ;
99+ const existingResults : BenchmarkResult [ ] = existsSync ( fileName )
100+ ? JSON . parse ( readFileSync ( fileName ) . toString ( ) ) . results
101+ : [ ] ;
102+
103+ // check that we're appending unique data
104+ const getKey = ( {
105+ benchmark,
106+ name,
107+ nodeVersion,
108+ } : BenchmarkResult ) : string => {
109+ return JSON . stringify ( { benchmark, name, nodeVersion } ) ;
110+ } ;
111+ const existingResultsIndex = new Set ( existingResults . map ( r => getKey ( r ) ) ) ;
112+
113+ results . forEach ( r => {
114+ if ( existingResultsIndex . has ( getKey ( r ) ) ) {
115+ console . error ( 'Result %s already exists in' , getKey ( r ) , fileName ) ;
116+
117+ throw new Error ( 'Duplicate result in result json file' ) ;
118+ }
119+ } ) ;
120+
121+ writeFileSync (
122+ fileName ,
123+
124+ JSON . stringify ( {
125+ results : [ ...existingResults , ...results ] ,
126+ } ) ,
127+
128+ { encoding : 'utf8' }
129+ ) ;
130+ }
131+
132+ function resultsJsonFilename ( ) {
133+ const majorVersion = getNodeMajorVersion ( ) ;
134+
135+ return join ( DOCS_DIR , 'results' , `node-${ majorVersion } .json` ) ;
136+ }
137+
138+ function previewSvgFilename ( ) {
139+ return join ( DOCS_DIR , 'results' , 'preview.svg' ) ;
140+ }
141+
77142function getNodeMajorVersion ( ) {
78143 let majorVersion = 0 ;
79144
0 commit comments