-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (95 loc) · 3.04 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
import puppeteer from 'puppeteer';
import {PredefinedNetworkConditions} from 'puppeteer';
import fs from 'fs';
import { parse } from 'csv-parse';
// Lighthouse script for getting URL JS elements
import lighthouseFromPuppeteer from './config/lighthouse-audit.js';
import runPerformanceReview from './config/puppteerRuntime.js';
// Measure the page load time
const slow3G = PredefinedNetworkConditions['Slow 3G'];
const fast3G = PredefinedNetworkConditions['Fast 3G'];
const slow4g = PredefinedNetworkConditions['Slow 4G'];
const fast4G = PredefinedNetworkConditions['Fast 4G'];
// Device to emulate
const device = 'mobile';
// Lighthouse conditions
const perfConfig = {
extends: 'lighthouse:default',
settings: {
throttlingMethod: 'devtools',
emulatedFormFactor:{device},
onlyCategories: ['performance'],
},
};
const options = {
logLevel: 'info',
disableDeviceEmulation: true,
chromeFlags: [
'--disable-mobile-emulation',
'--headless'
],
};
( () => {
try{
const urls = []
const performanceResults = [];
fs.createReadStream('documents/list_urls.csv')
.pipe(parse({ delimiter: ',' }))
.on('data', async (row) => {
urls.push(row[0]);
console.log('URLs collected:', urls);
})
.on('end', async () => {
for (let i = 0; i < urls.length; i++) {
if (urls[i] === '') {
console.log('No URL provided');
continue
}
const url = urls[i];
console.log("Running Performance Testing");
const performanceTesting = await Promise.all([
lighthouseFromPuppeteer(url, options, perfConfig),
runPerformanceReview(url),
]);
const performanceTest = {
lighthouse: performanceTesting[0],
consolePerformance: performanceTesting[1]
}
console.log('Performance Testing Complete');
performanceResults.push(performanceTest);
fs.writeFile('documents/jsonTests/performance.json', JSON.stringify(performanceResults), (err) => {
if (err) {
console.error(err);
}
});
}
console.log(performanceResults)
const results = '/lighthouse.json'
console.log(results)
const csv = performanceResults.map((row) => {
return [
row.lighthouse.totalByteWeight,
row.lighthouse.scriptTreemapData,
row.lighthouse.resourceSummary,
row.lighthouse.diagnostics,
row.consolePerformance.lcp,
row.consolePerformance.cls,
row.consolePerformance.inp,
];
}
).join('\n');
fs.writeFile('documents/performance.csv', csv, (err) => {
if (err) {
console.error(err);
}
});
})
// console.log(performanceResults)
// const data = [
// ['URL', 'Load Time', 'Bytes Used'],
// ['https://www.example.com', '100ms', '50%'],
// ];
}catch(err){
console.error(err);
}
})();