11#!/usr/bin/env node
2+
23/**
34 * External dependencies
45 */
5- // eslint-disable-next-line @typescript-eslint/no-var-requires
6- const { existsSync, readFileSync, writeFileSync } = require ( 'node:fs' ) ;
7- // eslint-disable-next-line @typescript-eslint/no-var-requires
8- const { join, resolve } = require ( 'node:path' ) ;
6+ const { existsSync, readFileSync, writeFileSync } = require ( 'node:fs' ) ;
7+ const { join, resolve } = require ( 'node:path' ) ;
98
10- process . env . WP_ARTIFACTS_PATH ??= join ( process . cwd ( ) , 'artifacts' ) ;
9+ process . env . WP_ARTIFACTS_PATH ??= join ( process . cwd ( ) , 'artifacts' ) ;
1110
12- const args = process . argv . slice ( 2 ) ;
11+ const args = process . argv . slice ( 2 ) ;
1312
14- const beforeFile = args [ 1 ] ;
15- const afterFile = args [ 0 ] || resolve ( './artifacts/performance-results.json' ) ;
13+ const beforeFile = args [ 1 ] ;
14+ const afterFile =
15+ args [ 0 ] || resolve ( './artifacts/performance-results.json' ) ;
1616
17- if ( ! existsSync ( afterFile ) ) {
18- // eslint-disable-next-line no-console
19- console . error ( `File not found: ${ afterFile } ` ) ;
20- process . exit ( 1 ) ;
17+ if ( ! existsSync ( afterFile ) ) {
18+ console . error ( `File not found: ${ afterFile } ` ) ;
19+ process . exit ( 1 ) ;
2120}
2221
23- if ( beforeFile && ! existsSync ( beforeFile ) ) {
24- // eslint-disable-next-line no-console
25- console . error ( `File not found: ${ beforeFile } ` ) ;
26- process . exit ( 1 ) ;
22+ if ( beforeFile && ! existsSync ( beforeFile ) ) {
23+ console . error ( `File not found: ${ beforeFile } ` ) ;
24+ process . exit ( 1 ) ;
2725}
2826
2927/**
3028 * @param {unknown } v
3129 * @return {string } Formatted value.
3230 */
33- function formatTableValue ( v ) {
34- if ( v === true || v === 'true' ) return '✅' ;
35- if ( ! v || v === 'false' ) return '' ;
36- return v ?. toString ( ) || String ( v ) ;
31+ function formatTableValue ( v ) {
32+ if ( v === true || v === 'true' ) return '✅' ;
33+ if ( ! v || v === 'false' ) return '' ;
34+ return v ?. toString ( ) || String ( v ) ;
3735}
3836
3937/**
@@ -64,22 +62,25 @@ function formatTableValue(v) {
6462 * @param {Array<Object> } rows Table rows.
6563 * @return {string } Markdown table content.
6664 */
67- function formatAsMarkdownTable ( rows ) {
65+ function formatAsMarkdownTable ( rows ) {
6866 let result = '' ;
69- const headers = Object . keys ( rows [ 0 ] ) ;
70- for ( const header of headers ) {
71- result += `| ${ header } ` ;
67+ const headers = Object . keys ( rows [ 0 ] ) ;
68+ for ( const header of headers ) {
69+ result += `| ${ header } ` ;
7270 }
7371 result += '|\n' ;
74- for ( const header of headers ) {
75- const dashes = '-' . repeat ( header . length ) ;
76- result += `| ${ dashes } ` ;
72+ for ( const header of headers ) {
73+ const dashes = '-' . repeat ( header . length ) ;
74+ result += `| ${ dashes } ` ;
7775 }
7876 result += '|\n' ;
7977
80- for ( const row of rows ) {
81- for ( const [ key , value ] of Object . entries ( row ) ) {
82- result += `| ${ formatTableValue ( value ) . padStart ( key . length , ' ' ) } ` ;
78+ for ( const row of rows ) {
79+ for ( const [ key , value ] of Object . entries ( row ) ) {
80+ result += `| ${ formatTableValue ( value ) . padStart (
81+ key . length ,
82+ ' '
83+ ) } `;
8384 }
8485 result += '|\n' ;
8586 }
@@ -93,15 +94,15 @@ function formatAsMarkdownTable(rows) {
9394 * @param {number[] } array List of numbers.
9495 * @return {number } Median.
9596 */
96- function median ( array ) {
97- const mid = Math . floor ( array . length / 2 ) ;
98- const numbers = [ ...array ] . sort ( ( a , b ) => a - b ) ;
97+ function median ( array ) {
98+ const mid = Math . floor ( array . length / 2 ) ;
99+ const numbers = [ ...array ] . sort ( ( a , b ) => a - b ) ;
99100 const result =
100101 array . length % 2 !== 0
101- ? numbers [ mid ]
102- : ( numbers [ mid - 1 ] + numbers [ mid ] ) / 2 ;
102+ ? numbers [ mid ]
103+ : ( numbers [ mid - 1 ] + numbers [ mid ] ) / 2 ;
103104
104- return Number ( result . toFixed ( 2 ) ) ;
105+ return Number ( result . toFixed ( 2 ) ) ;
105106}
106107
107108/**
@@ -114,39 +115,36 @@ let beforeStats = [];
114115 */
115116let afterStats ;
116117
117- if ( beforeFile ) {
118+ if ( beforeFile ) {
118119 try {
119120 beforeStats = JSON . parse (
120- readFileSync ( beforeFile , { encoding : 'utf-8' } )
121+ readFileSync ( beforeFile , { encoding : 'utf-8' } )
121122 ) ;
122123 } catch { }
123124}
124125
125126try {
126- afterStats = JSON . parse ( readFileSync ( afterFile , { encoding : 'utf-8' } ) ) ;
127+ afterStats = JSON . parse ( readFileSync ( afterFile , { encoding : 'utf-8' } ) ) ;
127128} catch {
128- // eslint-disable-next-line no-console
129- console . error ( `Could not read file: ${ afterFile } ` ) ;
130- process . exit ( 1 ) ;
129+ console . error ( `Could not read file: ${ afterFile } ` ) ;
130+ process . exit ( 1 ) ;
131131}
132132
133133let summaryMarkdown = `**Performance Test Results**\n\n` ;
134134
135- if ( process . env . GITHUB_SHA ) {
136- summaryMarkdown += `Performance test results for ${ process . env . GITHUB_SHA } are in 🛎️!\n\n` ;
135+ if ( process . env . GITHUB_SHA ) {
136+ summaryMarkdown += `Performance test results for ${ process . env . GITHUB_SHA } are in 🛎️!\n\n` ;
137137} else {
138138 summaryMarkdown += `Performance test results are in 🛎️!\n\n` ;
139139}
140140
141- if ( beforeFile ) {
141+ if ( beforeFile ) {
142142 summaryMarkdown += `Note: the numbers in parentheses show the difference to the previous (baseline) test run.\n\n` ;
143143}
144144
145- // eslint-disable-next-line no-console
146- console . log ( 'Performance Test Results\n' ) ;
145+ console . log ( 'Performance Test Results\n' ) ;
147146
148- if ( beforeFile ) {
149- // eslint-disable-next-line no-console
147+ if ( beforeFile ) {
150148 console . log (
151149 'Note: the numbers in parentheses show the difference to the previous (baseline) test run.\n'
152150 ) ;
@@ -166,32 +164,32 @@ const PERCENTAGE_VARIANCE = 2;
166164 * @param {string } key Key.
167165 * @return {string } Formatted value.
168166 */
169- function formatValue ( value , key ) {
170- if ( key === 'CLS' ) {
171- return value . toFixed ( 2 ) ;
167+ function formatValue ( value , key ) {
168+ if ( key === 'CLS' ) {
169+ return value . toFixed ( 2 ) ;
172170 }
173171
174- if ( key === 'wpDbQueries' ) {
175- return value . toFixed ( 0 ) ;
172+ if ( key === 'wpDbQueries' ) {
173+ return value . toFixed ( 0 ) ;
176174 }
177175
178- if ( key === 'wpMemoryUsage' ) {
179- return `${ ( value / Math . pow ( 10 , 6 ) ) . toFixed ( 2 ) } MB` ;
176+ if ( key === 'wpMemoryUsage' ) {
177+ return `${ ( value / Math . pow ( 10 , 6 ) ) . toFixed ( 2 ) } MB` ;
180178 }
181179
182- return `${ value . toFixed ( 2 ) } ms` ;
180+ return `${ value . toFixed ( 2 ) } ms` ;
183181}
184182
185- for ( const { file, title, results } of afterStats ) {
186- const prevStat = beforeStats . find ( ( s ) => s . file === file ) ;
183+ for ( const { file, title, results } of afterStats ) {
184+ const prevStat = beforeStats . find ( ( s ) => s . file === file ) ;
187185
188186 /**
189187 * @type {Array<Record<string,string|number|boolean>> }
190188 */
191189 const diffResults = [ ] ;
192190
193- for ( const i in results ) {
194- const newResult = results [ i ] ;
191+ for ( const i in results ) {
192+ const newResult = results [ i ] ;
195193
196194 /**
197195 * @type {Record<string, string> }
@@ -200,54 +198,55 @@ for (const { file, title, results } of afterStats) {
200198 Run : i ,
201199 } ;
202200
203- for ( const [ key , values ] of Object . entries ( newResult ) ) {
201+ for ( const [ key , values ] of Object . entries ( newResult ) ) {
204202 // Only do comparison if the number of results is the same.
205203 const prevValues =
206204 prevStat ?. results . length === results . length
207- ? prevStat ?. results [ i ] . key
205+ ? prevStat ?. results [ i ] . key
208206 : null ;
209207
210- const value = median ( values ) ;
211- const prevValue = prevValues ? median ( prevValues ) : 0 ;
208+ const value = median ( values ) ;
209+ const prevValue = prevValues ? median ( prevValues ) : 0 ;
212210 const delta = value - prevValue ;
213- const percentage = ( delta / value ) * 100 ;
211+ const percentage = ( delta / value ) * 100 ;
214212
215213 // Skip if there is not a significant delta or none at all.
216214 if (
217- ! prevValues ||
218- ! percentage ||
219- Math . abs ( percentage ) <= PERCENTAGE_VARIANCE ||
220- ! delta ||
221- Math . abs ( delta ) <= DELTA_VARIANCE
215+ ! prevValues ||
216+ ! percentage ||
217+ Math . abs ( percentage ) <= PERCENTAGE_VARIANCE ||
218+ ! delta ||
219+ Math . abs ( delta ) <= DELTA_VARIANCE
222220 ) {
223- diffResult [ key ] = formatValue (
224- /** @type {number } */ ( value ) ,
221+ diffResult [ key ] = formatValue (
222+ /** @type {number } */ ( value ) ,
225223 key
226224 ) ;
227225 continue ;
228226 }
229227
230228 const prefix = delta > 0 ? '+' : '' ;
231229
232- diffResult [ key ] = `${ formatValue (
230+ diffResult [ key ] = `${ formatValue (
233231 value ,
234232 key
235- ) } (${ prefix } ${ formatValue ( delta , key ) } / ${ prefix } ${ percentage } %)`;
233+ ) } (${ prefix } ${ formatValue (
234+ delta ,
235+ key
236+ ) } / ${ prefix } ${ percentage } %)`;
236237 }
237238
238- diffResults . push ( diffResult ) ;
239+ diffResults . push ( diffResult ) ;
239240 }
240241
241- // eslint-disable-next-line no-console
242- console . log ( title ) ;
243- // eslint-disable-next-line no-console
244- console . table ( diffResults ) ;
242+ console . log ( title ) ;
243+ console . table ( diffResults ) ;
245244
246- summaryMarkdown += `**${ title } **\n\n` ;
247- summaryMarkdown += `${ formatAsMarkdownTable ( diffResults ) } \n` ;
245+ summaryMarkdown += `**${ title } **\n\n` ;
246+ summaryMarkdown += `${ formatAsMarkdownTable ( diffResults ) } \n` ;
248247}
249248
250249writeFileSync (
251- join ( process . env . WP_ARTIFACTS_PATH , '/performance-results.md' ) ,
250+ join ( process . env . WP_ARTIFACTS_PATH , '/performance-results.md' ) ,
252251 summaryMarkdown
253252) ;
0 commit comments