-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (128 loc) · 3.88 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
import { writeFile } from "fs";
import puppeteer from "puppeteer";
import lighthouse, { desktopConfig } from "lighthouse";
import { pick } from "lodash-es";
const USERNAME = "";
const PASSWORD = "";
const ITERATIONS = 10;
// Login using puppeteer so that protected routes can be accessed
async function login(page, origin) {
await page.goto(origin);
await page.waitForSelector('input[data-cy="EmailInput"]');
const emailInput = await page.$('input[data-cy="EmailInput"]');
const passwordInput = await page.$('input[data-cy="PasswordInput"]');
await emailInput.type(USERNAME);
await passwordInput.type(PASSWORD);
const signInButton = await page.$('button[data-cy="SignInButton"]');
await signInButton.click();
}
function parsePerformanceResults(audits) {
const relevantAudits = (({
"first-contentful-paint": fcp,
"largest-contentful-paint": lcp,
"speed-index": si,
"total-blocking-time": tbt,
"max-potential-fid": mpf,
"cumulative-layout-shift": cls,
}) => ({ fcp, lcp, si, tbt, mpf, cls }))(audits);
const parsedResults = pick(relevantAudits, [
"fcp.numericValue",
"fcp.numericUnit",
"lcp.numericValue",
"lcp.numericUnit",
"si.numericValue",
"si.numericUnit",
"tbt.numericValue",
"tbt.numericUnit",
"mpf.numericValue",
"mpf.numericUnit",
"cls.numericValue",
"cls.numericUnit",
]);
return parsedResults;
}
function averagePerformanceResults(results) {
const averagedResults = {};
["fcp", "lcp", "si", "tbt", "mpf", "cls"].forEach((metric) => {
let sum = 0;
let count = 0;
const numericUnit = results[0][metric].numericUnit;
results.forEach((result) => {
const metricSample = result[metric];
if (metricSample.scoreDisplayMode === "numeric") {
sum += metricSample.numericValue;
count += 1;
}
});
const averageResult = {
averageNumericValue: sum / count,
numericUnit,
};
averagedResults[metric] = averageResult;
});
return averagedResults;
}
async function lighthouseTest(page, url) {
const result = await lighthouse(
url,
{ disableStorageReset: true },
desktopConfig,
page
);
return parsePerformanceResults(result.lhr.audits);
}
async function main() {
if (!USERNAME || !PASSWORD) {
console.log(
"Error: please setup username and password before running the script."
);
console.log("More info can be found in the README");
return;
}
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1080, height: 1024 });
const loginResults = [];
const workscopesResults = [];
const viewerResults = [];
process.stdout.write("Login page tests...\r");
for (let i = 0; i < ITERATIONS; i++) {
const result = await lighthouseTest(
page,
"https://digital-twin.veerum.com/login"
);
loginResults.push(result);
}
console.log("Login page tests... Completed");
// Login to gain access to protected routes
await login(page, "https://digital-twin.veerum.com/login");
process.stdout.write("Workscopes page tests...\r");
for (let i = 0; i < ITERATIONS; i++) {
const result = await lighthouseTest(
page,
"https://digital-twin.veerum.com"
);
workscopesResults.push(result);
}
console.log("Workscopes page tests... Completed");
process.stdout.write("Viewer page tests...\r");
for (let i = 0; i < ITERATIONS; i++) {
const result = await lighthouseTest(
page,
"https://digital-twin.veerum.com/workscopes/631b6641310c4751c59be759/viewer"
);
viewerResults.push(result);
}
console.log("Viewer page tests... Completed");
const fullResults = {
loginResults,
workscopesResults,
viewerResults,
};
writeFile("results.json", JSON.stringify(fullResults), function (err) {
if (err) throw err;
console.log("complete");
});
browser.close();
}
await main();