wordpress-develop/tests/performance/log-results.js
Joe McGill 8417a97deb Build/Test Tools: Add a performance measurement workflow.
This adds a new GitHub Action workflow that measures a set of performance metrics on every commit, so we can track changes in the performance of WordPress over time and more easily identify changes that are responsible for significant performance improvements or regressions during development cycles.

The workflow measures the homepage of a classic theme (Twenty Twenty-One) and a block theme (Twenty Twenty-Three) set up with demo content from the Theme Test Data project. Using the e2e testing framework, it makes 20 requests and records the median value of the following Server Timing metrics, generated by an mu-plugin installed as part of this workflow:

- Total server response time
- Server time before templates are loaded
- Server time during template rendering

In addition to measuring the performance metrics of the current commit, it also records performance metrics of a consistent version of WordPress (6.1.1) to be used as a baseline measurement in order to remove variance caused by the GitHub workers themselves from our reporting.

The measurements are collected and displayed at https://www.codevitals.run/project/wordpress.

Props adamsilverstein, mukesh27, flixos90, youknowriad, oandregal, desrosj, costdev, swissspidy.
Fixes #57687.


git-svn-id: https://develop.svn.wordpress.org/trunk@55459 602fd350-edb4-49c9-b593-d223f7449a82
2023-03-03 20:37:10 +00:00

103 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
/**
* External dependencies.
*/
const fs = require( 'fs' );
const path = require( 'path' );
const https = require( 'https' );
const [ token, branch, hash, baseHash, timestamp, host ] = process.argv.slice( 2 );
const { median } = require( './utils' );
// The list of test suites to log.
const testSuites = [
'home-block-theme',
'home-classic-theme',
];
// A list of results to parse based on test suites.
const testResults = testSuites.map(( key ) => ({
key,
file: `${ key }.test.results.json`,
}));
// A list of base results to parse based on test suites.
const baseResults = testSuites.map(( key ) => ({
key,
file: `base-${ key }.test.results.json`,
}));
/**
* Parse test files into JSON objects.
*
* @param {string} fileName The name of the file.
* @returns An array of parsed objects from each file.
*/
const parseFile = ( fileName ) => (
JSON.parse(
fs.readFileSync( path.join( __dirname, '/specs/', fileName ), 'utf8' )
)
);
/**
* Gets the array of metrics from a list of results.
*
* @param {Object[]} results A list of results to format.
* @return {Object[]} Metrics.
*/
const formatResults = ( results ) => {
return results.reduce(
( result, { key, file } ) => {
return {
...result,
...Object.fromEntries(
Object.entries(
parseFile( file ) ?? {}
).map( ( [ metric, value ] ) => [
key + '-' + metric,
median ( value ),
] )
),
};
},
{}
);
};
const data = new TextEncoder().encode(
JSON.stringify( {
branch,
hash,
baseHash,
timestamp: parseInt( timestamp, 10 ),
metrics: formatResults( testResults ),
baseMetrics: formatResults( baseResults ),
} )
);
const options = {
hostname: host,
port: 443,
path: '/api/log?token=' + token,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
},
};
const req = https.request( options, ( res ) => {
console.log( `statusCode: ${ res.statusCode }` );
res.on( 'data', ( d ) => {
process.stdout.write( d );
} );
} );
req.on( 'error', ( error ) => {
console.error( error );
} );
req.write( data );
req.end();