Performance: Log the performance metrics to codevitals (#42823)

This commit is contained in:
Seghir Nadir 2023-12-14 16:31:51 +01:00 committed by GitHub
commit 6ff354b943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 96 additions and 1 deletions

View File

@ -51,3 +51,11 @@ jobs:
with:
name: performance-results
path: ${{ env.WP_ARTIFACTS_PATH }}/*.performance-results*.json
- name: Publish performance results
if: github.event_name == 'push'
env:
CODEVITALS_PROJECT_TOKEN: ${{ secrets.CODEVITALS_PROJECT_TOKEN }}
run: |
COMMITTED_AT=$(git show -s $GITHUB_SHA --format="%cI")
cd tools/compare-perf && pnpm run log $CODEVITALS_PROJECT_TOKEN trunk $GITHUB_SHA 19f3d0884617d7ecdcf37664f648a51e2987cada $COMMITTED_AT

View File

@ -0,0 +1,86 @@
#!/usr/bin/env node
/* eslint-disable no-console */
const fs = require( 'fs' );
const path = require( 'path' );
const https = require( 'https' );
const [ token, branch, hash, baseHash, timestamp ] = process.argv.slice( 2 );
const resultsFiles = [
{
file: 'editor.performance-results.json',
metricsPrefix: 'editor-',
},
];
const performanceResults = resultsFiles.map( ( { file } ) =>
JSON.parse(
fs.readFileSync(
path.join( process.env.WP_ARTIFACTS_PATH, file ),
'utf8'
)
)
);
const data = new TextEncoder().encode(
JSON.stringify( {
branch,
hash,
baseHash,
timestamp,
metrics: resultsFiles.reduce( ( result, { metricsPrefix }, index ) => {
return {
...result,
...Object.fromEntries(
Object.entries(
performanceResults[ index ][ hash ] ?? {}
).map( ( [ key, value ] ) => [
metricsPrefix + key,
value,
] )
),
};
}, {} ),
baseMetrics: resultsFiles.reduce(
( result, { metricsPrefix }, index ) => {
return {
...result,
...Object.fromEntries(
Object.entries(
performanceResults[ index ][ baseHash ] ?? {}
).map( ( [ key, value ] ) => [
metricsPrefix + key,
value,
] )
),
};
},
{}
),
} )
);
const options = {
hostname: 'www.codevitals.run',
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();

View File

@ -7,7 +7,8 @@
"license": "GPLv2",
"repository": "woocommerce/woocommerce",
"scripts": {
"compare": "node index.js"
"compare": "node index.js",
"log": "node log-to-codevitals.js"
},
"dependencies": {
"@wordpress/env": "^8.13.0",