Skip to content

Commit

Permalink
Simplify data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Jan 8, 2025
1 parent ed90471 commit ddfdd5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 27 deletions.
16 changes: 8 additions & 8 deletions env/tests/performance/cli/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ function median( array ) {
}

/**
* @type {Array<{url: string, results: Record<string,number[]>[]}>}
* @type {Record< string, Array< Record< string, number[] > > >}
*/
let beforeStats = [];
let beforeStats = {};

/**
* @type {Array<{url, results: Record<string,number[]>[]}>}
* @type {Record< string, Array< Record< string, number[] > > >}
*/
let afterStats;

Expand Down Expand Up @@ -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<Record<string,string|number|boolean>>}
* @type {Array< Record< string, string | number | boolean > >}
*/
const diffResults = [];

Expand All @@ -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 );
Expand Down
24 changes: 5 additions & 19 deletions env/tests/performance/config/performance-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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
);
}
Expand All @@ -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(
Expand All @@ -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` );
Expand All @@ -91,11 +82,6 @@ class PerformanceReporter implements Reporter {
)
)
);

summary.push( {
url,
results,
} );
}

if ( ! existsSync( process.env.WP_ARTIFACTS_PATH as string ) ) {
Expand All @@ -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 )
);
}
}
Expand Down

0 comments on commit ddfdd5d

Please sign in to comment.