From 7a6026fb3d5c8ae7e7213514aa83eb46285dc935 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 9 Jul 2020 19:05:12 +0200 Subject: [PATCH 01/15] Perf Tests Reporter: Run onTestResult --- packages/e2e-tests/config/performance-reporter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/config/performance-reporter.js b/packages/e2e-tests/config/performance-reporter.js index 7353a9597207f..db0eec36fc71d 100644 --- a/packages/e2e-tests/config/performance-reporter.js +++ b/packages/e2e-tests/config/performance-reporter.js @@ -17,7 +17,7 @@ const title = chalk.bold; const success = chalk.bold.green; class PerformanceReporter { - onRunComplete() { + onTestResult() { const path = __dirname + '/../specs/performance/results.json'; if ( ! existsSync( path ) ) { From 40c77cac37a3e654f2860d0c650885bb2f4a300c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 9 Jul 2020 19:17:31 +0200 Subject: [PATCH 02/15] Perf test: Allow multiple test files --- bin/plugin/commands/performance.js | 2 +- packages/e2e-tests/config/performance-reporter.js | 11 +++++++---- ...rmance.test.js => post-editor.performance.test.js} | 0 3 files changed, 8 insertions(+), 5 deletions(-) rename packages/e2e-tests/specs/performance/{performance.test.js => post-editor.performance.test.js} (100%) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index c3b8dbbc9059d..a6a9acb08228f 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -153,7 +153,7 @@ async function getPerformanceResultsForBranch( const rawResults = await readJSONFile( path.join( performanceTestDirectory, - 'packages/e2e-tests/specs/performance/results.json' + 'packages/e2e-tests/specs/performance/post-editor.performance.test.results.json' ) ); results.push( curateResults( rawResults ) ); diff --git a/packages/e2e-tests/config/performance-reporter.js b/packages/e2e-tests/config/performance-reporter.js index db0eec36fc71d..52394092d8ce7 100644 --- a/packages/e2e-tests/config/performance-reporter.js +++ b/packages/e2e-tests/config/performance-reporter.js @@ -2,6 +2,7 @@ * External dependencies */ const { readFileSync, existsSync } = require( 'fs' ); +const path = require( 'path' ); const chalk = require( 'chalk' ); function average( array ) { @@ -17,14 +18,16 @@ const title = chalk.bold; const success = chalk.bold.green; class PerformanceReporter { - onTestResult() { - const path = __dirname + '/../specs/performance/results.json'; + onTestResult( test ) { + const dirname = path.dirname( test.path ); + const basename = path.basename( test.path, '.js' ); + const filepath = path.join( dirname, basename + '.results.json' ); - if ( ! existsSync( path ) ) { + if ( ! existsSync( filepath ) ) { return; } - const results = readFileSync( path, 'utf8' ); + const results = readFileSync( filepath, 'utf8' ); const { load, domcontentloaded, type, focus } = JSON.parse( results ); if ( load && load.length ) { diff --git a/packages/e2e-tests/specs/performance/performance.test.js b/packages/e2e-tests/specs/performance/post-editor.performance.test.js similarity index 100% rename from packages/e2e-tests/specs/performance/performance.test.js rename to packages/e2e-tests/specs/performance/post-editor.performance.test.js From 17328fdb4e36ce0c865b6768cc5bfb82b48c4c51 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 9 Jul 2020 19:18:40 +0200 Subject: [PATCH 03/15] Perf tests: Add basic Site Editor Perf test --- .../site-editor.performance.test.js | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 packages/e2e-tests/specs/performance/site-editor.performance.test.js diff --git a/packages/e2e-tests/specs/performance/site-editor.performance.test.js b/packages/e2e-tests/specs/performance/site-editor.performance.test.js new file mode 100644 index 0000000000000..7c69cb545b59b --- /dev/null +++ b/packages/e2e-tests/specs/performance/site-editor.performance.test.js @@ -0,0 +1,77 @@ +/** + * External dependencies + */ +import { writeFileSync } from 'fs'; + +/** + * Internal dependencies + */ +import { useExperimentalFeatures } from '../../experimental-features'; + +/** + * WordPress dependencies + */ +import { trashAllPosts, visitAdminPage } from '@wordpress/e2e-test-utils'; +import { addQueryArgs } from '@wordpress/url'; + +jest.setTimeout( 1000000 ); + +describe( 'Site Editor Performance', () => { + useExperimentalFeatures( [ + '#gutenberg-full-site-editing', + '#gutenberg-full-site-editing-demo', + ] ); + + beforeAll( async () => { + await trashAllPosts( 'wp_template' ); + await trashAllPosts( 'wp_template_part' ); + } ); + afterAll( async () => { + await trashAllPosts( 'wp_template' ); + await trashAllPosts( 'wp_template_part' ); + } ); + + it( 'Loading', async () => { + const results = { + load: [], + domcontentloaded: [], + type: [], + focus: [], + }; + + visitAdminPage( + 'admin.php', + addQueryArgs( '', { + page: 'gutenberg-edit-site', + } ).slice( 1 ) + ); + + let i = 1; + + // Measuring loading time + while ( i-- ) { + await page.reload( { waitUntil: [ 'domcontentloaded', 'load' ] } ); + const timings = JSON.parse( + await page.evaluate( () => + JSON.stringify( window.performance.timing ) + ) + ); + const { + navigationStart, + domContentLoadedEventEnd, + loadEventEnd, + } = timings; + results.load.push( loadEventEnd - navigationStart ); + results.domcontentloaded.push( + domContentLoadedEventEnd - navigationStart + ); + } + + writeFileSync( + __dirname + '/site-editor.performance.test.results.json', + JSON.stringify( results, null, 2 ) + ); + + expect( true ).toBe( true ); + } ); +} ); From d2de7a766c9b58efc316ab9776e90428d9f1156d Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Jul 2020 13:29:46 +0200 Subject: [PATCH 04/15] Perf test: Fix post editor results filename --- .../e2e-tests/specs/performance/post-editor.performance.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/performance/post-editor.performance.test.js b/packages/e2e-tests/specs/performance/post-editor.performance.test.js index 9360c98a6d313..e3f84c9e9d52e 100644 --- a/packages/e2e-tests/specs/performance/post-editor.performance.test.js +++ b/packages/e2e-tests/specs/performance/post-editor.performance.test.js @@ -183,7 +183,7 @@ describe( 'Performance', () => { results.focus = focusEvents; writeFileSync( - __dirname + '/results.json', + __dirname + '/post-editor.performance.test.results.json', JSON.stringify( results, null, 2 ) ); From aab0fe502c856f211663b4d890e48ad80c039b4b Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Jul 2020 15:10:36 +0200 Subject: [PATCH 05/15] Change perf script to show post and site editor results --- bin/plugin/commands/performance.js | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index a6a9acb08228f..d5dcd9d72cb9a 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -119,6 +119,7 @@ function curateResults( results ) { * * @param {string} performanceTestDirectory Path to the performance tests' clone. * @param {string} environmentDirectory Path to the plugin environment's clone. + * @param {string} testSuite Name of the tests set. * @param {string} branch Branch name. * * @return {Promise} Performance results for the branch. @@ -126,6 +127,7 @@ function curateResults( results ) { async function getPerformanceResultsForBranch( performanceTestDirectory, environmentDirectory, + testSuite, branch ) { // Restore clean working directory (e.g. if `package-lock.json` has local @@ -147,13 +149,13 @@ async function getPerformanceResultsForBranch( const results = []; for ( let i = 0; i < 3; i++ ) { await runShellScript( - 'npm run test-performance', + `npm run test-performance -- packages/e2e-tests/specs/performance/${ testSuite }.performance.test.js`, performanceTestDirectory ); const rawResults = await readJSONFile( path.join( performanceTestDirectory, - 'packages/e2e-tests/specs/performance/post-editor.performance.test.results.json' + `packages/e2e-tests/specs/performance/${ testSuite }.performance.test.results.json` ) ); results.push( curateResults( rawResults ) ); @@ -220,21 +222,32 @@ async function runPerformanceTests( branches, options ) { log( '>> Starting the WordPress environment' ); await runShellScript( 'npm run wp-env start', environmentDirectory ); - /** @type {Record} */ + const testSuites = [ 'post-editor', 'site-editor' ]; + + /** @type {Record>} */ const results = {}; - for ( const branch of branches ) { - results[ branch ] = await getPerformanceResultsForBranch( - performanceTestDirectory, - environmentDirectory, - branch - ); + for ( const testSuite of testSuites ) { + results[ testSuite ] = {}; + for ( const branch of branches ) { + results[ testSuite ][ + branch + ] = await getPerformanceResultsForBranch( + performanceTestDirectory, + environmentDirectory, + testSuite, + branch + ); + } } log( '>> Stopping the WordPress environment' ); await runShellScript( 'npm run wp-env stop', environmentDirectory ); log( '\n>> 🎉 Results.\n' ); - console.table( results ); + for ( const testSuite of testSuites ) { + log( `\n>> ${ testSuite }\n` ); + console.table( results[ testSuite ] ); + } } module.exports = { From 374b29abe22e29a2047dc440c47d0f1583b65d9f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 13 Jul 2020 17:16:32 +0200 Subject: [PATCH 06/15] Rename test suite --- .../e2e-tests/specs/performance/post-editor.performance.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/performance/post-editor.performance.test.js b/packages/e2e-tests/specs/performance/post-editor.performance.test.js index e3f84c9e9d52e..d9c6c4adc45f4 100644 --- a/packages/e2e-tests/specs/performance/post-editor.performance.test.js +++ b/packages/e2e-tests/specs/performance/post-editor.performance.test.js @@ -71,7 +71,7 @@ function getSelectionEventDurations( trace ) { jest.setTimeout( 1000000 ); -describe( 'Performance', () => { +describe( 'Post Editor Performance', () => { it( 'Loading, typing and selecting blocks', async () => { const results = { load: [], From a39f46d168a855ead81c5af2f0f0cb1e3d8eab71 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 14 Jul 2020 14:03:39 +0200 Subject: [PATCH 07/15] Drop .performance infix --- bin/plugin/commands/performance.js | 4 ++-- ...{post-editor.performance.test.js => post-editor.test.js} | 6 ++++-- ...{site-editor.performance.test.js => site-editor.test.js} | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) rename packages/e2e-tests/specs/performance/{post-editor.performance.test.js => post-editor.test.js} (96%) rename packages/e2e-tests/specs/performance/{site-editor.performance.test.js => site-editor.test.js} (91%) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index d5dcd9d72cb9a..dcc44f2c54eb1 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -149,13 +149,13 @@ async function getPerformanceResultsForBranch( const results = []; for ( let i = 0; i < 3; i++ ) { await runShellScript( - `npm run test-performance -- packages/e2e-tests/specs/performance/${ testSuite }.performance.test.js`, + `npm run test-performance -- packages/e2e-tests/specs/performance/${ testSuite }.test.js`, performanceTestDirectory ); const rawResults = await readJSONFile( path.join( performanceTestDirectory, - `packages/e2e-tests/specs/performance/${ testSuite }.performance.test.results.json` + `packages/e2e-tests/specs/performance/${ testSuite }.test.results.json` ) ); results.push( curateResults( rawResults ) ); diff --git a/packages/e2e-tests/specs/performance/post-editor.performance.test.js b/packages/e2e-tests/specs/performance/post-editor.test.js similarity index 96% rename from packages/e2e-tests/specs/performance/post-editor.performance.test.js rename to packages/e2e-tests/specs/performance/post-editor.test.js index d9c6c4adc45f4..f007512a0c234 100644 --- a/packages/e2e-tests/specs/performance/post-editor.performance.test.js +++ b/packages/e2e-tests/specs/performance/post-editor.test.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { join } from 'path'; +import { basename, join } from 'path'; import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs'; /** @@ -182,8 +182,10 @@ describe( 'Post Editor Performance', () => { const [ focusEvents ] = getSelectionEventDurations( traceResults ); results.focus = focusEvents; + const resultsFilename = basename( __filename, '.js' ) + '.results.json'; + writeFileSync( - __dirname + '/post-editor.performance.test.results.json', + join( __dirname, resultsFilename ), JSON.stringify( results, null, 2 ) ); diff --git a/packages/e2e-tests/specs/performance/site-editor.performance.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js similarity index 91% rename from packages/e2e-tests/specs/performance/site-editor.performance.test.js rename to packages/e2e-tests/specs/performance/site-editor.test.js index 7c69cb545b59b..f734cd87b3155 100644 --- a/packages/e2e-tests/specs/performance/site-editor.performance.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -1,6 +1,7 @@ /** * External dependencies */ +import { basename, join } from 'path'; import { writeFileSync } from 'fs'; /** @@ -67,8 +68,10 @@ describe( 'Site Editor Performance', () => { ); } + const resultsFilename = basename( __filename, '.js' ) + '.results.json'; + writeFileSync( - __dirname + '/site-editor.performance.test.results.json', + join( __dirname, resultsFilename ), JSON.stringify( results, null, 2 ) ); From 3b314bf757313979207918becb31370e703832bd Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 16:25:36 +0200 Subject: [PATCH 08/15] Add --tests-branch option, use in GH Action --- .github/workflows/performance.yml | 2 +- bin/plugin/cli.js | 4 ++++ bin/plugin/commands/performance.js | 16 +++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.github/workflows/performance.yml b/.github/workflows/performance.yml index 617d603ab0b30..37a729d7a1eaf 100644 --- a/.github/workflows/performance.yml +++ b/.github/workflows/performance.yml @@ -36,4 +36,4 @@ jobs: npm ci - name: Run the performance tests - run: ./bin/plugin/cli.js perf --ci $GITHUB_SHA master + run: ./bin/plugin/cli.js perf --ci $GITHUB_SHA master --tests-branch $GITHUB_SHA diff --git a/bin/plugin/cli.js b/bin/plugin/cli.js index 00809b5054016..dc3a8cf65e620 100755 --- a/bin/plugin/cli.js +++ b/bin/plugin/cli.js @@ -58,6 +58,10 @@ program .command( 'performance-tests [branches...]' ) .alias( 'perf' ) .option( '-c, --ci', 'Run in CI (non interactive)' ) + .option( + '--tests-branch ', + "Use this branch's performance test files" + ) .description( 'Runs performance tests on two separate branches and outputs the result' ) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index dcc44f2c54eb1..669898a6a4908 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -19,7 +19,8 @@ const config = require( '../config' ); /** * @typedef WPPerformanceCommandOptions * - * @property {boolean=} ci Run on CI. + * @property {boolean=} ci Run on CI. + * @property {string=} testsBranch The branch whose performance test files will be used for testing. */ /** @@ -200,6 +201,19 @@ async function runPerformanceTests( branches, options ) { log( '>> Cloning the repository' ); const performanceTestDirectory = await git.clone( config.gitRepositoryURL ); + + if ( !! options.testsBranch ) { + log( + '>> Fetching the ' + + formats.success( options.testsBranch ) + + ' branch' + ); + await git.checkoutRemoteBranch( + performanceTestDirectory, + options.testsBranch + ); + } + const environmentDirectory = getRandomTemporaryPath(); log( '>> Perf Tests Directory : ' + From ccc62dae03b9c3c5efe1472f40b6528fef421ae7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 16:49:15 +0200 Subject: [PATCH 09/15] Document --tests-branch option --- docs/contributors/testing-overview.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/contributors/testing-overview.md b/docs/contributors/testing-overview.md index 1b1150273bca2..8c3a99a417594 100644 --- a/docs/contributors/testing-overview.md +++ b/docs/contributors/testing-overview.md @@ -471,4 +471,10 @@ In addition to that, you can also compare the metrics across branches (or tags o ./bin/plugin/cli.js perf master v8.1.0 v8.0.0 ``` +Finally, you can pass an additional `--tests-branch` argument to specify which branch's performance test files you'd like to run. This is particularly useful when modifying/extending the perf tests: + +``` +./bin/plugin/cli.js perf master v8.1.0 v8.0.0 --tests-branch add/perf-tests-coverage +``` + **Note** This command needs may take some time to perform the benchmark. While running make sure to avoid using your computer or have a lot of background process to minimize external factors that can impact the results across branches. From 48d1b3de2263f3a121716c9c4b1531251ecffb3c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 17:33:18 +0200 Subject: [PATCH 10/15] Make average() empty array-proof --- bin/plugin/commands/performance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index 669898a6a4908..462aee6371dc2 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -65,7 +65,7 @@ const config = require( '../config' ); * @return {number} Average. */ function average( array ) { - return array.reduce( ( a, b ) => a + b ) / array.length; + return array.reduce( ( a, b ) => a + b, 0 ) / array.length; } /** From 41f805712824efbee06ff0c1243bbc65665c9e1c Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 21:11:45 +0200 Subject: [PATCH 11/15] await visitAdminPage --- packages/e2e-tests/specs/performance/site-editor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index f734cd87b3155..78b3eb4616f4a 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -40,7 +40,7 @@ describe( 'Site Editor Performance', () => { focus: [], }; - visitAdminPage( + await visitAdminPage( 'admin.php', addQueryArgs( '', { page: 'gutenberg-edit-site', From db31da884b4b4a892c619757a44a8450b5249e17 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 21:17:16 +0200 Subject: [PATCH 12/15] Load the Site Editor 3 times --- packages/e2e-tests/specs/performance/site-editor.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/e2e-tests/specs/performance/site-editor.test.js b/packages/e2e-tests/specs/performance/site-editor.test.js index 78b3eb4616f4a..da62661f07d88 100644 --- a/packages/e2e-tests/specs/performance/site-editor.test.js +++ b/packages/e2e-tests/specs/performance/site-editor.test.js @@ -47,7 +47,7 @@ describe( 'Site Editor Performance', () => { } ).slice( 1 ) ); - let i = 1; + let i = 3; // Measuring loading time while ( i-- ) { From 5ac6ec40e51b981fa96286b6e6ef9b9b02c51bdf Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 22:31:23 +0200 Subject: [PATCH 13/15] Remove non-finite columns --- bin/plugin/commands/performance.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index 462aee6371dc2..2ff23efaa8aa5 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -2,6 +2,7 @@ * External dependencies */ const path = require( 'path' ); +const { pickBy } = require( 'lodash' ); /** * Internal dependencies @@ -47,14 +48,14 @@ const config = require( '../config' ); /** * @typedef WPFormattedPerformanceResults * - * @property {string} load Load Time. - * @property {string} domcontentloaded DOM Contentloaded time. - * @property {string} type Average type time. - * @property {string} minType Minium type time. - * @property {string} maxType Maximum type time. - * @property {string} focus Average block selection time. - * @property {string} minFocus Min block selection time. - * @property {string} maxFocus Max block selection time. + * @property {string=} load Load Time. + * @property {string=} domcontentloaded DOM Contentloaded time. + * @property {string=} type Average type time. + * @property {string=} minType Minium type time. + * @property {string=} maxType Maximum type time. + * @property {string=} focus Average block selection time. + * @property {string=} minFocus Min block selection time. + * @property {string=} maxFocus Max block selection time. */ /** @@ -162,7 +163,7 @@ async function getPerformanceResultsForBranch( results.push( curateResults( rawResults ) ); } - return { + const formattedResults = { load: formatTime( median( results.map( ( r ) => r.load ) ) ), domcontentloaded: formatTime( median( results.map( ( r ) => r.domcontentloaded ) ) @@ -174,6 +175,9 @@ async function getPerformanceResultsForBranch( minFocus: formatTime( median( results.map( ( r ) => r.minFocus ) ) ), maxFocus: formatTime( median( results.map( ( r ) => r.maxFocus ) ) ), }; + + // Remove results for which we don't have data (and where the statistical functions thus returned NaN or Infinity etc). + return pickBy( formattedResults, isFinite ); } /** From 8f9b0212f02a155aa1a52903047816c427dc888a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 22:43:18 +0200 Subject: [PATCH 14/15] Oops, need to pick before formatting --- bin/plugin/commands/performance.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index 2ff23efaa8aa5..e5aac8f1a7078 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -2,7 +2,7 @@ * External dependencies */ const path = require( 'path' ); -const { pickBy } = require( 'lodash' ); +const { pickBy, mapValues } = require( 'lodash' ); /** * Internal dependencies @@ -163,21 +163,21 @@ async function getPerformanceResultsForBranch( results.push( curateResults( rawResults ) ); } - const formattedResults = { - load: formatTime( median( results.map( ( r ) => r.load ) ) ), - domcontentloaded: formatTime( - median( results.map( ( r ) => r.domcontentloaded ) ) - ), - type: formatTime( median( results.map( ( r ) => r.type ) ) ), - minType: formatTime( median( results.map( ( r ) => r.minType ) ) ), - maxType: formatTime( median( results.map( ( r ) => r.maxType ) ) ), - focus: formatTime( median( results.map( ( r ) => r.focus ) ) ), - minFocus: formatTime( median( results.map( ( r ) => r.minFocus ) ) ), - maxFocus: formatTime( median( results.map( ( r ) => r.maxFocus ) ) ), + const medians = { + load: median( results.map( ( r ) => r.load ) ), + domcontentloaded: median( results.map( ( r ) => r.domcontentloaded ) ), + type: median( results.map( ( r ) => r.type ) ), + minType: median( results.map( ( r ) => r.minType ) ), + maxType: median( results.map( ( r ) => r.maxType ) ), + focus: median( results.map( ( r ) => r.focus ) ), + minFocus: median( results.map( ( r ) => r.minFocus ) ), + maxFocus: median( results.map( ( r ) => r.maxFocus ) ), }; // Remove results for which we don't have data (and where the statistical functions thus returned NaN or Infinity etc). - return pickBy( formattedResults, isFinite ); + const finiteMedians = pickBy( medians, isFinite ); + // Format results as times. + return mapValues( finiteMedians, formatTime ); } /** From 164611c910eb57e9b2a87a22a55154f6e8255970 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 15 Jul 2020 22:46:05 +0200 Subject: [PATCH 15/15] Fun with mapValues --- bin/plugin/commands/performance.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/bin/plugin/commands/performance.js b/bin/plugin/commands/performance.js index e5aac8f1a7078..421989173f44e 100644 --- a/bin/plugin/commands/performance.js +++ b/bin/plugin/commands/performance.js @@ -163,16 +163,19 @@ async function getPerformanceResultsForBranch( results.push( curateResults( rawResults ) ); } - const medians = { - load: median( results.map( ( r ) => r.load ) ), - domcontentloaded: median( results.map( ( r ) => r.domcontentloaded ) ), - type: median( results.map( ( r ) => r.type ) ), - minType: median( results.map( ( r ) => r.minType ) ), - maxType: median( results.map( ( r ) => r.maxType ) ), - focus: median( results.map( ( r ) => r.focus ) ), - minFocus: median( results.map( ( r ) => r.minFocus ) ), - maxFocus: median( results.map( ( r ) => r.maxFocus ) ), - }; + const medians = mapValues( + { + load: results.map( ( r ) => r.load ), + domcontentloaded: results.map( ( r ) => r.domcontentloaded ), + type: results.map( ( r ) => r.type ), + minType: results.map( ( r ) => r.minType ), + maxType: results.map( ( r ) => r.maxType ), + focus: results.map( ( r ) => r.focus ), + minFocus: results.map( ( r ) => r.minFocus ), + maxFocus: results.map( ( r ) => r.maxFocus ), + }, + median + ); // Remove results for which we don't have data (and where the statistical functions thus returned NaN or Infinity etc). const finiteMedians = pickBy( medians, isFinite );