diff --git a/plots/README.md b/plots/README.md index a57a66feaad5..e2830043fd51 100644 --- a/plots/README.md +++ b/plots/README.md @@ -24,6 +24,14 @@ $ node measure.js --out out-123 # This will launch the charts web page in the browser # node analyze.js {out_directory} $ node analyze.js ./out-hello + +# Generate dashboard using a parent folder with multiple batch results +$ node generate-dashboard.js out-parent-folder + +# Or you can specify each batch result explicitly +$ node generate-dashboard.js out-1 out-2 out-3 + + ``` ### Advanced usage diff --git a/plots/dashboard/dashboard.css b/plots/dashboard/dashboard.css new file mode 100644 index 000000000000..6d594f6f616e --- /dev/null +++ b/plots/dashboard/dashboard.css @@ -0,0 +1,125 @@ +/** + * @license Copyright 2017 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ + +* { + box-sizing: border-box; +} + +:root { + --header-height: 60px; + --primary-color: #3367d6; + --form-color: #333; + --overlay-offset: 50px; +} + +body { + font-family: "Roboto", + -apple-system, + BlinkMacSystemFont, + "Segoe UI", + "Oxygen", + "Ubuntu", + "Cantarell", + "Fira Sans", + "Droid Sans", + "Helvetica Neue", + sans-serif; + color: #555; + width: 100%; + margin-top: calc(var(--header-height) + 35px); +} + +#nav { + position: fixed; + top: 0; + left: 0; + height: var(--header-height); + width: 100%; + color: white; + padding: 15px; + font-size: 16px; + background-color: var(--primary-color); + box-shadow: 0 2px 4px rgba(0, 0, 0, .28); + z-index: 1; +} + +.notes { + margin-top: -10px; + flex-direction: column; + padding-left: 20px; + font-size: 12px; + font-style: italic; + display: inline-flex; +} + +.title { + font-size: 1.5rem; +} + +.dth-select { + background-image: url(./dropdown_lt_2x.png); + color: var(--form-color); + background-color: #fff; + -webkit-appearance: none; + border-color: rgba(0, 0, 0, 0.2); + position: relative; + background-size: 12px 12px; + background-repeat: no-repeat; + background-position: right 6px center; + padding-left: 4px; + padding-right: 24px; + font-size: 12px; + line-height: 16px; +} + +.select-metric { + padding-left: 24px; + font-size: 0.9rem; +} + +.plot-container.plotly { + margin: -25px; +} + +.dth-button { + left: 0; + position: absolute; + top: 35px; + cursor: pointer; + color: var(--primary-color);; + background-color: #fff; + border-color: rgba(0, 0, 0, 0.2); + padding: 3px 12px; + font-weight: 600; + -webkit-appearance: none; + margin: 0; + outline: 0; + height: 24px; + font-size: 12px; + line-height: 16px; + border: 1px solid; + border-radius: 2px; +} + +#overlay { + position: absolute; + width: 100%; + height: 100vh; + top: 0; + background: #e3f2f7; +} + +#overlay .close-button { + position: absolute; + top: calc(var(--header-height) + 30px); + left: var(--overlay-offset);; +} + +#overlay .chart { + position: absolute; + top: calc(var(--header-height) + 100px); + left: var(--overlay-offset);; +} diff --git a/plots/dashboard/dashboard.js b/plots/dashboard/dashboard.js new file mode 100644 index 000000000000..dfb6df235ccb --- /dev/null +++ b/plots/dashboard/dashboard.js @@ -0,0 +1,252 @@ +/** + * @license Copyright 2017 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; + +/* global Plotly, dashboardResults */ +/* eslint-env browser */ + +class Dashboard { + constructor(metrics, charts) { + this._charts = charts; + this._currentMetric = metrics[0]; + this._numberOfBatchesToShow = 0; + this._initializeSelectMetricControl(metrics); + this._initializeSelectNumberOfBatchesToShow(); + } + + render() { + this._charts.render(this._currentMetric, this._numberOfBatchesToShow); + } + + _initializeSelectMetricControl(metrics) { + const metricsControl = document.getElementById('select-metric'); + for (const metric of metrics) { + const option = document.createElement('option'); + option.label = metric; + option.value = metric; + metricsControl.appendChild(option); + } + metricsControl.addEventListener('change', e => this._onSelectMetric(e), false); + } + + _onSelectMetric(event) { + this._currentMetric = event.target.value; + this.render(); + } + + _initializeSelectNumberOfBatchesToShow() { + const control = document.getElementById('select-number-of-batches'); + control.addEventListener('change', e => this._onSelectNumberOfBatchesToShow(e), false); + } + + _onSelectNumberOfBatchesToShow(event) { + if (event.target.value === 'all') { + this._numberOfBatchesToShow = 0; + } else { + this._numberOfBatchesToShow = parseInt(event.target.value, 10); + } + this.render(); + } +} + +class Charts { + constructor(renderingScheduler) { + this._renderingScheduler = renderingScheduler; + this._elementId = 1; + this._layout = { + width: 400, + height: 300, + xaxis: { + showgrid: false, + zeroline: false, + tickangle: 60, + showticklabels: false + }, + yaxis: { + zeroline: true, + rangemode: 'tozero' + }, + showlegend: false, + titlefont: { + family: `"Roboto", -apple-system, BlinkMacSystemFont, sans-serif`, + size: 14 + } + }; + } + + render(currentMetric, numberOfBatchesToShow) { + Utils.removeChildren(document.getElementById('charts')); + for (const [metricName, site] of Object.entries(dashboardResults[currentMetric])) { + const percentiles = Object.entries(site) + .map(([batchName, batch]) => { + return { + x: batchName, + higher: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.8), + median: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.5), + lower: Utils.calculatePercentile(batch.map(metric => metric.timing), 0.2) + }; + }) + .slice(-1 * numberOfBatchesToShow); + + const median = { + x: percentiles.map(r => r.x), + y: percentiles.map(r => r.median), + type: 'scatter', + mode: 'line', + name: 'median' + }; + + const errorBands = { + x: percentiles.map(r => r.x).concat(percentiles.map(r => r.x).reverse()), + y: percentiles.map(r => r.higher).concat(percentiles.map(r => r.lower).reverse()), + fill: 'toself', + fillcolor: 'rgba(0,176,246,0.2)', + line: {color: 'transparent'}, + name: 'error bands', + showlegend: false, + type: 'scatter' + }; + this._renderPreviewChart([median, errorBands], metricName); + } + } + + _renderPreviewChart(data, title) { + this._renderingScheduler.enqueue(_ => { + Plotly.newPlot( + this._createPreviewChartElement(data, title), + data, + Object.assign({title}, this._layout) + ); + }); + } + + _createPreviewChartElement(data, title) { + const chart = document.createElement('div'); + chart.style = 'display: inline-block; position: relative'; + chart.id = 'chart' + this._elementId++; + + const button = document.createElement('button'); + button.className = 'dth-button show-bigger-button'; + button.appendChild(document.createTextNode('Focus')); + button.addEventListener('click', () => this._onFocusChart(data, title), false); + chart.appendChild(button); + + const container = document.getElementById('charts'); + container.appendChild(chart); + return chart.id; + } + + _onFocusChart(data, title) { + const overlay = document.createElement('div'); + overlay.id = 'overlay'; + document.body.appendChild(overlay); + + document.getElementById('charts').style.display = 'none'; + + const closeButton = document.createElement('button'); + closeButton.className = 'dth-button close-button'; + closeButton.appendChild(document.createTextNode('Close')); + closeButton.addEventListener('click', onCloseFocusedChart, false); + overlay.appendChild(closeButton); + + const chart = document.createElement('div'); + chart.className = 'chart'; + overlay.appendChild(chart); + this._renderFocusedChart(data, title, chart); + + function onCloseFocusedChart() { + document.getElementById('charts').style.display = 'block'; + document.body.removeChild(overlay); + } + } + + _renderFocusedChart(data, title, element) { + Plotly.newPlot( + element, + data, + Object.assign({title}, this._layout, { + width: document.body.clientWidth - 100, + height: 500 + }) + ); + } +} + +class RenderingScheduler { + constructor() { + this._queue = []; + } + + enqueue(fn) { + const isFirst = this._queue.length == 0; + this._queue.push(fn); + if (isFirst) { + this._render(); + } + } + + _render() { + window.requestAnimationFrame(_ => { + const plotFn = this._queue.shift(); + if (plotFn) { + plotFn(); + this._render(); + } + }); + } +} + +const Utils = { + /** + * @param {!Element} parent + */ + removeChildren(parent) { + while (parent.firstChild) { + parent.removeChild(parent.firstChild); + } + }, + + /** + * Calculate the value at a given percentile + * Based on: https://gist.github.com/IceCreamYou/6ffa1b18c4c8f6aeaad2 + * @param {!Array} array + * @param {number} percentile should be from 0 to 1 + */ + calculatePercentile(array, percentile) { + const sorted = array.filter(x => x !== null).sort((a, b) => a - b); + + if (sorted.length === 0) { + return 0; + } + if (sorted.length === 1 || percentile <= 0) { + return sorted[0]; + } + if (percentile >= 1) { + return sorted[sorted.length - 1]; + } + + const index = (sorted.length - 1) * percentile; + const lower = Math.floor(index); + const upper = lower + 1; + const weight = index % 1; + + return sorted[lower] * (1 - weight) + sorted[upper] * weight; + } +}; + +function main() { + /** + * Navigation Start is usually not a very informative metric. + */ + const metrics = Object.keys(dashboardResults).filter(m => m !== 'Navigation Start'); + + const renderingScheduler = new RenderingScheduler(); + const charts = new Charts(renderingScheduler); + const dashboard = new Dashboard(metrics, charts); + dashboard.render(); +} + +main(); diff --git a/plots/dashboard/dropdown_lt_2x.png b/plots/dashboard/dropdown_lt_2x.png new file mode 100644 index 000000000000..5ac406d92555 Binary files /dev/null and b/plots/dashboard/dropdown_lt_2x.png differ diff --git a/plots/dashboard/index.html b/plots/dashboard/index.html new file mode 100644 index 000000000000..f46a8af0f4c7 --- /dev/null +++ b/plots/dashboard/index.html @@ -0,0 +1,47 @@ + + + + + + + + Plots Dashboard + + + + + + +
+ + + + + + diff --git a/plots/generate-dashboard.js b/plots/generate-dashboard.js new file mode 100644 index 000000000000..d01a4a802cd2 --- /dev/null +++ b/plots/generate-dashboard.js @@ -0,0 +1,156 @@ +/** + * @license Copyright 2017 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + */ +'use strict'; +const childProcess = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +const opn = require('opn'); +const args = require('yargs') + .wrap(Math.min(process.stdout.columns, 120)) + .example('node $0 out-parent-folder') + .example('node $0 out-1 out-2 out-3') + .help('help') + .demand(1) + .argv; + +const constants = require('./constants'); +const utils = require('./utils'); + +/** + * Run analyze.js on each of the outs and then + * aggregate all the results by metric. + */ +function main() { + const inputPaths = getInputPaths(); + analyzeInputPaths(inputPaths); + const results = {}; + for (const inputPath of inputPaths) { + const result = fs.readFileSync( + path.resolve(inputPath, constants.GENERATED_RESULTS_FILENAME), + 'utf-8' + ); + results[path.basename(inputPath)] = /** @type {!ResultsByMetric} */ (JSON.parse( + result.replace('var generatedResults = ', '') + )); + } + + const groupByMetricResults = groupByMetric(results); + + if (!utils.isDir(constants.OUT_PATH)) { + fs.mkdirSync(constants.OUT_PATH); + } + fs.writeFileSync( + path.resolve(constants.OUT_PATH, 'dashboard-results.js'), + `const dashboardResults = ${JSON.stringify(groupByMetricResults, undefined, 2)}` + ); + + if (process.env.CI) { + return; + } + // eslint-disable-next-line no-console + console.log('Opening the dashboard web page...'); + opn(path.resolve(__dirname, 'dashboard', 'index.html')); +} + +main(); + +/** + * + * @param {!AggregatedResults} results + * @return {!GroupByMetricResults} + */ +function groupByMetric(results) { + return Object.keys(results).reduce( + (acc, batchId) => { + const batchResults = results[batchId]; + Object.keys(batchResults).forEach(metricId => { + if (!acc[metricId]) { + acc[metricId] = {}; + } + const sites = batchResults[metricId]; + sites.forEach(site => { + if (!acc[metricId][site.site]) { + acc[metricId][site.site] = {}; + } + acc[metricId][site.site][batchId] = site.metrics; + }); + }); + return acc; + }, + {} + ); +} + +/** + * @param {!Array} inputPaths + */ +function analyzeInputPaths(inputPaths) { + for (const inputPath of inputPaths) { + // Prevent analyze script from opening the results in browser + childProcess.execSync(`node analyze.js ${inputPath}`, { + env: Object.assign({}, process.env, {CI: '1'}) + }); + } +} + +/** + * Returns a list of out paths generated by measure.js + * @return {!Array} + */ +function getInputPaths() { + const relativePaths = args._; + let inputPaths = []; + relativePaths.forEach(relativePath => { + const fullPath = path.resolve(__dirname, relativePath); + if (isOutParentFolder(fullPath)) { + const paths = fs.readdirSync(fullPath) + .map(pathComponent => path.resolve(fullPath, pathComponent)) + .filter(inputPath => utils.isDir(inputPath)); + inputPaths = inputPaths.concat(paths); + return; + } + inputPaths.push(fullPath); + }); + return inputPaths; +} + +/** + * Detects if any of the directory's children is a valid input path + * @param {boolean} fullPath + */ +function isOutParentFolder(fullPath) { + for (const maybeOutFolder of fs.readdirSync(fullPath)) { + const maybeOutPath = path.resolve(fullPath, maybeOutFolder); + if (!utils.isDir(maybeOutPath)) { + continue; + } + for (const maybeSiteFolder of fs.readdirSync(maybeOutPath)) { + const maybeSitePath = path.resolve(maybeOutPath, maybeSiteFolder); + if (!utils.isDir(maybeSitePath)) { + continue; + } + for (const maybeRunFolder of fs.readdirSync(maybeSitePath)) { + const maybeLighthouseResults = path.resolve( + maybeSitePath, maybeRunFolder, constants.LIGHTHOUSE_RESULTS_FILENAME); + if (utils.isFile(maybeLighthouseResults)) { + return true; + } + } + } + } + return false; +} + +/** + * @typedef {!Object>>} + */ +let BatchResultsBySite; // eslint-disable-line no-unused-vars diff --git a/plots/package.json b/plots/package.json deleted file mode 100644 index 22830cc78a54..000000000000 --- a/plots/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "main": "index.js", - "scripts": { - "measure": "node measure.js", - "analyze": "node analyze.js" - } -}