-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrinity.js
60 lines (52 loc) · 2.02 KB
/
trinity.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
const puppeteer = require('puppeteer');
const utils = require('./utils');
let tmMetrics = {};
const numberOfRuns = process.argv[2] || 1;
const testPageUrl = process.argv[3] || 'http://www.hinckleytimes.net/news/local-news/your-surname-list-you-could-13736516?onScroll=off';
async function getFramePerformanceData(frame) {
return await frame.evaluate(() => {
const perfData = window.performance.timing;
return perfData.loadEventEnd - perfData.navigationStart;
})
};
async function traverseAllFrames(frame) {
const totalLoadTime = await getFramePerformanceData(frame);
console.log(frame.url(), totalLoadTime);
for (let child of frame.childFrames()) {
traverseAllFrames(child);
}
}
function upsertSlotData(componentsData) {
for (let slot in componentsData) {
for (let metric in componentsData[slot].data) {
if (!tmMetrics[slot]) tmMetrics[slot] = {};
if (!tmMetrics[slot][metric]) tmMetrics[slot][metric] = [];
tmMetrics[slot][metric].push(componentsData[slot].data[metric]);
}
}
}
async function trace() {
let browser, page;
browser = await puppeteer.launch({ headless: false }); // as of 0.11.0 of puppeteer timeout option doesn't work so have to catch the rejection exceeding default timeout
page = await browser.newPage();
await page.goto(testPageUrl);
await page.waitFor(5*1000);
// await traverseAllFrames(page.mainFrame());
upsertSlotData(await page.evaluate(() => commercialData.performanceReport.components));
await browser.close();
}
let proms = [];
for (let i = 0; i < numberOfRuns; ++i) {
proms.push(trace());
}
Promise.all(proms).then(function() {
for (let slot in tmMetrics) {
for (let metric in tmMetrics[slot]) {
let numbers = tmMetrics[slot][metric];
tmMetrics[slot][metric].push(JSON.stringify({ mean: utils.getMean(numbers), sd: utils.getStandardDeviation(numbers) }));
}
}
console.log(tmMetrics);
}).catch(function(e) {
console.log(e);
});