-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
117 lines (101 loc) · 3.01 KB
/
utils.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
/**
* External dependencies
*/
import { existsSync, readFileSync, unlinkSync } from 'fs';
export function readFile( filePath ) {
return existsSync( filePath )
? readFileSync( filePath, 'utf8' ).trim()
: '';
}
export function deleteFile( filePath ) {
if ( existsSync( filePath ) ) {
unlinkSync( filePath );
}
}
function isEvent( item ) {
return (
item.cat === 'devtools.timeline' &&
item.name === 'EventDispatch' &&
item.dur &&
item.args &&
item.args.data
);
}
function isKeyDownEvent( item ) {
return isEvent( item ) && item.args.data.type === 'keydown';
}
function isKeyPressEvent( item ) {
return isEvent( item ) && item.args.data.type === 'keypress';
}
function isKeyUpEvent( item ) {
return isEvent( item ) && item.args.data.type === 'keyup';
}
function isFocusEvent( item ) {
return isEvent( item ) && item.args.data.type === 'focus';
}
function isClickEvent( item ) {
return isEvent( item ) && item.args.data.type === 'click';
}
function isMouseOverEvent( item ) {
return isEvent( item ) && item.args.data.type === 'mouseover';
}
function isMouseOutEvent( item ) {
return isEvent( item ) && item.args.data.type === 'mouseout';
}
function getEventDurationsForType( trace, filterFunction ) {
return trace.traceEvents
.filter( filterFunction )
.map( ( item ) => item.dur / 1000 );
}
export function getTypingEventDurations( trace ) {
return [
getEventDurationsForType( trace, isKeyDownEvent ),
getEventDurationsForType( trace, isKeyPressEvent ),
getEventDurationsForType( trace, isKeyUpEvent ),
];
}
export function getSelectionEventDurations( trace ) {
return [ getEventDurationsForType( trace, isFocusEvent ) ];
}
export function getClickEventDurations( trace ) {
return [ getEventDurationsForType( trace, isClickEvent ) ];
}
export function getHoverEventDurations( trace ) {
return [
getEventDurationsForType( trace, isMouseOverEvent ),
getEventDurationsForType( trace, isMouseOutEvent ),
];
}
export async function getLoadingDurations() {
return await page.evaluate( () => {
const [
{
requestStart,
responseStart,
responseEnd,
domContentLoadedEventEnd,
loadEventEnd,
},
] = performance.getEntriesByType( 'navigation' );
const paintTimings = performance.getEntriesByType( 'paint' );
return {
// Server side metric.
serverResponse: responseStart - requestStart,
// For client side metrics, consider the end of the response (the
// browser receives the HTML) as the start time (0).
firstPaint:
paintTimings.find( ( { name } ) => name === 'first-paint' )
.startTime - responseEnd,
domContentLoaded: domContentLoadedEventEnd - responseEnd,
loaded: loadEventEnd - responseEnd,
firstContentfulPaint:
paintTimings.find(
( { name } ) => name === 'first-contentful-paint'
).startTime - responseEnd,
// This is evaluated right after Puppeteer found the block selector.
firstBlock: performance.now() - responseEnd,
// As defined by https://web.dev/ttfb/#measure-ttfb-in-javascript
timeToFirstByte: responseStart,
};
} );
}