From a4e85d1ab8f771175cb4d55ab3b3227e548f5ff9 Mon Sep 17 00:00:00 2001 From: soulgalore Date: Fri, 5 Jun 2015 15:59:01 +0200 Subject: [PATCH] fetch custom metrics using WPT closing #678 --- CHANGELOG.md | 4 +++- lib/analyze/webpagetest.js | 4 ++++ lib/cli.js | 10 +++++++++ lib/collectors/pages.js | 11 ++++++++++ lib/graphite/graphiteCollector.js | 4 ++++ lib/util/hbHelpers.js | 4 ++++ templates/partials/wpt.hb | 36 +++++++++++++++++++++++++++++++ 7 files changed, 72 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a8ee373cf..0381e0f848 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ version 3.5.1 (unreleased) ------------------------ -* Holy cow, we now support WebPageTest scripting ( https://sites.google.com/a/webpagetest.org/docs/using-webpagetest/scripting)! Every occurrence of {{{URL}}} in your script will be replaced with the URL that is actually going to be tested. +* Holy cow, we now support WebPageTest scripting ( https://sites.google.com/a/webpagetest.org/docs/using-webpagetest/scripting)! Every occurrence of {{{URL}}} in your script will be replaced with the URL that is actually going to be tested. Feed the script file to sitespeed.io using --wptScript + +* When we are at it, also support custom scripts to collect metrics for WebPageTest! Feed your custom javascript metrics file using --wptCustomMetrics. Read more here: https://sites.google.com/a/webpagetest.org/docs/using-webpagetest/custom-metrics version 3.5 ------------------------ diff --git a/lib/analyze/webpagetest.js b/lib/analyze/webpagetest.js index 06c6471a9d..862a0afa10 100644 --- a/lib/analyze/webpagetest.js +++ b/lib/analyze/webpagetest.js @@ -99,6 +99,10 @@ function analyzeUrl(args, asyncDoneCallback) { video: true }); + if (config.wptCustomMetrics) { + wptOptions.custom = config.wptCustomMetrics; + } + // set basic auth if it is configured if (config.basicAuth) { wptOptions.login = config.basicAuth.split(':')[0]; diff --git a/lib/cli.js b/lib/cli.js index 2540baab6a..164e17e16b 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -385,6 +385,16 @@ var cli = nomnom.help( return fileHelper.getFileAsString(path); } }, + wptCustomMetrics: { + metavar: '', + help: 'Fetch metrics from your page using Javascript', + callback: function(path) { + return validatePathOption('wptCustomMetrics', path); + }, + transform: function(path) { + return fileHelper.getFileAsString(path); + } + }, wptHost: { metavar: '', help: 'The domain of your WebPageTest instance.' diff --git a/lib/collectors/pages.js b/lib/collectors/pages.js index a1fddae741..e06a0a436c 100644 --- a/lib/collectors/pages.js +++ b/lib/collectors/pages.js @@ -215,6 +215,17 @@ function collectWPT(pageData, p) { }); } + // and custom metrics + var customMetrics = browserAndLocation.response.data.median[view].custom; + if (customMetrics) { + customMetrics.value.forEach(function(metricName) { + p.wpt[location][browser][connectivity][view][metricName] = { + 'v': browserAndLocation.response.data.median[view][metricName], + 'unit': '' + }; + }); + } + others.forEach(function(metric) { p.wpt[location][browser][connectivity][view][metric] = { 'v': browserAndLocation.response.data.median[view][metric], diff --git a/lib/graphite/graphiteCollector.js b/lib/graphite/graphiteCollector.js index 274655af9f..0f41f9850a 100644 --- a/lib/graphite/graphiteCollector.js +++ b/lib/graphite/graphiteCollector.js @@ -171,10 +171,14 @@ GraphiteCollector.prototype._getWPTStats = function(page, urlKey) { Object.keys(page.wpt[location][browser]).forEach(function(connectivity) { Object.keys(page.wpt[location][browser][connectivity]).forEach(function(view) { Object.keys(page.wpt[location][browser][connectivity][view]).forEach(function(metric) { + // we can have custom metrics that are not numbers + // so lets skip them + if (!isNaN(metric)) { statistics += self.namespace + '.' + urlKey + '.wpt.' + location + '.' + connectivity + '.' + browser + '.' + view + '.' + metric + '.median ' + page.wpt[location][browser][connectivity][view][metric].v + self.timeStamp; + } }); }); }); diff --git a/lib/util/hbHelpers.js b/lib/util/hbHelpers.js index 40ff1d5c14..363d83f15e 100644 --- a/lib/util/hbHelpers.js +++ b/lib/util/hbHelpers.js @@ -185,6 +185,10 @@ module.exports.registerHelpers = function registerHelpers() { return util.getWPTKey(location, connectivity); }); + hb.registerHelper('getWPTCustomMetricValue', function(view, metricName) { + return view[metricName]; + }); + hb.registerHelper('getColumnsMeta', function(column, columnsMeta, ruleDictionary, type) { // TODO major cleanup // strip diff --git a/templates/partials/wpt.hb b/templates/partials/wpt.hb index bf4fd3ed92..55866e7a02 100644 --- a/templates/partials/wpt.hb +++ b/templates/partials/wpt.hb @@ -62,6 +62,42 @@ {{/if}} + + + {{#if response.data.median.firstView.custom.value}} +

Custom Metrics

+
+ + + + + {{#each response.data.median.firstView.custom.value}} + + {{/each}} + + + + + + + {{#each response.data.median.firstView.custom.value}} + + {{/each}} + + {{#if response.data.median.repeatView}} + + + {{#each response.data.median.repeatView.custom.value}} + + {{/each}} + + {{/if}} + +
View{{this}}
First View{{getWPTCustomMetricValue ../response.data.median.firstView this}}
Repeat View{{getWPTCustomMetricValue ../response.data.median.repeatView this}}
+
+ + {{/if}} +

Waterfall first view