diff --git a/.github/workflows/metrics.yml b/.github/workflows/metrics.yml index da5de8e5312..61d5f3efede 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 == '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 diff --git a/tools/compare-perf/log-to-codevitals.js b/tools/compare-perf/log-to-codevitals.js new file mode 100644 index 00000000000..3b4e31a6a1e --- /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: '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(); 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",