diff --git a/lib/collectors/domains.js b/lib/collectors/domains.js index cc08b589c..e47d77bb8 100644 --- a/lib/collectors/domains.js +++ b/lib/collectors/domains.js @@ -5,12 +5,15 @@ * Released under the Apache 2.0 License */ -var util = require('../util'); -var domains = {}; -var Stats = require('fast-stats').Stats; +var util = require('../util'), + domains = {}, + Stats = require('fast-stats').Stats, + DomainTiming = require('../DomainTiming'), + log = require('winston'); exports.processPage = function(pageData) { if (pageData.har) { + var pageURL = util.getURLFromPageData(pageData); pageData.har.forEach(function(har) { har.log.entries.forEach(function(entry) { var domain = domains[util.getHostname(entry.request.url)]; @@ -18,41 +21,40 @@ exports.processPage = function(pageData) { if (domain) { if (entry.timings) { domain.count++; - total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings.send + entry.timings.wait + entry.timings.receive; - if (total>domain.total.max) { - domain.slowestUrl = entry.request.url; - } - domain.blocked.push(entry.timings.blocked); - domain.dns.push(entry.timings.dns); - domain.connect.push(entry.timings.connect); - domain.ssl.push(entry.timings.ssl); - domain.send.push(entry.timings.send); - domain.wait.push(entry.timings.wait); - domain.receive.push(entry.timings.receive); - domain.total.push(total); + total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings + .send + entry.timings.wait + entry.timings.receive; + domain.blocked.add(entry.timings.blocked, entry.request.url, pageURL); + domain.dns.add(entry.timings.dns, entry.request.url, pageURL); + domain.connect.add(entry.timings.connect, entry.request.url, pageURL); + domain.ssl.add(entry.timings.ssl, entry.request.url, pageURL); + domain.send.add(entry.timings.send, entry.request.url, pageURL); + domain.wait.add(entry.timings.wait, entry.request.url, pageURL); + domain.receive.add(entry.timings.receive, entry.request.url, pageURL); + domain.total.add(total, entry.request.url, pageURL); } else { - console.log('Missing timings in the HAR'); + log.log('info', 'Missing timings in the HAR'); } } else { if (entry.timings) { - total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings.send + entry.timings.wait + entry.timings.receive; + total = entry.timings.blocked + entry.timings.dns + entry.timings.connect + entry.timings.ssl + entry.timings + .send + entry.timings.wait + entry.timings.receive; domains[util.getHostname(entry.request.url)] = { domain: util.getHostname(entry.request.url), - blocked: new Stats().push(entry.timings.blocked), - dns: new Stats().push(entry.timings.dns), - connect: new Stats().push(entry.timings.connect), - ssl: new Stats().push(entry.timings.ssl), - send: new Stats().push(entry.timings.send), - wait: new Stats().push(entry.timings.wait), - receive: new Stats().push(entry.timings.receive), - slowestUrl: entry.request.url, - total: new Stats().push(total), + blocked: new DomainTiming(entry.timings.blocked, entry.request.url, pageURL), + dns: new DomainTiming(entry.timings.dns, entry.request.url, pageURL), + connect: new DomainTiming(entry.timings.connect, entry.request.url, pageURL), + ssl: new DomainTiming(entry.timings.ssl, entry.request.url, pageURL), + send: new DomainTiming(entry.timings.send, entry.request.url, pageURL), + wait: new DomainTiming(entry.timings.wait, entry.request.url, pageURL), + receive: new DomainTiming(entry.timings.receive, entry.request.url, pageURL), + total: new DomainTiming(total, entry.request.url, pageURL), count: 1 }; } else { - console.log('Missing timings in the HAR'); + log.log('info', 'Missing timings in the HAR'); + } } }); @@ -73,4 +75,4 @@ exports.generateResults = function() { exports.clear = function() { domains = {}; -}; +}; \ No newline at end of file diff --git a/lib/domainTiming.js b/lib/domainTiming.js new file mode 100644 index 000000000..00b6afc56 --- /dev/null +++ b/lib/domainTiming.js @@ -0,0 +1,21 @@ +Stats = require('fast-stats').Stats; + +function DomainTiming(time, url, pageUrl) { + this.stats = new Stats().push(time); + this.maxTimeUrl = url; + this.maxTimePageUrl = pageUrl; +}; + +DomainTiming.prototype.add = function(time, url, pageUrl) { + if (time > this.stats.max) { + this.maxTimeUrl = url; + this.maxTimePageUrl = pageUrl; + } + this.stats.push(time); +}; + +DomainTiming.prototype.stats = function() { + return this.stats; +} + +module.exports = DomainTiming; \ No newline at end of file diff --git a/templates/domains.hb b/templates/domains.hb index f75b960c5..29c2e0abb 100644 --- a/templates/domains.hb +++ b/templates/domains.hb @@ -26,14 +26,14 @@ {{#each domains}} {{this.domain}} - {{this.blocked.max}} - {{this.dns.max}} - {{this.connect.max}} - {{this.ssl.max}} - {{this.send.max}} - {{this.wait.max}} - {{this.receive.max}} - {{this.total.max}} + {{this.blocked.stats.max}} + {{this.dns.stats.max}} + {{this.connect.stats.max}} + {{this.ssl.stats.max}} + {{this.send.stats.max}} + {{this.wait.stats.max}} + {{this.receive.stats.max}} + {{this.total.stats.max}} {{this.count}} {{/each}}