From ddfdd5d025c0f9426e5740a6490c51381b76fba2 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 8 Jan 2025 10:46:12 +0100 Subject: [PATCH] Simplify data structure --- env/tests/performance/cli/results.js | 16 ++++++------- .../config/performance-reporter.ts | 24 ++++--------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/env/tests/performance/cli/results.js b/env/tests/performance/cli/results.js index 4bd7d49..fee6c93 100644 --- a/env/tests/performance/cli/results.js +++ b/env/tests/performance/cli/results.js @@ -107,12 +107,12 @@ function median( array ) { } /** - * @type {Array<{url: string, results: Record[]}>} + * @type {Record< string, Array< Record< string, number[] > > >} */ -let beforeStats = []; +let beforeStats = {}; /** - * @type {Array<{url, results: Record[]}>} + * @type {Record< string, Array< Record< string, number[] > > >} */ let afterStats; @@ -201,11 +201,11 @@ function formatValue( value, key ) { return `${ value.toFixed( 2 ) } ms`; } -for ( const { url, results } of afterStats ) { - const prevStat = beforeStats.find( ( s ) => s.url === url ); +for ( const [ url, results ] of Object.entries( afterStats ) ) { + const prevStat = beforeStats[ url ]; /** - * @type {Array>} + * @type {Array< Record< string, string | number | boolean > >} */ const diffResults = []; @@ -222,8 +222,8 @@ for ( const { url, results } of afterStats ) { for ( const [ key, values ] of Object.entries( newResult ) ) { // Only do comparison if the number of results is the same. const prevValues = - prevStat?.results.length === results.length - ? prevStat?.results[ i ][ key ] + prevStat.length === results.length + ? prevStat[ i ][ key ] : null; const value = median( values ); diff --git a/env/tests/performance/config/performance-reporter.ts b/env/tests/performance/config/performance-reporter.ts index 0e019e3..beb9b2f 100644 --- a/env/tests/performance/config/performance-reporter.ts +++ b/env/tests/performance/config/performance-reporter.ts @@ -14,12 +14,7 @@ process.env.WP_ARTIFACTS_PATH ??= join( process.cwd(), 'artifacts' ); class PerformanceReporter implements Reporter { private shard?: FullConfig[ 'shard' ]; - allResults: Record< - string, - { - results: Record< string, number[] >[]; - } - > = {}; + allResults: Record< string, Array< Record< string, number[] > > > = {}; onBegin( config: FullConfig ) { if ( config.shard ) { @@ -44,11 +39,9 @@ class PerformanceReporter implements Reporter { const resultsByUrl = JSON.parse( performanceResults.body.toString( 'utf-8' ) ) as Record< string, Record< string, number[] > >; for ( const [url, results ] of Object.entries(resultsByUrl)) { - this.allResults[ url ] ??= { - results: [], - }; + this.allResults[ url ] ??= []; - this.allResults[ url ].results.push( + this.allResults[ url ].push( results ); } @@ -64,8 +57,6 @@ class PerformanceReporter implements Reporter { * @param result */ onEnd( result: FullResult ) { - const summary = []; - if ( Object.keys( this.allResults ).length > 0 ) { if ( this.shard ) { console.log( @@ -77,7 +68,7 @@ class PerformanceReporter implements Reporter { console.log( `Status: ${ result.status }` ); } - for ( const [ url, { results } ] of Object.entries( + for ( const [ url, results ] of Object.entries( this.allResults ) ) { console.log( `\nURL: \`${ url }\`\n` ); @@ -91,11 +82,6 @@ class PerformanceReporter implements Reporter { ) ) ); - - summary.push( { - url, - results, - } ); } if ( ! existsSync( process.env.WP_ARTIFACTS_PATH as string ) ) { @@ -107,7 +93,7 @@ class PerformanceReporter implements Reporter { process.env.WP_ARTIFACTS_PATH as string, 'performance-results.json' ), - JSON.stringify( summary, null, 2 ) + JSON.stringify( this.allResults, null, 2 ) ); } }