plots: dashboard - identify variance over lighthouse versions (#2520)

* up

* fix lint errors but not working

* fix

* pilot dashboard

* pilot

* fix merge issue

* fixup

* fmt

* fixups

* clean up

* fixit

* refactor

* fixup

* cl feedback

* cl fb

* fix

* fixup

* filter out nulls

* update text

* done
This commit is contained in:
Will Chen 2017-07-06 13:33:02 -07:00 коммит произвёл GitHub
Родитель 6df6b0e2fb
Коммит 9561330dc1
7 изменённых файлов: 588 добавлений и 7 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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);;
}

Просмотреть файл

@ -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<number>} 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();

Двоичные данные
plots/dashboard/dropdown_lt_2x.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 142 B

Просмотреть файл

@ -0,0 +1,47 @@
<!--
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.
-->
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="dashboard.css">
<title>Plots Dashboard</title>
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAADjklEQVR4AWI08P/HQEvAQrxSQKvlECfLFYXx75xCY2qmh89GbNvOMjb3v9jOOlxnFWxj206ebQ3b7q6q+z1rNagu8/zvPSZACAABpeUAA0miMgU7SA7JjCraFGwZwECOwvL75dWjsKgWBKtx0jvWo+vkBAFbACCkByMP6nMn48+AVgXB2fzSCwsv22/lMGlUhmJ0AE7BH8dyUUDbUEgN6RzJRSeaPxhdRYR0Inel+7Hd5lBiFpkMAxACc0394//9C4voFHDiAAGLpuOXebdfdHfctgwJKaZRLRKy6ItrSis6RBnVBgGtbHyKTEmJHQoEXoBCE5BCrDeA2ogMUIGDAKEBDEhUqwgMqBYDjW4DQzmuffVdqff42/ZQYYqVcMXGZsMPyCsH3lyJSetxvEaxAQXdjR1HjfwCdIS7lo2DZke26Qe+MXO12OWkGT0O6oE7vMGkMnkYw4aN1KQgMKExhXqswfiov4+a7MQ11XPnbr/5qpKlgACAAQj94Lu271bN9DUecQasIZlNzG72llRAAKJiAi+/BSHrSFjRvQhg3DEKEqJh08tsmLTx597+f6enr4cc2Zpk57pihfX24dW7RHcOLLUbJYhJSl0ErQCI9BVXH/XrO97QasuvQQSiECa0BrQCIIJp6X9T/r8QG6L71WYSqCoIIGo2BZDUBnS/D9EA9Nun1iYvbM0MFExIDQRoKFatc1Z6zrm5uWeObJotq0BGV9FuQBWq5a4Fw3PPz848rZHstZSuA5FWAFSMP2nOppOOGpl6qh9PCSg0IFyHKjSQyDNQHTru2t75NOEe0fsf246oAmFkI6vCdnWvbQFQFCKx8vCswV8TrDLiDLgH4Nr7RAtNsrC9d8sfk7b8ls4igdNy8CQKAISlsB0FjZfd3Lfp155tf8fKI4BxZZIj/oTdVEAIAcJFOCmzauHG71I7/rdreUAgAqpDP05fDARCAQQARwEIBQSVxq0FyaLvZZtevpHa8WHw8cft6cpxlq8eAJtIhnSbWDf951yx3y13OqUuu5qyGgkxCgGFh9cDihDGbTa6BqvT1lWmrav3bmt2ZMJ4mU6TGgIC4DBzcv/JqAau1WhzSt3x9Ixk/4Jk/8J4ZrrViFMA4W6A7+WK8xcVjvyrOmVD0FbAXokcT48r+xVqLKvuJYbmpNadnlp3mpufJHOe/GXktM+r09bT8kEdq9BRYAbGSgzP7ll82U71Mc+ZFooXgwAAAABJRU5ErkJggg==">
</head>
<body>
<nav id="nav">
<span class="title">
Plots dashboard
</span>
<span class="select-metric">
Metric:
<select class="dth-select" id="select-metric">
</select>
</span>
<span class="select-metric">
Number of batches:
<select class="dth-select" id="select-number-of-batches">
<option checked="">All</option>
<option checked="">5</option>
<option checked="">10</option>
<option checked="">15</option>
</select>
</span>
<span class="notes">
<span>Error bands represent 80%ile and 20%ile</span>
<span>Note: null results are filtered out</span>
</span>
</nav>
<div id="charts"></div>
<script src="https://cdn.plot.ly/plotly-1.25.2.min.js"></script>
<script src="../out/dashboard-results.js"></script>
<script src="./dashboard.js"></script>
</body>
</html>

156
plots/generate-dashboard.js Normal file
Просмотреть файл

@ -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<string>} 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<string>}
*/
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<string, !BatchResultsBySite}
*/
let GroupByMetricResults; // eslint-disable-line no-unused-vars
/**
* @typedef {!Object<string, !Object<string, !Array<{timing: number}>>>}
*/
let BatchResultsBySite; // eslint-disable-line no-unused-vars

Просмотреть файл

@ -1,7 +0,0 @@
{
"main": "index.js",
"scripts": {
"measure": "node measure.js",
"analyze": "node analyze.js"
}
}