forked from franzenzenhofer/lighthouse-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlighthouse-script.js
284 lines (219 loc) · 8.62 KB
/
lighthouse-script.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import fs from 'fs/promises';
import { performance } from 'perf_hooks';
import * as chromeLauncher from 'chrome-launcher';
import lighthouse from 'lighthouse';
import { writeFile } from './file-utils.mjs';
import { formatAsCSV, formatAsHTML } from './format-utils.mjs';
import { readPastRunsFile, writePastRunsFile, generateIndexHTML } from './index-utils.mjs';
//import { broadcast } from './websocket-utils.mjs';
const inputFile = 'urls.txt';
const logDir = 'logs';
const resultsDir = 'results';
/*const getLighthouseVersion = async () => {
const lighthousePkg = await import('lighthouse/package.json', { assert: { type: 'json' } });
return lighthousePkg.default.version;
};
getLighthouseVersion().then(version => {
console.log(`Using Lighthouse version: ${version}`);
});*/
function getTopOpportunities(results) {
const opportunities = [];
results.forEach(result => {
if (result.error) {
console.log(`Skipping result with error for URL ${result.url}`);
return;
}
if (!result.audits) {
console.log(`No audits found for URL ${result.url}. Result:`, result);
return;
}
Object.values(result.audits).forEach(audit => {
if (audit.details && audit.details.type === 'opportunity' && audit.score !== 1) {
console.log(`Found opportunity for URL ${result.url}: ${audit.description}`);
opportunities.push(audit);
}
});
});
const grouped = {};
opportunities.forEach(opportunity => {
const key = opportunity.description;
if (grouped[key]) {
grouped[key].count++;
} else {
grouped[key] = {
opportunity,
count: 1,
};
}
});
const sortedOpportunities = Object.values(grouped).sort((a, b) => {
const scoreDiff = a.opportunity.score - b.opportunity.score;
if (scoreDiff !== 0) {
return scoreDiff;
}
return b.count - a.count;
});
const topOpportunities = sortedOpportunities.slice(0, 10).map(({ opportunity, count }) => {
return {
...opportunity,
count,
};
});
console.log("Top 10 opportunities:");
console.log(topOpportunities);
return topOpportunities;
}
async function processURLs(urls, ts, resultsSubDir, broadcast, chromeProfileDir) {
const results = [];
function logAndBroadcast(type, message, data = {}) {
console.log(message);
broadcast({ type, message, ...data });
}
for (const [index, url] of urls.entries()) {
const start = performance.now();
logAndBroadcast('testStart', `Starting Lighthouse test for ${url}`, { url, index, total: urls.length });
try {
const r = await runLighthouse(url, ts, resultsSubDir, chromeProfileDir);
logAndBroadcast('testEnd', `Lighthouse test successful for ${url}`, { url, index, success: true });
results.push(r);
} catch (error) {
logAndBroadcast('testError', `Lighthouse test failed for ${url}: ${error.message}`, { url, index, success: false, error: error.message });
results.push({ error, url });
} finally {
const end = performance.now();
const duration = (end - start) / 1000;
logAndBroadcast('testDuration', `Lighthouse test for ${url} took ${duration.toFixed(2)} seconds`, { url, duration });
}
}
return results;
}
async function createResultsSubDir(resultsSubDir) {
try {
await fs.access(resultsSubDir, fs.constants.F_OK);
} catch (error) {
await fs.mkdir(resultsSubDir, { recursive: true });
}
}
async function saveCSV(results, csvFilePath) {
try {
await writeFile(csvFilePath, formatAsCSV(results));
} catch (e) {
console.error(`Error writing CSV results: ${e.message}`);
}
}
async function saveHTML(results, htmlFilePath, topOpportunities) {
try {
const htmlContent = formatAsHTML(results, topOpportunities);
await writeFile(htmlFilePath, htmlContent);
} catch (e) {
console.error(`Error writing HTML results: ${e.message}`);
console.error(`Error stack trace: ${e.stack}`);
console.log(`HTML content:\n${formatAsHTML(results,topOpportunities)}`);
try {
await writeFile(`${htmlFilePath}_failed-run.html`, formatAsHTML(results,topOpportunities));
} catch (e) {
console.error(`Error saving failed HTML results: ${e.message}`);
}
}
}
async function runLighthouseForUrls(broadcast, chromeProfileDir = null) {
//debug chreomeProfileDir
console.log(`chromeProfileDir: ${chromeProfileDir}`);
const urls = (await fs.readFile(inputFile, 'utf-8')).split('\n').filter(Boolean);
const ts = new Date().toISOString().replace(/[:.]/g, '-');
const now = new Date();
const yearMonth = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}`;
const resultsSubDir = `${resultsDir}/${yearMonth}`;
await fs.mkdir(logDir, { recursive: true });
await fs.mkdir(resultsSubDir, { recursive: true });
const runSubDir = `${resultsSubDir}/${ts}`;
await fs.mkdir(runSubDir, { recursive: true });
const results = await processURLs(urls, ts, runSubDir, broadcast, chromeProfileDir);
const topOpportunities = getTopOpportunities(results);
// Save results internally
await createResultsSubDir(resultsSubDir);
const csvFilePath = `${resultsSubDir}/${ts}/lighthouse-results-${ts}.csv`;
const htmlFilePath = `${resultsSubDir}/${ts}/lighthouse-results-${ts}.html`;
await saveCSV(results, csvFilePath);
console.log('Top issues:', topOpportunities); // Add this line
await saveHTML(results, htmlFilePath, topOpportunities);
// Update index.html with the new results
//await generateIndexHTML(results);
broadcast({ type: 'testsFinished' });
await generateIndexHTML(results);
return {
results,
timestamp: ts,
reportDir: runSubDir,
};
}
function removeBase64Images(obj) {
for (const key in obj) {
if (typeof obj[key] === 'object' && obj[key] !== null) {
removeBase64Images(obj[key]);
} else if (key === 'data' && typeof obj[key] === 'string' && obj[key].startsWith('data:image/')) {
delete obj[key];
}
}
}
async function runLighthouse(url, ts, runSubDir, chromeProfileDir = null) {
try {
const chromeFlags = []; //['--headless'];
if (chromeProfileDir) {
chromeFlags.push(`--user-data-dir=${chromeProfileDir}`);
//profileId is the last part of chromeProfileDir
let profileId = chromeProfileDir.split("/").pop();
chromeFlags.push(`--profile-directory=${profileId}`);
console.log(`Chrome flags set to: ${chromeFlags.join(' ')}`);
} else {
console.warn('Chrome profile directory not set. Running headless.');
chromeFlags.push(`--headless`);
}
const chrome = await chromeLauncher.launch({ chromeFlags });
const opts = { output: ['json', 'html'], onlyCategories: ['performance'], port: chrome.port };
const results = await lighthouse(url, opts);
await chrome.kill();
const { audits, categories } = results.lhr;
const networkRequests = results.lhr.audits['network-requests'].details.items;
const numNetworkRequests = networkRequests.length;
const rootResponseProtocol = networkRequests.find(item => item.resourceType === 'Document')?.protocol || 'unknown';
const diagnostics = audits['diagnostics'].details.items[0];
const performanceScore = categories.performance.score;
const totalByteWeight = audits['total-byte-weight'].numericValue;
const mainThreadTime = audits['mainthread-work-breakdown'].numericValue;
const timeToInteractive = audits['interactive'].numericValue;
const serverResponseTime = audits['server-response-time'].numericValue;
const hash = url.split('').reduce((acc, char) => {
return (acc * 31 + char.charCodeAt(0)) & 0x7fffffff;
}, 0);
const reportFilename = `${runSubDir}/report-${hash}-${ts}.html`;
await fs.writeFile(reportFilename, results.report[1]);
removeBase64Images(results.lhr);
const jsonReportFilename = `${runSubDir}/report-${hash}-${ts}.json`;
await fs.writeFile(jsonReportFilename, JSON.stringify(results.lhr));
return {
url,
reportFilename,
jsonReportFilename,
performance: categories.performance.score,
firstContentfulPaint: audits['first-contentful-paint'].numericValue,
speedIndex: audits['speed-index'].numericValue,
largestContentfulPaint: audits['largest-contentful-paint'].numericValue,
interactive: audits.interactive.numericValue,
totalBlockingTime: audits['total-blocking-time'].numericValue,
cumulativeLayoutShift: audits['cumulative-layout-shift'].numericValue,
numNetworkRequests,
rootResponseProtocol,
diagnostics,
performanceScore,
totalByteWeight,
mainThreadTime,
timeToInteractive,
serverResponseTime,
audits
};
} catch (error) {
return { error, url };
}
}
export { runLighthouse, runLighthouseForUrls };