-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
post-editor.test.js
262 lines (222 loc) · 6.43 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
/**
* External dependencies
*/
import { basename, join } from 'path';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
/**
* WordPress dependencies
*/
import {
createNewPost,
saveDraft,
insertBlock,
openGlobalBlockInserter,
closeGlobalBlockInserter,
} from '@wordpress/e2e-test-utils';
function readFile( filePath ) {
return existsSync( filePath )
? readFileSync( filePath, 'utf8' ).trim()
: '';
}
function deleteFile( filePath ) {
if ( existsSync( filePath ) ) {
unlinkSync( filePath );
}
}
function isKeyEvent( item ) {
return (
item.cat === 'devtools.timeline' &&
item.name === 'EventDispatch' &&
item.dur &&
item.args &&
item.args.data
);
}
function isKeyDownEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'keydown';
}
function isKeyPressEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'keypress';
}
function isKeyUpEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'keyup';
}
function isFocusEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'focus';
}
function isClickEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'click';
}
function isMouseOverEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'mouseover';
}
function isMouseOutEvent( item ) {
return isKeyEvent( item ) && item.args.data.type === 'mouseout';
}
function getEventDurationsForType( trace, filterFunction ) {
return trace.traceEvents
.filter( filterFunction )
.map( ( item ) => item.dur / 1000 );
}
function getTypingEventDurations( trace ) {
return [
getEventDurationsForType( trace, isKeyDownEvent ),
getEventDurationsForType( trace, isKeyPressEvent ),
getEventDurationsForType( trace, isKeyUpEvent ),
];
}
function getSelectionEventDurations( trace ) {
return [ getEventDurationsForType( trace, isFocusEvent ) ];
}
function getClickEventDurations( trace ) {
return [ getEventDurationsForType( trace, isClickEvent ) ];
}
function getHoverEventDurations( trace ) {
return [
getEventDurationsForType( trace, isMouseOverEvent ),
getEventDurationsForType( trace, isMouseOutEvent ),
];
}
jest.setTimeout( 1000000 );
describe( 'Post Editor Performance', () => {
it( 'Loading, typing and selecting blocks', async () => {
const results = {
load: [],
type: [],
focus: [],
inserterOpen: [],
inserterHover: [],
};
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();
let i = 5;
// Measuring loading time
while ( i-- ) {
const startTime = new Date();
await page.reload();
await page.waitForSelector( '.wp-block' );
results.load.push( new Date() - startTime );
}
// Measure time to open inserter
await page.waitForSelector( '.edit-post-layout' );
const traceFile = __dirname + '/trace.json';
let traceResults;
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();
}
// 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 < 20; j++ ) {
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();
// Measuring typing performance
await insertBlock( 'Paragraph' );
i = 200;
await page.tracing.start( {
path: traceFile,
screenshots: false,
categories: [ 'devtools.timeline' ],
} );
while ( i-- ) {
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
) {
for ( let j = 0; j < keyDownEvents.length; j++ ) {
results.type.push(
keyDownEvents[ j ] + keyPressEvents[ j ] + keyUpEvents[ j ]
);
}
}
// 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' ],
} );
for ( let j = 0; j < 10; j++ ) {
await paragraphs[ j ].click();
}
await page.tracing.stop();
traceResults = JSON.parse( readFile( traceFile ) );
const [ focusEvents ] = getSelectionEventDurations( traceResults );
results.focus = focusEvents;
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
deleteFile( traceFile );
expect( true ).toBe( true );
} );
} );