@ -1,9 +1,15 @@
#!/usr/bin/env node
const { promises : fs } = require ( 'fs' )
const path = require ( 'path' )
const request = require ( 'request-promise' )
const VERSION = require ( '../dist/chrome/manifest.json' ) . version // eslint-disable-line import/no-unresolved
start ( ) . catch ( console . error )
function capitalizeFirstLetter ( string ) {
return string . charAt ( 0 ) . toUpperCase ( ) + string . slice ( 1 )
}
async function start ( ) {
const GITHUB _COMMENT _TOKEN = process . env . GITHUB _COMMENT _TOKEN
@ -54,7 +60,92 @@ async function start () {
]
const hiddenContent = ` <ul> ` + contentRows . map ( row => ` <li> ${ row } </li> ` ) . join ( '\n' ) + ` </ul> `
const exposedContent = ` Builds ready [ ${ SHORT _SHA1 } ] `
const commentBody = ` <details><summary> ${ exposedContent } </summary> ${ hiddenContent } </details> `
const artifactsBody = ` <details><summary> ${ exposedContent } </summary> ${ hiddenContent } </details> `
const benchmarkResults = { }
for ( const platform of platforms ) {
const benchmarkPath = path . resolve ( _ _dirname , '..' , path . join ( 'test-artifacts' , platform , 'benchmark' , 'pageload.json' ) )
try {
const data = await fs . readFile ( benchmarkPath , 'utf8' )
const benchmark = JSON . parse ( data )
benchmarkResults [ platform ] = benchmark
} catch ( error ) {
if ( error . code === 'ENOENT' ) {
console . log ( ` No benchmark data found for ${ platform } ; skipping ` )
} else {
console . error ( ` Error encountered processing benchmark data for ' ${ platform } ': ' ${ error } ' ` )
}
}
}
let commentBody
if ( ! benchmarkResults . chrome ) {
console . log ( ` No results for Chrome found; skipping benchmark ` )
commentBody = artifactsBody
} else {
try {
const chromePageLoad = Math . round ( parseFloat ( benchmarkResults . chrome . notification . average . load ) )
const chromePageLoadMarginOfError = Math . round ( parseFloat ( benchmarkResults . chrome . notification . marginOfError . load ) )
const benchmarkSummary = ` Page Load Metrics ( ${ chromePageLoad } ± ${ chromePageLoadMarginOfError } ms) `
const allPlatforms = new Set ( )
const allPages = new Set ( )
const allMetrics = new Set ( )
const allMeasures = new Set ( )
for ( const platform of Object . keys ( benchmarkResults ) ) {
allPlatforms . add ( platform )
const platformBenchmark = benchmarkResults [ platform ]
const pages = Object . keys ( platformBenchmark )
for ( const page of pages ) {
allPages . add ( page )
const pageBenchmark = platformBenchmark [ page ]
const measures = Object . keys ( pageBenchmark )
for ( const measure of measures ) {
allMeasures . add ( measure )
const measureBenchmark = pageBenchmark [ measure ]
const metrics = Object . keys ( measureBenchmark )
for ( const metric of metrics ) {
allMetrics . add ( metric )
}
}
}
}
const tableRows = [ ]
for ( const platform of allPlatforms ) {
const pageRows = [ ]
for ( const page of allPages ) {
const metricRows = [ ]
for ( const metric of allMetrics ) {
let metricData = ` <td> ${ metric } </td> `
for ( const measure of allMeasures ) {
metricData += ` <td align="right"> ${ Math . round ( parseFloat ( benchmarkResults [ platform ] [ page ] [ measure ] [ metric ] ) ) } </td> `
}
metricRows . push ( metricData )
}
metricRows [ 0 ] = ` <td rowspan=" ${ allMetrics . size } "> ${ capitalizeFirstLetter ( page ) } </td> ${ metricRows [ 0 ] } `
pageRows . push ( ... metricRows )
}
pageRows [ 0 ] = ` <td rowspan=" ${ allPages . size * allMetrics . size } "> ${ capitalizeFirstLetter ( platform ) } </td> ${ pageRows [ 0 ] } `
for ( const row of pageRows ) {
tableRows . push ( ` <tr> ${ row } </tr> ` )
}
}
const benchmarkTableHeaders = [ 'Platform' , 'Page' , 'Metric' ]
for ( const measure of allMeasures ) {
benchmarkTableHeaders . push ( ` ${ capitalizeFirstLetter ( measure ) } (ms) ` )
}
const benchmarkTableHeader = ` <thead><tr> ${ benchmarkTableHeaders . map ( header => ` <th> ${ header } </th> ` ) . join ( '' ) } </tr></thead> `
const benchmarkTableBody = ` <tbody> ${ tableRows . join ( '' ) } </tbody> `
const benchmarkTable = ` <table> ${ benchmarkTableHeader } ${ benchmarkTableBody } </table> `
const benchmarkBody = ` <details><summary> ${ benchmarkSummary } </summary> ${ benchmarkTable } </details> `
commentBody = ` ${ artifactsBody } ${ benchmarkBody } `
} catch ( error ) {
console . error ( ` Error constructing benchmark results: ' ${ error } ' ` )
commentBody = artifactsBody
}
}
const JSON _PAYLOAD = JSON . stringify ( { body : commentBody } )
const POST _COMMENT _URI = ` https://api.github.com/repos/metamask/metamask-extension/issues/ ${ CIRCLE _PR _NUMBER } /comments `