-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
post-editor.test.js
300 lines (281 loc) · 8.28 KB
/
post-editor.test.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
/**
* External dependencies
*/
import { basename, join } from 'path';
import { writeFileSync } from 'fs';
import { sum } from 'lodash';
/**
* WordPress dependencies
*/
import {
createNewPost,
saveDraft,
insertBlock,
openGlobalBlockInserter,
closeGlobalBlockInserter,
openListView,
closeListView,
} from '@wordpress/e2e-test-utils';
/**
* Internal dependencies
*/
import {
readFile,
deleteFile,
getTypingEventDurations,
getClickEventDurations,
getHoverEventDurations,
getSelectionEventDurations,
getLoadingDurations,
} from './utils';
jest.setTimeout( 1000000 );
describe( 'Post Editor Performance', () => {
const results = {
serverResponse: [],
firstPaint: [],
domContentLoaded: [],
loaded: [],
firstContentfulPaint: [],
firstBlock: [],
type: [],
focus: [],
listViewOpen: [],
inserterOpen: [],
inserterHover: [],
inserterSearch: [],
};
const traceFile = __dirname + '/trace.json';
let traceResults;
beforeAll( async () => {
const html = readFile(
join( __dirname, '../../assets/large-post.html' )
);
await createNewPost();
await page.evaluate( ( _html ) => {
const { parse } = window.wp.blocks;
const { dispatch } = window.wp.data;
const blocks = parse( _html );
blocks.forEach( ( block ) => {
if ( block.name === 'core/image' ) {
delete block.attributes.id;
delete block.attributes.url;
}
} );
dispatch( 'core/block-editor' ).resetBlocks( blocks );
}, html );
await saveDraft();
} );
afterAll( async () => {
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
deleteFile( traceFile );
} );
beforeEach( async () => {
// Disable auto-save to avoid impacting the metrics.
await page.evaluate( () => {
window.wp.data
.dispatch( 'core/edit-post' )
.__experimentalUpdateLocalAutosaveInterval( 100000000000 );
window.wp.data
.dispatch( 'core/editor' )
.updateEditorSettings( { autosaveInterval: 100000000000 } );
} );
} );
it( 'Loading', async () => {
// Measuring loading time.
let i = 5;
while ( i-- ) {
await page.reload();
await page.waitForSelector( '.wp-block' );
const {
serverResponse,
firstPaint,
domContentLoaded,
loaded,
firstContentfulPaint,
firstBlock,
} = await getLoadingDurations();
results.serverResponse.push( serverResponse );
results.firstPaint.push( firstPaint );
results.domContentLoaded.push( domContentLoaded );
results.loaded.push( loaded );
results.firstContentfulPaint.push( firstContentfulPaint );
results.firstBlock.push( firstBlock );
}
} );
it( 'Typing', async () => {
// Measuring typing performance.
await insertBlock( 'Paragraph' );
let i = 20;
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
while ( i-- ) {
// Wait for the browser to be idle before starting the monitoring.
// The timeout should be big enough to allow all async tasks tor run.
// And also to allow Rich Text to mark the change as persistent.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 2000 );
await page.keyboard.type( 'x' );
}
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [
keyDownEvents,
keyPressEvents,
keyUpEvents,
] = getTypingEventDurations( traceResults );
if (
keyDownEvents.length === keyPressEvents.length &&
keyPressEvents.length === keyUpEvents.length
) {
// The first character typed triggers a longer time (isTyping change)
// It can impact the stability of the metric, so we exclude it.
for ( let j = 1; j < keyDownEvents.length; j++ ) {
results.type.push(
keyDownEvents[ j ] + keyPressEvents[ j ] + keyUpEvents[ j ]
);
}
}
} );
it( 'Selecting blocks', async () => {
// Measuring block selection performance.
await createNewPost();
await page.evaluate( () => {
const { createBlock } = window.wp.blocks;
const { dispatch } = window.wp.data;
const blocks = window.lodash
.times( 1000 )
.map( () => createBlock( 'core/paragraph' ) );
dispatch( 'core/block-editor' ).resetBlocks( blocks );
} );
const paragraphs = await page.$$( '.wp-block' );
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
await paragraphs[ 0 ].click();
for ( let j = 1; j <= 10; j++ ) {
// Wait for the browser to be idle before starting the monitoring.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 1000 );
await paragraphs[ j ].click();
}
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [ focusEvents ] = getSelectionEventDurations( traceResults );
results.focus = focusEvents;
} );
it( 'Opening persistent list view', async () => {
// Measure time to open inserter.
await page.waitForSelector( '.edit-post-layout' );
for ( let j = 0; j < 10; j++ ) {
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
await openListView();
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [ mouseClickEvents ] = getClickEventDurations( traceResults );
for ( let k = 0; k < mouseClickEvents.length; k++ ) {
results.listViewOpen.push( mouseClickEvents[ k ] );
}
await closeListView();
}
} );
it( 'Opening the inserter', async () => {
// Measure time to open inserter.
await page.waitForSelector( '.edit-post-layout' );
for ( let j = 0; j < 10; j++ ) {
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
await openGlobalBlockInserter();
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [ mouseClickEvents ] = getClickEventDurations( traceResults );
for ( let k = 0; k < mouseClickEvents.length; k++ ) {
results.inserterOpen.push( mouseClickEvents[ k ] );
}
await closeGlobalBlockInserter();
}
} );
it( 'Searching the inserter', async () => {
// Measure time to search the inserter and get results.
await openGlobalBlockInserter();
for ( let j = 0; j < 10; j++ ) {
// Wait for the browser to be idle before starting the monitoring.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 500 );
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
await page.keyboard.type( 'p' );
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [
keyDownEvents,
keyPressEvents,
keyUpEvents,
] = getTypingEventDurations( traceResults );
if (
keyDownEvents.length === keyPressEvents.length &&
keyPressEvents.length === keyUpEvents.length
) {
results.inserterSearch.push(
sum( keyDownEvents ) +
sum( keyPressEvents ) +
sum( keyUpEvents )
);
}
await page.keyboard.press( 'Backspace' );
}
await closeGlobalBlockInserter();
} );
it( 'Hovering Inserter Items', async () => {
// Measure inserter hover performance.
const paragraphBlockItem =
'.block-editor-inserter__menu .editor-block-list-item-paragraph';
const headingBlockItem =
'.block-editor-inserter__menu .editor-block-list-item-heading';
await openGlobalBlockInserter();
await page.waitForSelector( paragraphBlockItem );
await page.hover( paragraphBlockItem );
await page.hover( headingBlockItem );
for ( let j = 0; j < 10; j++ ) {
// Wait for the browser to be idle before starting the monitoring.
// eslint-disable-next-line no-restricted-syntax
await page.waitForTimeout( 200 );
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
await page.hover( paragraphBlockItem );
await page.hover( headingBlockItem );
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [ mouseOverEvents, mouseOutEvents ] = getHoverEventDurations(
traceResults
);
for ( let k = 0; k < mouseOverEvents.length; k++ ) {
results.inserterHover.push(
mouseOverEvents[ k ] + mouseOutEvents[ k ]
);
}
}
await closeGlobalBlockInserter();
} );
} );