-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
171 lines (147 loc) · 5.75 KB
/
index.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
const { getEventsGroups, getEventsTree } = require('./events-tree.js');
const { makeEventsRelative, castMarkName } = require('./events-helpers.js');
const { getEventInMainFrame, getMainEventsTimestamps } = require('./events-main.js');
const { parseNetwork, getResourcesStats, getCoverageStats } = require('./events-network.js');
const {
getSpeedIndex,
prepareElementsTimings,
prepareLayersPaints,
getCumulativeLayoutShift,
getTotalBlockingTime
} = require('./events-user-centric.js');
const { getLighthouseScore } = require('./lighthouse-score');
const { getActionsStats } = require('./events-actions.js');
const { getCustomMetrics } = require('./events-custom.js');
const { makeCoverageMap } = require('./events-coverage.js');
const {
getScriptsEvaluationStats,
makeScriptsEvaluationMap,
getMeaningEvaluationEvents,
prepareEvaluations
} = require('./events-evaluation.js');
const { getScreenshotsByMetrics } = require('./events-screenshots.js');
const { flat } = require('./../objects-utils.js');
const { getBriefTracing, getWaterfall, processTracingTree, gzip } = require('./events-brief-tracing.js');
const getAllStats = async ({
tracing,
coverage,
actions,
timeToInteractive,
elementsTimings,
layersPaints,
cacheLog
}, config) => {
const { requests: { internalTest }, firstEvent: firstEventName, customMetrics, platform } = config;
const firstEvent = getEventInMainFrame(tracing, firstEventName) || getEventInMainFrame(tracing, 'responseEnd');
const navigationStart = getEventInMainFrame(tracing, 'navigationStart');
const navigationStartDelta = navigationStart.ts - firstEvent.ts;
const mainFrame = firstEvent.args.frame;
const relativeEvents = makeEventsRelative(tracing, firstEvent);
const eventsGroups = getEventsGroups(relativeEvents);
const mainEvents = eventsGroups.find(({ name }) => name === 'Main').events;
const otherEvents = eventsGroups.find(({ name }) => name === 'Other').events;
const mainEventsTree = processTracingTree(getEventsTree(mainEvents));
const meaningfulEvaluations = getMeaningEvaluationEvents(mainEventsTree);
const extractEvaluationValues = prepareEvaluations(meaningfulEvaluations);
const evaluationMap = makeScriptsEvaluationMap(extractEvaluationValues.filter(({ url }) => url));
const coverageMap = makeCoverageMap(coverage);
const network = parseNetwork(relativeEvents, evaluationMap, coverageMap, internalTest);
const resourcesStats = getResourcesStats(network);
const evaluationStats = getScriptsEvaluationStats(extractEvaluationValues, internalTest);
const coverageStats = getCoverageStats(network);
const timings = getMainEventsTimestamps(relativeEvents, mainFrame);
const custom = getCustomMetrics(relativeEvents, customMetrics);
const speedIndex = await getSpeedIndex(tracing);
const totalBlockingTime = getTotalBlockingTime(mainEventsTree, timeToInteractive, timings.firstContentfulPaint);
const cumulativeLayoutShift = getCumulativeLayoutShift(relativeEvents);
const userCentric = {
speedIndex,
timeToInteractive,
cumulativeLayoutShift,
totalBlockingTime,
lighthouseScore: getLighthouseScore({
FCP: timings.firstContentfulPaint / 1000,
SI: speedIndex / 1000,
LCP: timings.largestContentfulPaint / 1000,
TTI: timeToInteractive / 1000,
TBT: totalBlockingTime / 1000,
CLS: cumulativeLayoutShift,
}, platform)
};
const elementsTimingsStats = prepareElementsTimings(elementsTimings, navigationStartDelta);
const layersPaintsStats = prepareLayersPaints(layersPaints, firstEvent);
const actionsStats = getActionsStats(actions, navigationStart, config);
const flattedTimestamps = Object.entries(flat({
timings,
userCentric: {
timeToInteractive
},
elementsTimings: elementsTimingsStats,
layersPaints: layersPaintsStats
}))
const screenshots = getScreenshotsByMetrics(
relativeEvents,
flattedTimestamps.map(([name, value]) => ({ name, value }))
);
let flameChartData;
if (config.tracing) {
const data = await getBriefTracing(mainEvents, otherEvents, firstEvent);
const hasCache = cacheLog && cacheLog.length;
const firstResourceEvent = tracing.find(({
name,
args
}) => (
name === 'ResourceSendRequest' && hasCache && args && args.data && args.data.url === cacheLog[0].name
));
const timeDelta = hasCache ? (
cacheLog[0].requestStart - (
firstResourceEvent ? (
(firstResourceEvent.ts - firstEvent.ts) / 1000
) : 0
)
) : 0;
const waterfall = getWaterfall(network, cacheLog, timeDelta);
const marks = flattedTimestamps
.map(([fullName, timestamp]) => {
const markInfo = castMarkName(fullName);
if (markInfo) {
return {
fullName,
timestamp: timestamp / 1000,
...markInfo
}
} else {
return null;
}
})
.filter(Boolean);
flameChartData = {
data,
waterfall,
marks
};
if (config.tracing === 'gzip') {
flameChartData = await gzip(flameChartData);
}
}
return {
stats: {
timings,
userCentric,
custom,
elementsTimings: elementsTimingsStats,
layersPaints: layersPaintsStats,
evaluation: evaluationStats,
resources: resourcesStats,
coverage: coverageStats,
},
network,
coverage,
screenshots,
tracing: flameChartData,
actions: actionsStats
}
};
module.exports = {
getAllStats
};