-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
performance.js
551 lines (502 loc) · 18.5 KB
/
performance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
* External dependencies
*/
const fs = require( 'fs' );
const path = require( 'path' );
const SimpleGit = require( 'simple-git' );
/**
* Internal dependencies
*/
const { formats, log } = require( '../lib/logger' );
const {
runShellScript,
readJSONFile,
askForConfirmation,
getRandomTemporaryPath,
} = require( '../lib/utils' );
const config = require( '../config' );
const ARTIFACTS_PATH =
process.env.WP_ARTIFACTS_PATH || path.join( process.cwd(), 'artifacts' );
/**
* @typedef WPPerformanceCommandOptions
*
* @property {boolean=} ci Run on CI.
* @property {number=} rounds Run each test suite this many times for each branch.
* @property {string=} testsBranch The branch whose performance test files will be used for testing.
* @property {string=} wpVersion The WordPress version to be used as the base install for testing.
*/
/**
* @typedef WPRawPerformanceResults
*
* @property {number[]} timeToFirstByte Represents the time since the browser started the request until it received a response.
* @property {number[]} largestContentfulPaint Represents the time when the main content of the page has likely loaded.
* @property {number[]} lcpMinusTtfb Represents the difference between LCP and TTFB.
* @property {number[]} serverResponse Represents the time the server takes to respond.
* @property {number[]} firstPaint Represents the time when the user agent first rendered after navigation.
* @property {number[]} domContentLoaded Represents the time immediately after the document's DOMContentLoaded event completes.
* @property {number[]} loaded Represents the time when the load event of the current document is completed.
* @property {number[]} firstContentfulPaint Represents the time when the browser first renders any text or media.
* @property {number[]} firstBlock Represents the time when Puppeteer first sees a block selector in the DOM.
* @property {number[]} type Average type time.
* @property {number[]} typeContainer Average type time within a container.
* @property {number[]} focus Average block selection time.
* @property {number[]} inserterOpen Average time to open global inserter.
* @property {number[]} inserterSearch Average time to search the inserter.
* @property {number[]} inserterHover Average time to move mouse between two block item in the inserter.
* @property {number[]} listViewOpen Average time to open listView
*/
/**
* @typedef WPPerformanceResults
*
* @property {number=} timeToFirstByte Represents the time since the browser started the request until it received a response.
* @property {number=} largestContentfulPaint Represents the time when the main content of the page has likely loaded.
* @property {number=} lcpMinusTtfb Represents the difference between LCP and TTFB.
* @property {number=} serverResponse Represents the time the server takes to respond.
* @property {number=} firstPaint Represents the time when the user agent first rendered after navigation.
* @property {number=} domContentLoaded Represents the time immediately after the document's DOMContentLoaded event completes.
* @property {number=} loaded Represents the time when the load event of the current document is completed.
* @property {number=} firstContentfulPaint Represents the time when the browser first renders any text or media.
* @property {number=} firstBlock Represents the time when Puppeteer first sees a block selector in the DOM.
* @property {number=} type Average type time.
* @property {number=} minType Minimum type time.
* @property {number=} maxType Maximum type time.
* @property {number=} typeContainer Average type time within a container.
* @property {number=} minTypeContainer Minimum type time within a container.
* @property {number=} maxTypeContainer Maximum type time within a container.
* @property {number=} focus Average block selection time.
* @property {number=} minFocus Min block selection time.
* @property {number=} maxFocus Max block selection time.
* @property {number=} inserterOpen Average time to open global inserter.
* @property {number=} minInserterOpen Min time to open global inserter.
* @property {number=} maxInserterOpen Max time to open global inserter.
* @property {number=} inserterSearch Average time to open global inserter.
* @property {number=} minInserterSearch Min time to open global inserter.
* @property {number=} maxInserterSearch Max time to open global inserter.
* @property {number=} inserterHover Average time to move mouse between two block item in the inserter.
* @property {number=} minInserterHover Min time to move mouse between two block item in the inserter.
* @property {number=} maxInserterHover Max time to move mouse between two block item in the inserter.
* @property {number=} listViewOpen Average time to open list view.
* @property {number=} minListViewOpen Min time to open list view.
* @property {number=} maxListViewOpen Max time to open list view.
*/
/**
* Sanitizes branch name to be used in a path or a filename.
*
* @param {string} branch
*
* @return {string} Sanitized branch name.
*/
function sanitizeBranchName( branch ) {
return branch.replace( /[^a-zA-Z0-9-]/g, '-' );
}
/**
* Computes the average number from an array numbers.
*
* @param {number[]} array
*
* @return {number} Average.
*/
function average( array ) {
return array.reduce( ( a, b ) => a + b, 0 ) / array.length;
}
/**
* Computes the median number from an array numbers.
*
* @param {number[]} array
*
* @return {number} Median.
*/
function median( array ) {
const mid = Math.floor( array.length / 2 ),
numbers = [ ...array ].sort( ( a, b ) => a - b );
return array.length % 2 !== 0
? numbers[ mid ]
: ( numbers[ mid - 1 ] + numbers[ mid ] ) / 2;
}
/**
* Rounds and format a time passed in milliseconds.
*
* @param {number} number
*
* @return {number} Formatted time.
*/
function formatTime( number ) {
const factor = Math.pow( 10, 2 );
return Math.round( number * factor ) / factor;
}
/**
* Curate the raw performance results.
*
* @param {string} testSuite
* @param {WPRawPerformanceResults} results
*
* @return {WPPerformanceResults} Curated Performance results.
*/
function curateResults( testSuite, results ) {
if (
testSuite === 'front-end-classic-theme' ||
testSuite === 'front-end-block-theme'
) {
return {
timeToFirstByte: median( results.timeToFirstByte ),
largestContentfulPaint: median( results.largestContentfulPaint ),
lcpMinusTtfb: median( results.lcpMinusTtfb ),
};
}
return {
serverResponse: average( results.serverResponse ),
firstPaint: average( results.firstPaint ),
domContentLoaded: average( results.domContentLoaded ),
loaded: average( results.loaded ),
firstContentfulPaint: average( results.firstContentfulPaint ),
firstBlock: average( results.firstBlock ),
type: average( results.type ),
minType: Math.min( ...results.type ),
maxType: Math.max( ...results.type ),
typeContainer: average( results.typeContainer ),
minTypeContainer: Math.min( ...results.typeContainer ),
maxTypeContainer: Math.max( ...results.typeContainer ),
focus: average( results.focus ),
minFocus: Math.min( ...results.focus ),
maxFocus: Math.max( ...results.focus ),
inserterOpen: average( results.inserterOpen ),
minInserterOpen: Math.min( ...results.inserterOpen ),
maxInserterOpen: Math.max( ...results.inserterOpen ),
inserterSearch: average( results.inserterSearch ),
minInserterSearch: Math.min( ...results.inserterSearch ),
maxInserterSearch: Math.max( ...results.inserterSearch ),
inserterHover: average( results.inserterHover ),
minInserterHover: Math.min( ...results.inserterHover ),
maxInserterHover: Math.max( ...results.inserterHover ),
listViewOpen: average( results.listViewOpen ),
minListViewOpen: Math.min( ...results.listViewOpen ),
maxListViewOpen: Math.max( ...results.listViewOpen ),
};
}
/**
* Runs the performance tests on the current branch.
*
* @param {string} testSuite Name of the tests set.
* @param {string} performanceTestDirectory Path to the performance tests' clone.
* @param {string} runKey Unique identifier for the test run, e.g. `branch-name_post-editor_run-3`.
*
* @return {Promise<WPPerformanceResults>} Performance results for the branch.
*/
async function runTestSuite( testSuite, performanceTestDirectory, runKey ) {
const resultsFilename = `${ runKey }.performance-results.json`;
await runShellScript(
`npm run test:performance -- ${ testSuite }`,
performanceTestDirectory,
{
...process.env,
WP_ARTIFACTS_PATH: ARTIFACTS_PATH,
RESULTS_FILENAME: resultsFilename,
}
);
return curateResults(
testSuite,
await readJSONFile( path.join( ARTIFACTS_PATH, resultsFilename ) )
);
}
/**
* Runs the performances tests on an array of branches and output the result.
*
* @param {string[]} branches Branches to compare
* @param {WPPerformanceCommandOptions} options Command options.
*/
async function runPerformanceTests( branches, options ) {
const runningInCI = !! process.env.CI || !! options.ci;
const TEST_ROUNDS = options.rounds || 1;
// The default value doesn't work because commander provides an array.
if ( branches.length === 0 ) {
branches = [ 'trunk' ];
}
log(
formats.title( '\n💃 Performance Tests 🕺\n' ),
'\nWelcome! This tool runs the performance tests on multiple branches and displays a comparison table.\n' +
'In order to run the tests, the tool is going to load a WordPress environment on ports 8888 and 8889.\n' +
'Make sure these ports are not used before continuing.\n'
);
if ( ! runningInCI ) {
await askForConfirmation( 'Ready to go? ' );
}
// 1- Preparing the tests directory.
log( '\n>> Preparing the tests directories' );
log( ' >> Cloning the repository' );
/**
* @type {string[]} git refs against which to run tests;
* could be commit SHA, branch name, tag, etc...
*/
if ( branches.length < 2 ) {
throw new Error( `Need at least two git refs to run` );
}
const baseDirectory = getRandomTemporaryPath();
fs.mkdirSync( baseDirectory, { recursive: true } );
// @ts-ignore
const git = SimpleGit( baseDirectory );
await git
.raw( 'init' )
.raw( 'remote', 'add', 'origin', config.gitRepositoryURL );
for ( const branch of branches ) {
await git.raw( 'fetch', '--depth=1', 'origin', branch );
}
await git.raw( 'checkout', branches[ 0 ] );
const rootDirectory = getRandomTemporaryPath();
const performanceTestDirectory = rootDirectory + '/tests';
await runShellScript( 'mkdir -p ' + rootDirectory );
await runShellScript(
'cp -R ' + baseDirectory + ' ' + performanceTestDirectory
);
if ( !! options.testsBranch ) {
const branchName = formats.success( options.testsBranch );
log( ` >> Fetching the test-runner branch: ${ branchName }` );
// @ts-ignore
await SimpleGit( performanceTestDirectory )
.raw( 'fetch', '--depth=1', 'origin', options.testsBranch )
.raw( 'checkout', options.testsBranch );
}
log( ' >> Installing dependencies and building packages' );
await runShellScript(
'npm ci && node ./bin/packages/build.js',
performanceTestDirectory
);
log( ' >> Creating the environment folders' );
await runShellScript( 'mkdir -p ' + rootDirectory + '/envs' );
// 2- Preparing the environment directories per branch.
log( '\n>> Preparing an environment directory per branch' );
const branchDirectories = {};
for ( const branch of branches ) {
log( ` >> Branch: ${ branch }` );
const sanitizedBranch = sanitizeBranchName( branch );
const environmentDirectory = rootDirectory + '/envs/' + sanitizedBranch;
// @ts-ignore
branchDirectories[ branch ] = environmentDirectory;
const buildPath = `${ environmentDirectory }/plugin`;
await runShellScript( 'mkdir ' + environmentDirectory );
await runShellScript( `cp -R ${ baseDirectory } ${ buildPath }` );
const fancyBranch = formats.success( branch );
if ( branch === options.testsBranch ) {
log(
` >> Re-using the testing branch for ${ fancyBranch }`
);
await runShellScript(
`cp -R ${ performanceTestDirectory } ${ buildPath }`
);
} else {
log( ` >> Fetching the ${ fancyBranch } branch` );
// @ts-ignore
await SimpleGit( buildPath ).reset( 'hard' ).checkout( branch );
}
log( ` >> Building the ${ fancyBranch } branch` );
await runShellScript(
'npm ci && npm run prebuild:packages && node ./bin/packages/build.js && npx wp-scripts build',
buildPath
);
// Create the config file for the current env.
fs.writeFileSync(
path.join( environmentDirectory, '.wp-env.json' ),
JSON.stringify(
{
config: {
WP_DEBUG: false,
SCRIPT_DEBUG: false,
},
core: 'WordPress/WordPress',
plugins: [ path.join( environmentDirectory, 'plugin' ) ],
themes: [
path.join(
performanceTestDirectory,
'test/emptytheme'
),
],
env: {
tests: {
mappings: {
'wp-content/mu-plugins': path.join(
performanceTestDirectory,
'packages/e2e-tests/mu-plugins'
),
'wp-content/plugins/gutenberg-test-plugins':
path.join(
performanceTestDirectory,
'packages/e2e-tests/plugins'
),
'wp-content/themes/gutenberg-test-themes':
path.join(
performanceTestDirectory,
'test/gutenberg-test-themes'
),
'wp-content/themes/gutenberg-test-themes/twentytwentyone':
'https://downloads.wordpress.org/theme/twentytwentyone.1.7.zip',
'wp-content/themes/gutenberg-test-themes/twentytwentythree':
'https://downloads.wordpress.org/theme/twentytwentythree.1.0.zip',
},
},
},
},
null,
2
),
'utf8'
);
if ( options.wpVersion ) {
// In order to match the topology of ZIP files at wp.org, remap .0
// patch versions to major versions:
//
// 5.7 -> 5.7 (unchanged)
// 5.7.0 -> 5.7 (changed)
// 5.7.2 -> 5.7.2 (unchanged)
const zipVersion = options.wpVersion.replace(
/^(\d+\.\d+).0/,
'$1'
);
const zipUrl = `https://wordpress.org/wordpress-${ zipVersion }.zip`;
log( ` Using WordPress version ${ zipVersion }` );
// Patch the environment's .wp-env.json config to use the specified WP
// version:
//
// {
// "core": "https://wordpress.org/wordpress-$VERSION.zip",
// ...
// }
const confPath = `${ environmentDirectory }/.wp-env.json`;
const conf = { ...readJSONFile( confPath ), core: zipUrl };
await fs.writeFileSync(
confPath,
JSON.stringify( conf, null, 2 ),
'utf8'
);
}
}
// 3- Printing the used folders.
log(
'\n>> Perf Tests Directory : ' +
formats.success( performanceTestDirectory )
);
for ( const branch of branches ) {
// @ts-ignore
const envPath = formats.success( branchDirectories[ branch ] );
log( `>> Environment Directory (${ branch }) : ${ envPath }` );
}
// 4- Running the tests.
log( '\n>> Running the tests' );
const testSuites = [
'post-editor',
'site-editor',
'front-end-classic-theme',
'front-end-block-theme',
];
/** @type {Record<string,Record<string, WPPerformanceResults>>} */
const results = {};
const wpEnvPath = path.join(
performanceTestDirectory,
'node_modules/.bin/wp-env'
);
for ( const testSuite of testSuites ) {
results[ testSuite ] = {};
/** @type {Array<Record<string, WPPerformanceResults>>} */
const rawResults = [];
for ( let i = 0; i < TEST_ROUNDS; i++ ) {
const roundInfo = `round ${ i + 1 } of ${ TEST_ROUNDS }`;
log( ` >> Suite: ${ testSuite } (${ roundInfo })` );
rawResults[ i ] = {};
for ( const branch of branches ) {
const sanitizedBranch = sanitizeBranchName( branch );
const runKey = `${ testSuite }_${ sanitizedBranch }_run-${ i }`;
// @ts-ignore
const environmentDirectory = branchDirectories[ branch ];
log( ` >> Branch: ${ branch }` );
log( ' >> Starting the environment.' );
await runShellScript(
`${ wpEnvPath } start`,
environmentDirectory
);
log( ' >> Running the test.' );
rawResults[ i ][ branch ] = await runTestSuite(
testSuite,
performanceTestDirectory,
runKey
);
log( ' >> Stopping the environment' );
await runShellScript(
`${ wpEnvPath } stop`,
environmentDirectory
);
}
}
// Computing medians.
for ( const branch of branches ) {
/**
* @type {string[]}
*/
let dataPointsForTestSuite = [];
if ( rawResults.length > 0 ) {
dataPointsForTestSuite = Object.keys(
rawResults[ 0 ][ branch ]
);
}
const resultsByDataPoint = {};
dataPointsForTestSuite.forEach( ( dataPoint ) => {
// @ts-ignore
resultsByDataPoint[ dataPoint ] = rawResults.map(
// @ts-ignore
( r ) => r[ branch ][ dataPoint ]
);
} );
// @ts-ignore
const medians = Object.fromEntries(
Object.entries( resultsByDataPoint ).map(
( [ dataPoint, dataPointResults ] ) => [
dataPoint,
median( dataPointResults ),
]
)
);
// Format results as times.
// @ts-ignore
results[ testSuite ][ branch ] = Object.fromEntries(
Object.entries( medians ).map(
( [ dataPoint, dataPointMedian ] ) => [
dataPoint,
formatTime( dataPointMedian ),
]
)
);
}
}
// 5- Formatting the results.
log( '\n>> 🎉 Results.\n' );
log(
'\nPlease note that client side metrics EXCLUDE the server response time.\n'
);
for ( const testSuite of testSuites ) {
log( `\n>> ${ testSuite }\n` );
/** @type {Record<string, Record<string, string>>} */
const invertedResult = {};
Object.entries( results[ testSuite ] ).reduce(
( acc, [ key, val ] ) => {
for ( const entry of Object.keys( val ) ) {
// @ts-ignore
if ( ! acc[ entry ] && isFinite( val[ entry ] ) )
acc[ entry ] = {};
// @ts-ignore
if ( isFinite( val[ entry ] ) ) {
// @ts-ignore
acc[ entry ][ key ] = val[ entry ] + ' ms';
}
}
return acc;
},
invertedResult
);
console.table( invertedResult );
const resultsFilename = testSuite + '.performance-results.json';
fs.writeFileSync(
path.join( ARTIFACTS_PATH, resultsFilename ),
JSON.stringify( results[ testSuite ], null, 2 )
);
}
}
module.exports = {
runPerformanceTests,
};