-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
performance.js
317 lines (288 loc) · 10.1 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
/**
* External dependencies
*/
const path = require( 'path' );
const { pickBy, mapValues } = require( 'lodash' );
/**
* Internal dependencies
*/
const { formats, log } = require( '../lib/logger' );
const {
runShellScript,
readJSONFile,
askForConfirmation,
getRandomTemporaryPath,
} = require( '../lib/utils' );
const git = require( '../lib/git' );
const config = require( '../config' );
/**
* @typedef WPPerformanceCommandOptions
*
* @property {boolean=} ci Run on CI.
* @property {string=} testsBranch The branch whose performance test files will be used for testing.
*/
/**
* @typedef WPRawPerformanceResults
*
* @property {number[]} load Load Time.
* @property {number[]} type Average type time.
* @property {number[]} focus Average block selection time.
* @property {number[]} inserterOpen Average time to open global inserter.
* @property {number[]} inserterHover Average time to move mouse between two block item in the inserter.
*/
/**
* @typedef WPPerformanceResults
*
* @property {number} load Load Time.
* @property {number} type Average type time.
* @property {number} minType Minium type time.
* @property {number} maxType Maximum type time.
* @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} 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.
*/
/**
* @typedef WPFormattedPerformanceResults
*
* @property {string=} load Load 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=} inserterOpen Average time to open global inserter.
* @property {string=} minInserterOpen Min time to open global inserter.
* @property {string=} maxInserterOpen Max time to open global inserter.
* @property {string=} inserterHover Average time to move mouse between two block item in the inserter.
* @property {string=} minInserterHover Min time to move mouse between two block item in the inserter.
* @property {string=} maxInserterHover Max time to move mouse between two block item in the inserter.
*/
/**
* 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 {string} Formatted time.
*/
function formatTime( number ) {
const factor = Math.pow( 10, 2 );
return Math.round( number * factor ) / factor + ' ms';
}
/**
* Curate the raw performance results.
*
* @param {WPRawPerformanceResults} results
*
* @return {WPPerformanceResults} Curated Performance results.
*/
function curateResults( results ) {
return {
load: average( results.load ),
type: average( results.type ),
minType: Math.min( ...results.type ),
maxType: Math.max( ...results.type ),
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 ),
inserterHover: average( results.inserterHover ),
minInserterHover: Math.min( ...results.inserterHover ),
maxInserterHover: Math.max( ...results.inserterHover ),
};
}
/**
* Set up the given branch for testing.
*
* @param {string} branch Branch name.
* @param {string} environmentDirectory Path to the plugin environment's clone.
*/
async function setUpGitBranch( branch, environmentDirectory ) {
// Restore clean working directory (e.g. if `package-lock.json` has local
// changes after install).
await git.discardLocalChanges( environmentDirectory );
log( '>> Fetching the ' + formats.success( branch ) + ' branch' );
await git.checkoutRemoteBranch( environmentDirectory, branch );
log( '>> Building the ' + formats.success( branch ) + ' branch' );
await runShellScript(
'rm -rf node_modules packages/*/node_modules && npm install && npm run build',
environmentDirectory
);
}
/**
* 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.
*
* @return {Promise<WPFormattedPerformanceResults>} Performance results for the branch.
*/
async function runTestSuite( testSuite, performanceTestDirectory ) {
const results = [];
for ( let i = 0; i < 3; i++ ) {
await runShellScript(
`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 }.test.results.json`
)
);
results.push( curateResults( rawResults ) );
}
const medians = mapValues(
{
load: results.map( ( r ) => r.load ),
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 ),
inserterOpen: results.map( ( r ) => r.inserterOpen ),
minInserterOpen: results.map( ( r ) => r.minInserterOpen ),
maxInserterOpen: results.map( ( r ) => r.maxInserterOpen ),
inserterHover: results.map( ( r ) => r.inserterHover ),
minInserterHover: results.map( ( r ) => r.minInserterHover ),
maxInserterHover: results.map( ( r ) => r.maxInserterHover ),
},
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 );
// Format results as times.
return mapValues( finiteMedians, formatTime );
}
/**
* 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 ) {
// The default value doesn't work because commander provides an array.
if ( branches.length === 0 ) {
branches = [ 'trunk' ];
}
log(
formats.title( '\n💃 Performance Tests 🕺\n\n' ),
'Welcome! 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 8888 and 8889 ports.\n' +
'Make sure these ports are not used before continuing.\n'
);
if ( ! options.ci ) {
await askForConfirmation( 'Ready to go? ' );
}
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 : ' +
formats.success( performanceTestDirectory )
);
log(
'>> Environment Directory : ' + formats.success( environmentDirectory )
);
log( '>> Installing dependencies' );
// The build packages is necessary for the performance folder
await runShellScript(
'npm install && npm run build:packages',
performanceTestDirectory
);
await runShellScript(
'cp -R ' + performanceTestDirectory + ' ' + environmentDirectory
);
log( '>> Starting the WordPress environment' );
await runShellScript( 'npm run wp-env start', environmentDirectory );
const testSuites = [ 'post-editor', 'site-editor' ];
/** @type {Record<string,Record<string, WPFormattedPerformanceResults>>} */
let results = {};
for ( const branch of branches ) {
await setUpGitBranch( branch, environmentDirectory );
log(
'>> Running the test on the ' +
formats.success( branch ) +
' branch'
);
for ( const testSuite of testSuites ) {
results = {
...results,
[ testSuite ]: {
...results[ testSuite ],
[ branch ]: await runTestSuite(
testSuite,
performanceTestDirectory
),
},
};
}
}
log( '>> Stopping the WordPress environment' );
await runShellScript( 'npm run wp-env stop', environmentDirectory );
log( '\n>> 🎉 Results.\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 ) ) {
if ( ! acc[ entry ] ) acc[ entry ] = {};
// @ts-ignore
acc[ entry ][ key ] = val[ entry ];
}
return acc;
},
invertedResult
);
console.table( invertedResult );
}
}
module.exports = {
runPerformanceTests,
};