-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
requestsMonitor.js
315 lines (261 loc) · 9.13 KB
/
requestsMonitor.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* Simple HTTP requests monitor and analyzer
*/
'use strict';
exports.version = '1.2';
exports.module = function(phantomas) {
// imports
var HTTP_STATUS_CODES = phantomas.require('http').STATUS_CODES,
parseUrl = phantomas.require('url').parse;
var requests = [];
// register metric
phantomas.setMetric('requests'); // @desc total number of HTTP requests made
phantomas.setMetric('gzipRequests'); // @desc number of gzipped HTTP responses @unreliable
phantomas.setMetric('postRequests'); // @desc number of POST requests
phantomas.setMetric('httpsRequests'); // @desc number of HTTPS requests
phantomas.setMetric('notFound'); // @desc number of HTTP 404 responses
phantomas.setMetric('timeToFirstByte'); // @desc time it took to receive the first byte of the first response (that was not a redirect)
phantomas.setMetric('timeToLastByte'); // @desc time it took to receive the last byte of the first response (that was not a redirect)
phantomas.setMetric('bodySize'); // @desc size of the uncompressed content of all responses @unreliable
phantomas.setMetric('contentLength'); // @desc size of the content of all responses (based on Content-Length header) @unreliable
phantomas.setMetric('httpTrafficCompleted'); // @desc time it took to receive the last byte of the last HTTP response
// parse given URL to get protocol and domain
function parseEntryUrl(entry) {
var parsed;
if (entry.url.indexOf('data:') !== 0) {
// @see http://nodejs.org/api/url.html#url_url
parsed = parseUrl(entry.url) || {};
entry.protocol = parsed.protocol.replace(':', '');
entry.domain = parsed.hostname;
entry.query = parsed.query;
if (entry.protocol === 'https') {
entry.isSSL = true;
}
}
else {
// base64 encoded data
entry.domain = false;
entry.protocol = false;
entry.isBase64 = true;
}
}
// when the monitoring started?
var loadStartedTime;
phantomas.on('loadStarted', function(res) {
loadStartedTime = Date.now();
});
phantomas.on('onResourceRequested', function(res, request) {
// store current request data
var entry = requests[res.id] = {
id: res.id,
url: res.url,
method: res.method,
requestHeaders: {},
sendTime: res.time,
bodySize: 0,
isBlocked: false
};
// allow modules to block requests
entry.block = function() {
this.isBlocked = true;
};
res.headers.forEach(function(header) {
entry.requestHeaders[header.name] = header.value;
switch (header.name.toLowerCase()) {
// AJAX requests
case 'x-requested-with':
if (header.value === 'XMLHttpRequest') {
entry.isAjax = true;
}
break;
}
});
parseEntryUrl(entry);
if (entry.isBase64) {
return;
}
// give modules a chance to block requests using entry.block()
// @see https://github.com/ariya/phantomjs/issues/10230
phantomas.emit('beforeSend', entry, res); // @desc allows the request to be blocked
if ( (entry.isBlocked === true) && (typeof request !== 'undefined') ) {
phantomas.log('Blocked request: <' + entry.url + '>');
request.abort();
return;
}
// proceed
phantomas.emit('send', entry, res); // @desc request has been sent
});
phantomas.on('onResourceReceived', function(res) {
// current request data
var entry = requests[res.id] || {};
// fix for blocked requests still "emitting" onResourceReceived with "stage" = 'end' and empty "status" (issue #122)
if (res.status === null ) {
if (entry.isBlocked) {
return;
} else if (!entry.isBase64) {
phantomas.log('Blocked request by phantomjs: <' + entry.url + '>');
phantomas.emit('abort', entry, res); // @desc request has been blocked
}
}
switch(res.stage) {
// the beginning of response
case 'start':
entry.recvStartTime = res.time;
entry.timeToFirstByte = res.time - entry.sendTime;
// FIXME: buggy
// @see https://github.com/ariya/phantomjs/issues/10169
entry.bodySize = res.bodySize || 0;
break;
// the end of response
case 'end':
// timing
entry.recvEndTime = res.time;
entry.timeToLastByte = res.time - entry.sendTime;
entry.receiveTime = entry.recvEndTime - entry.recvStartTime;
// request method
switch(entry.method) {
case 'POST':
phantomas.incrMetric('postRequests');
phantomas.addOffender('postRequests', entry.url);
break;
}
// asset type
entry.type = 'other';
// analyze HTTP headers
entry.headers = {};
res.headers.forEach(function(header) {
entry.headers[header.name] = header.value;
switch (header.name.toLowerCase()) {
// TODO: why it's not gzipped?
// because: https://github.com/ariya/phantomjs/issues/10156
case 'content-length':
entry.contentLength = parseInt(header.value, 10);
/**
if (entry.bodySize !== entry.contentLength) {
phantomas.log('%s: %j', 'bodySize vs contentLength', {url: entry.url, bodySize: entry.bodySize, contentLength: entry.contentLength});
}
**/
break;
// detect content type
case 'content-type':
// parse header value
var value = header.value.split(';').shift().toLowerCase();
switch(value) {
case 'text/html':
entry.type = 'html';
entry.isHTML = true;
break;
case 'text/css':
entry.type = 'css';
entry.isCSS = true;
break;
case 'application/x-javascript':
case 'application/javascript':
case 'text/javascript':
entry.type = 'js';
entry.isJS = true;
break;
case 'application/json':
entry.type = 'json';
entry.isJSON = true;
break;
case 'image/png':
case 'image/jpeg':
case 'image/gif':
case 'image/svg+xml':
entry.type = 'image';
entry.isImage = true;
break;
// @see http://stackoverflow.com/questions/2871655/proper-mime-type-for-fonts
case 'application/font-wof':
case 'application/font-woff':
case 'application/vnd.ms-fontobject':
case 'application/x-font-opentype':
case 'application/x-font-truetype':
case 'application/x-font-ttf':
case 'application/x-font-woff':
case 'font/ttf':
case 'font/woff':
entry.type = 'webfont';
entry.isWebFont = true;
break;
case 'application/octet-stream':
var ext = (entry.url || '').split('.').pop();
switch(ext) {
// @see http://stackoverflow.com/questions/2871655/proper-mime-type-for-fonts#comment-8077637
case 'otf':
entry.type = 'webfont';
entry.isWebFont = true;
break;
}
break;
default:
phantomas.log('Unknown content type found: ' + value + ' for <' + entry.url + '>');
}
break;
// detect content encoding
case 'content-encoding':
if (header.value === 'gzip') {
entry.gzip = true;
}
break;
}
});
parseEntryUrl(entry);
// HTTP code
entry.status = res.status || 200 /* for base64 data */;
entry.statusText = HTTP_STATUS_CODES[entry.status];
switch(entry.status) {
case 301: // Moved Permanently
case 302: // Found
case 303: // See Other
entry.isRedirect = true;
break;
case 404: // Not Found
phantomas.incrMetric('notFound');
phantomas.addOffender('notFound', entry.url);
break;
}
// default value (if Content-Length header is not present in the response or it's base64-encoded)
// see issue #137
if (typeof entry.contentLength === 'undefined') {
entry.contentLength = entry.bodySize;
phantomas.log('%s: %j', 'contentLength missing', {url: entry.url, bodySize: entry.bodySize});
}
// requests stats
if (!entry.isBase64) {
phantomas.incrMetric('requests');
phantomas.incrMetric('bodySize', entry.bodySize);
phantomas.incrMetric('contentLength', entry.contentLength);
}
if (entry.gzip) {
phantomas.incrMetric('gzipRequests');
}
if (entry.isSSL) {
phantomas.incrMetric('httpsRequests');
phantomas.addOffender('httpsRequests', entry.url);
}
if (entry.isBase64) {
phantomas.emit('base64recv', entry, res); // @desc base64-encoded "response" has been received
}
else {
phantomas.emit('recv' , entry, res); // @desc response has been received
}
break;
}
});
// TTFB / TTLB metrics
var ttfbMeasured = false;
phantomas.on('recv', function(entry, res) {
// check the first response which is not a redirect (issue #74)
if (!ttfbMeasured && !entry.isRedirect) {
phantomas.setMetric('timeToFirstByte', entry.timeToFirstByte, true);
phantomas.setMetric('timeToLastByte', entry.timeToLastByte, true);
ttfbMeasured = true;
phantomas.log('Time to first byte: set to %d ms for <%s> (HTTP %d)', entry.timeToFirstByte, entry.url, entry.status);
phantomas.emit('responseEnd', entry, res); // @desc the first response (that was not a redirect) fully received
}
// completion of the last HTTP request
phantomas.setMetric('httpTrafficCompleted', entry.recvEndTime - loadStartedTime);
});
};