|
6 | 6 | */ |
7 | 7 | 'use strict'; |
8 | 8 | var util = require('../util/util'), |
9 | | - RequestTiming = require('../requestTiming'), |
10 | | - Stats = require('fast-stats').Stats, |
11 | | - winston = require('winston'); |
| 9 | + RequestTiming = require('../requestTiming'), |
| 10 | + Stats = require('fast-stats').Stats, |
| 11 | + winston = require('winston'); |
12 | 12 | var domains = {}; |
13 | 13 |
|
14 | 14 | exports.processPage = function(pageData) { |
15 | | - var log = winston.loggers.get('sitespeed.io'); |
16 | | - var harData = []; |
17 | | - if (pageData.browsertime) { |
18 | | - Array.prototype.push.apply(harData, pageData.browsertime.har); |
19 | | - } |
20 | | - if (pageData.webpagetest) { |
21 | | - Array.prototype.push.apply(harData, pageData.webpagetest.har); |
22 | | - } |
23 | | - var pageURL = util.getURLFromPageData(pageData); |
24 | | - harData.forEach(function(har) { |
25 | | - har.log.entries.forEach(function(entry) { |
26 | | - var domain = domains[util.getHostname(entry.request.url)]; |
27 | | - var total; |
28 | | - if (domain) { |
29 | | - if (entry.timings) { |
30 | | - total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings |
31 | | - .send + entry.timings.wait + entry.timings.receive; |
32 | | - domain.blocked.add(entry.timings.blocked, entry.request.url, pageURL); |
33 | | - domain.dns.add(entry.timings.dns, entry.request.url, pageURL); |
34 | | - domain.connect.add(entry.timings.connect, entry.request.url, pageURL); |
35 | | - domain.ssl.add(entry.timings.ssl, entry.request.url, pageURL); |
36 | | - domain.send.add(entry.timings.send, entry.request.url, pageURL); |
37 | | - domain.wait.add(entry.timings.wait, entry.request.url, pageURL); |
38 | | - domain.receive.add(entry.timings.receive, entry.request.url, pageURL); |
39 | | - domain.total.add(total, entry.request.url, pageURL); |
40 | | - } else { |
41 | | - log.log('info', 'Missing timings in the HAR'); |
42 | | - } |
| 15 | + var log = winston.loggers.get('sitespeed.io'); |
| 16 | + var harData = []; |
| 17 | + if (pageData.browsertime) { |
| 18 | + Array.prototype.push.apply(harData, pageData.browsertime.har); |
| 19 | + } |
| 20 | + if (pageData.webpagetest) { |
| 21 | + Array.prototype.push.apply(harData, pageData.webpagetest.har); |
| 22 | + } |
| 23 | + var pageURL = util.getURLFromPageData(pageData); |
| 24 | + harData.forEach(function(har) { |
| 25 | + har.log.entries.forEach(function(entry) { |
| 26 | + var domain = domains[util.getHostname(entry.request.url)]; |
| 27 | + var total; |
| 28 | + if (domain) { |
| 29 | + if (entry.timings) { |
| 30 | + total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings |
| 31 | + .send + entry.timings.wait + entry.timings.receive; |
| 32 | + domain.blocked.add(entry.timings.blocked, entry.request.url, pageURL); |
| 33 | + domain.dns.add(entry.timings.dns, entry.request.url, pageURL); |
| 34 | + domain.connect.add(entry.timings.connect, entry.request.url, pageURL); |
| 35 | + domain.ssl.add(entry.timings.ssl, entry.request.url, pageURL); |
| 36 | + domain.send.add(entry.timings.send, entry.request.url, pageURL); |
| 37 | + domain.wait.add(entry.timings.wait, entry.request.url, pageURL); |
| 38 | + domain.receive.add(entry.timings.receive, entry.request.url, pageURL); |
| 39 | + domain.total.add(total, entry.request.url, pageURL); |
| 40 | + } else { |
| 41 | + log.log('info', 'Missing timings in the HAR'); |
| 42 | + } |
43 | 43 |
|
44 | | - } else { |
45 | | - if (entry.timings) { |
46 | | - total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings |
47 | | - .send + entry.timings.wait + entry.timings.receive; |
| 44 | + } else { |
| 45 | + if (entry.timings) { |
| 46 | + total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings |
| 47 | + .send + entry.timings.wait + entry.timings.receive; |
48 | 48 |
|
49 | | - domains[util.getHostname(entry.request.url)] = { |
50 | | - domain: util.getHostname(entry.request.url), |
51 | | - blocked: new RequestTiming(entry.timings.blocked, entry.request.url, pageURL), |
52 | | - dns: new RequestTiming(entry.timings.dns, entry.request.url, pageURL), |
53 | | - connect: new RequestTiming(entry.timings.connect, entry.request.url, pageURL), |
54 | | - ssl: new RequestTiming(entry.timings.ssl, entry.request.url, pageURL), |
55 | | - send: new RequestTiming(entry.timings.send, entry.request.url, pageURL), |
56 | | - wait: new RequestTiming(entry.timings.wait, entry.request.url, pageURL), |
57 | | - receive: new RequestTiming(entry.timings.receive, entry.request.url, pageURL), |
58 | | - total: new RequestTiming(total, entry.request.url, pageURL) |
59 | | - }; |
60 | | - } else { |
61 | | - log.log('info', 'Missing timings in the HAR'); |
62 | | - } |
63 | | - } |
64 | | - }); |
65 | | - }); |
| 49 | + domains[util.getHostname(entry.request.url)] = { |
| 50 | + domain: util.getHostname(entry.request.url), |
| 51 | + blocked: new RequestTiming(entry.timings.blocked, entry.request.url, pageURL), |
| 52 | + dns: new RequestTiming(entry.timings.dns, entry.request.url, pageURL), |
| 53 | + connect: new RequestTiming(entry.timings.connect, entry.request.url, pageURL), |
| 54 | + ssl: new RequestTiming(entry.timings.ssl, entry.request.url, pageURL), |
| 55 | + send: new RequestTiming(entry.timings.send, entry.request.url, pageURL), |
| 56 | + wait: new RequestTiming(entry.timings.wait, entry.request.url, pageURL), |
| 57 | + receive: new RequestTiming(entry.timings.receive, entry.request.url, pageURL), |
| 58 | + total: new RequestTiming(total, entry.request.url, pageURL) |
| 59 | + }; |
| 60 | + } else { |
| 61 | + log.log('info', 'Missing timings in the HAR'); |
| 62 | + } |
| 63 | + } |
| 64 | + }); |
| 65 | + }); |
66 | 66 |
|
67 | | - // add request & size, just do it for the first run |
68 | | - harData[0].log.entries.forEach(function(entry) { |
69 | | - var domain = domains[util.getHostname(entry.request.url)]; |
| 67 | + // add request & size, just do it for the first run |
| 68 | + if (harData.length > 0) { |
| 69 | + harData[0].log.entries.forEach(function(entry) { |
| 70 | + var domain = domains[util.getHostname(entry.request.url)]; |
70 | 71 |
|
71 | | - domain.count = domain.count ? domain.count++ : domain.count = 1; |
| 72 | + domain.count = domain.count ? domain.count++ : domain.count = 1; |
72 | 73 |
|
73 | | - if (domain.size) { |
74 | | - domain.size.total += entry.response.content.size; |
75 | | - domain.size[util.getContentType(entry.response.content.mimeType)] += |
76 | | - entry.response.content.size; |
77 | | - } |
78 | | - else { |
79 | | - domain.size = { |
80 | | - total: entry.response.content.size, |
81 | | - css: 0, |
82 | | - doc: 0, |
83 | | - js: 0, |
84 | | - image: 0, |
85 | | - font: 0, |
86 | | - flash: 0, |
87 | | - unknown: 0 |
88 | | - }; |
89 | | - domain.size[util.getContentType(entry.response.content.mimeType)] = entry.response.content.size; |
90 | | - } |
91 | | - }); |
| 74 | + if (domain.size) { |
| 75 | + domain.size.total += entry.response.content.size; |
| 76 | + domain.size[util.getContentType(entry.response.content.mimeType)] += |
| 77 | + entry.response.content.size; |
| 78 | + } else { |
| 79 | + domain.size = { |
| 80 | + total: entry.response.content.size, |
| 81 | + css: 0, |
| 82 | + doc: 0, |
| 83 | + js: 0, |
| 84 | + image: 0, |
| 85 | + font: 0, |
| 86 | + flash: 0, |
| 87 | + unknown: 0 |
| 88 | + }; |
| 89 | + domain.size[util.getContentType(entry.response.content.mimeType)] = entry.response.content.size; |
| 90 | + } |
| 91 | + }); |
| 92 | +} |
92 | 93 |
|
93 | 94 |
|
94 | 95 | }; |
95 | 96 |
|
96 | 97 | exports.generateResults = function() { |
97 | | - var values = Object.keys(domains).map(function(key) { |
98 | | - return domains[key]; |
99 | | - }); |
| 98 | + var values = Object.keys(domains).map(function(key) { |
| 99 | + return domains[key]; |
| 100 | + }); |
100 | 101 |
|
101 | | - return { |
102 | | - id: 'domains', |
103 | | - list: values |
104 | | - }; |
| 102 | + return { |
| 103 | + id: 'domains', |
| 104 | + list: values |
| 105 | + }; |
105 | 106 | }; |
106 | 107 |
|
107 | 108 | exports.clear = function() { |
108 | | - domains = {}; |
| 109 | + domains = {}; |
109 | 110 | }; |
0 commit comments