1
1
import { add , complete , cycle , suite } from 'benny' ;
2
- import { readFileSync , writeFileSync } from 'fs' ;
2
+ import { readFileSync , writeFileSync , existsSync , unlinkSync } from 'fs' ;
3
3
import { join } from 'path' ;
4
4
import { writePreviewGraph } from './graph' ;
5
5
import { getRegisteredBenchmarks } from './register' ;
@@ -10,15 +10,18 @@ const NODE_VERSION = process.env.NODE_VERSION || process.version;
10
10
const NODE_VERSION_FOR_PREVIEW = 17 ;
11
11
const TEST_PREVIEW_GENERATION = false ;
12
12
13
- export async function main ( ) {
13
+ /**
14
+ * Run all registered benchmarks and append the results to a file.
15
+ */
16
+ export async function runAllBenchmarks ( ) {
14
17
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
16
19
const allResults : BenchmarkResult [ ] = JSON . parse (
17
20
readFileSync ( join ( DOCS_DIR , 'results' , 'node-17.json' ) ) . toString ( )
18
21
) . results ;
19
22
20
23
await writePreviewGraph ( {
21
- filename : join ( DOCS_DIR , 'results' , 'preview.svg' ) ,
24
+ filename : previewSvgFilename ( ) ,
22
25
values : allResults ,
23
26
} ) ;
24
27
@@ -42,24 +45,40 @@ export async function main() {
42
45
} ) ;
43
46
}
44
47
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
+ }
47
51
48
- JSON . stringify ( {
49
- results : allResults ,
50
- } ) ,
52
+ /**
53
+ * Remove the results json file.
54
+ */
55
+ export function deleteResults ( ) {
56
+ const fileName = resultsJsonFilename ( ) ;
51
57
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 ( ) ;
54
68
55
69
if ( majorVersion === NODE_VERSION_FOR_PREVIEW ) {
70
+ const allResults : BenchmarkResult [ ] = JSON . parse (
71
+ readFileSync ( resultsJsonFilename ( ) ) . toString ( )
72
+ ) . results ;
73
+
56
74
await writePreviewGraph ( {
57
- filename : join ( DOCS_DIR , 'results' , 'preview.svg' ) ,
75
+ filename : previewSvgFilename ( ) ,
58
76
values : allResults ,
59
77
} ) ;
60
78
}
61
79
}
62
80
81
+ // run a benchmark fn with benny
63
82
async function runBenchmarks ( name : string , cases : BenchmarkCase [ ] ) {
64
83
const fns = cases . map ( c => add ( c . moduleName , ( ) => c . run ( ) ) ) ;
65
84
@@ -74,6 +93,52 @@ async function runBenchmarks(name: string, cases: BenchmarkCase[]) {
74
93
) ;
75
94
}
76
95
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
+
77
142
function getNodeMajorVersion ( ) {
78
143
let majorVersion = 0 ;
79
144
0 commit comments