From 743b26d4981e51cc64a5a10cd286a5a9e886739f Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 14 Dec 2023 14:27:42 +0100 Subject: [PATCH 1/3] Performance: Log the performance metrics to codevitals --- .github/workflows/metrics.yml | 8 +++ tools/compare-perf/log-to-codevitals.js | 86 +++++++++++++++++++++++++ tools/compare-perf/package.json | 3 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tools/compare-perf/log-to-codevitals.js diff --git a/.github/workflows/metrics.yml b/.github/workflows/metrics.yml index da5de8e5312..4e3db893dd1 100644 --- a/.github/workflows/metrics.yml +++ b/.github/workflows/metrics.yml @@ -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 == 'pull_request' + 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 2f6ca66e00b3666b2567877ae67cbfb5b6ce171a $COMMITTED_AT diff --git a/tools/compare-perf/log-to-codevitals.js b/tools/compare-perf/log-to-codevitals.js new file mode 100644 index 00000000000..bf772f7d384 --- /dev/null +++ b/tools/compare-perf/log-to-codevitals.js @@ -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: '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(); diff --git a/tools/compare-perf/package.json b/tools/compare-perf/package.json index 77c193c56ff..36ae6fe2746 100644 --- a/tools/compare-perf/package.json +++ b/tools/compare-perf/package.json @@ -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", From 28e876e7cd40b58dcb7ec21d1feace63764c1163 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 14 Dec 2023 14:56:17 +0100 Subject: [PATCH 2/3] Fix URL --- tools/compare-perf/log-to-codevitals.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/compare-perf/log-to-codevitals.js b/tools/compare-perf/log-to-codevitals.js index bf772f7d384..cc6823c2f9a 100644 --- a/tools/compare-perf/log-to-codevitals.js +++ b/tools/compare-perf/log-to-codevitals.js @@ -60,7 +60,7 @@ const data = new TextEncoder().encode( ); const options = { - hostname: 'codevitals.run', + hostname: 'www.codevitals.run', port: 443, path: '/api/log?token=' + token, method: 'POST', From e1b7c42256af492773fa3e26f584e3945b065ab6 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 14 Dec 2023 15:33:42 +0100 Subject: [PATCH 3/3] try sending the right hashes --- .github/workflows/metrics.yml | 4 ++-- tools/compare-perf/log-to-codevitals.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/metrics.yml b/.github/workflows/metrics.yml index 4e3db893dd1..61d5f3efede 100644 --- a/.github/workflows/metrics.yml +++ b/.github/workflows/metrics.yml @@ -53,9 +53,9 @@ jobs: path: ${{ env.WP_ARTIFACTS_PATH }}/*.performance-results*.json - name: Publish performance results - if: github.event_name == 'pull_request' + 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 2f6ca66e00b3666b2567877ae67cbfb5b6ce171a $COMMITTED_AT + cd tools/compare-perf && pnpm run log $CODEVITALS_PROJECT_TOKEN trunk $GITHUB_SHA 19f3d0884617d7ecdcf37664f648a51e2987cada $COMMITTED_AT diff --git a/tools/compare-perf/log-to-codevitals.js b/tools/compare-perf/log-to-codevitals.js index cc6823c2f9a..3b4e31a6a1e 100644 --- a/tools/compare-perf/log-to-codevitals.js +++ b/tools/compare-perf/log-to-codevitals.js @@ -8,7 +8,7 @@ const [ token, branch, hash, baseHash, timestamp ] = process.argv.slice( 2 ); const resultsFiles = [ { file: 'editor.performance-results.json', - metricsPrefix: 'editor', + metricsPrefix: 'editor-', }, ];