-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebpagetest.js
171 lines (147 loc) · 5.91 KB
/
webpagetest.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
#!/usr/bin/env node
const program = require('commander')
const { description, version } = require('./package.json')
const WebPageTest = require("webpagetest");
const helpers = require('./utils/helperFunctions');
const argv = require('yargs');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const Queue = require('./utils/queue')
program
.version(version, '-v, - version')
.requiredOption('-k, --key <key>','[Required] Add WPT Key')
.requiredOption('-f, --filePath <filePath>','[Required] File Path for URLs')
.option('-l, --level [level]','[Optional] Depth to reach',parseInt)
.option('-ul, --url_limit [url_limit]','[Optional] Max URLs to reach',parseInt)
.parse(process.argv)
const options = program.opts();
const wpt = new WebPageTest('www.webpagetest.org', options.key);
let level = options.level || 3;
let url_limit = options.url_limit || 10;
const csvWriter = createCsvWriter({
path: './report.csv',
header: [
{ id: 'url', title: 'URL' },
{ id: 'test_id', title: 'Test ID' },
{ id: 'pageLinks', title: 'Page Links' },
{ id: 'CumulativeLayoutShift', title: 'CumulativeLayoutShift' },
{ id: 'Images', title: 'Images' },
{ id: 'domComplete', title: 'DomComplete' },
{ id: 'jsBytesUncompressed', title: 'JS Bytes Uncompressed' },
{ id: 'htmlBytesUncompressed', title: 'Html Bytes Uncompressed' },
{ id: 'cssBytesUncompressed', title: 'CSS Bytes Uncompressed' },
{ id: 'longTasks', title: 'Long Tasks' },
{ id: 'level', title: 'Level' }
]
});
let wptOptions = {
"firstViewOnly": true,
"runs": 1,
"location": 'Dulles:Chrome',
"connectivity": '4G',
"pollResults": 25,
"timeout": 600,
"timeline": true,
customMetrics: [
'[memory]',
'return new Promise((resolve) => { performance.measureUserAgentSpecificMemory().then((value) => { resolve(value.bytes); }); });',
'[pageLinks]',
'let urls=[];',
'for(var i = document.links.length; i --> 0;)',
'if(document.links[i].hostname === location.hostname)',
'urls.push(document.links[i].href)',
'return urls',
].join('\n'),
commandLine: '--disable-web-security',
}
let linksArray = [];
let queue = new Queue();
helpers.promisedReadFile(options.filePath).then(async fileData =>{
console.log('File Read Successful')
const wptUrls = fileData.split(',');
wptUrls.forEach(value => {
queue.enqueue({
url: value,
level: 0
});
})
await recursiveCaller(queue);
let records = [];
finalResult.forEach((value, key) => {
let csvObject = {};
csvObject.url = key;
csvObject.pageLinks = value.pageLinks.length;
csvObject.Images = value.Images.length;
csvObject.htmlBytesUncompressed = value.breakdown.html.bytesUncompressed;
csvObject.cssBytesUncompressed = value.breakdown.css.bytesUncompressed;
csvObject.jsBytesUncompressed = value.breakdown.js.bytesUncompressed;
csvObject.test_id = value.test_id;
csvObject.longTasks = value.longTasks.length;
csvObject.level = value.level;
value.chromeUserTiming.forEach(chromObject => {
if (chromObject.name == 'CumulativeLayoutShift')
csvObject.CumulativeLayoutShift = chromObject.value;
if (chromObject.name == 'domComplete')
csvObject.domComplete = chromObject.time;
})
records.push(csvObject);
})
csvWriter.writeRecords(records) // returns a promise
.then(() => {
console.log('Done Writing To CSV');
});
}).catch(err =>{
console.log('Error while crawling')
})
let finalResult = new Map();
let submission_count = 0;
let recursiveCaller = (urls_queue) => {
if (linksArray.length <= 10) {
return Promise.all(urls_queue.items.map(async url_object => {
let url = url_object;
try {
if (submission_count == 5) {
sleep(3000);
submission_count = 0;
}
if (!linksArray.includes(url) && linksArray.length <= url_limit && submission_count < 5) {
linksArray.push(url);
submission_count++;
let url_queue_onject = urls_queue.front()
let url_from_queue = url_queue_onject.url;
level = url_queue_onject.level;
urls_queue.dequeue();
if (url_queue_onject.level <= level) {
let wptResult = await helpers.runTest(wpt, url_from_queue, wptOptions);
wptResult.result.data.median.firstView.test_id = wptResult.result.data.id;
wptResult.result.data.median.firstView.level = url_queue_onject.level;
finalResult.set(url_from_queue, wptResult.result.data.median.firstView)
if (wptResult.result.data.median.firstView.pageLinks) {
let pageLinks = wptResult.result.data.median.firstView.pageLinks;
pageLinks.forEach(pageLink => {
urls_queue.enqueue(
{
url: pageLink,
level: url_queue_onject.level + 1
})
})
await recursiveCaller(urls_queue)
}
}
}
} catch (error) {
console.log(error)
console.log("Error while testing :-", JSON.parse(JSON.stringify(error)))
}
})
)
}
else
return;
}
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}