Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Perf-X server Rerun lighthouse on POST request. #1393

Merged
merged 20 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lighthouse-cli/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ function runLighthouse(url: string,
})
.then((results: Results) => {
if (flags.view) {
return performanceXServer.serveAndOpenReport({url, flags, config}, results);
return performanceXServer.serveAndOpenReport({url, flags}, results);
}
})
.then(() => chromeLauncher.kill())
Expand Down
60 changes: 50 additions & 10 deletions lighthouse-cli/performance-experiment/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@
* @fileoverview Server script for Project Performance Experiment.
*
* Functionality:
* Host and open report page.
* Host experiment.
* Report can be access via URL http://localhost:[PORT]/
* Rerun data can be access via URL http://localhost:[PORT]/rerun.
* This will rerun lighthouse with same parameters and rerun results in JSON format
*/

const http = require('http');
const parse = require('url').parse;
const path = require('path');
const opn = require('opn');
const stringify = require('json-stringify-safe');
const log = require('../../lighthouse-core/lib/log');
const ReportGenerator = require('../../lighthouse-core/report/report-generator');
const lighthouse = require('../../lighthouse-core');
const perfOnlyConfig = require('../../lighthouse-core/config/perf.json');

/**
* Start the server with an arbitrary port and open report page in the default browser.
Expand All @@ -37,7 +43,9 @@ const ReportGenerator = require('../../lighthouse-core/report/report-generator')
* @return {!Promise<string>} Promise that resolves when server is closed
*/
let lhResults;
let lhParams;
function serveAndOpenReport(lighthouseParams, results) {
lhParams = lighthouseParams;
lhResults = results;
return new Promise(resolve => {
const server = http.createServer(requestHandler);
Expand All @@ -55,24 +63,56 @@ function serveAndOpenReport(lighthouseParams, results) {

function requestHandler(request, response) {
const pathname = path.normalize(parse(request.url).pathname);

if (pathname === '/') {
reportRequestHandler(request, response);
if (request.method === 'GET') {
if (pathname === '/') {
reportRequestHandler(request, response);
} else {
response.writeHead(404);
response.end('404: Resource Not Found');
}
} else if (request.method === 'POST') {
if (pathname === '/rerun') {
rerunRequestHandler(request, response);
} else {
response.writeHead(404);
response.end('404: Resource Not Found');
}
} else {
response.writeHead(400);
response.write('400 - Bad request');
response.end();
response.writeHead(405);
response.end('405: Method Not Supported');
}
}

function reportRequestHandler(request, response) {
const reportGenerator = new ReportGenerator();
const html = reportGenerator.generateHTML(lhResults, 'cli');
const html = reportGenerator.generateHTML(lhResults, 'perf-x');
response.writeHead(200, {'Content-Type': 'text/html'});
response.write(html);
response.end();
response.end(html);
}

function rerunRequestHandler(request, response) {
try {
let message = '';
request.on('data', data => message += data);

request.on('end', () => {
const additionalFlags = JSON.parse(message);

// Add more to flags without changing the original flags
const flags = Object.assign({}, lhParams.flags, additionalFlags);
lighthouse(lhParams.url, flags, perfOnlyConfig).then(results => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we rerun tests with configurations (e.g. URL blocking patterns), we only care about performance. So use perfOnlyConfig here to get faster result.

results.artifacts = undefined;
response.writeHead(200, {'Content-Type': 'text/json'});
response.end(stringify(results));
});
});
} catch (e) {
response.writeHead(500);
response.end('500: Internal Server Error');
}
}


module.exports = {
serveAndOpenReport
};
19 changes: 15 additions & 4 deletions lighthouse-core/report/report-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,21 @@ class ReportGenerator {

/**
* Gets the script for the report UI
* @return {string}
* @param {string} reportContext
* @return {Array<string>}
*/
getReportJS() {
return fs.readFileSync(path.join(__dirname, './scripts/lighthouse-report.js'), 'utf8');
getReportJS(reportContext) {
const scriptList = [];

if (reportContext === 'perf-x') {
scriptList.push(fs.readFileSync(path.join(__dirname, './scripts/perf-x-api.js')));
}

if (reportContext !== 'devtools') {
scriptList.push(fs.readFileSync(path.join(__dirname, './scripts/lighthouse-report.js')));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was the 'utf8' removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. I didn't add this when I rewrite this method because I thought 'utf8' is default anyway : (
I will add it back in the next commit. Would you mind to tell me why it's necessary though?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. Fixed in the next update! Thanks for pointing out!

}

return scriptList;
}

/**
Expand Down Expand Up @@ -303,7 +314,7 @@ class ReportGenerator {
lhresults: this._escapeScriptTags(JSON.stringify(results, null, 2)),
css: this.getReportCSS(),
reportContext: reportContext,
script: reportContext === 'devtools' ? '' : this.getReportJS(),
scripts: this.getReportJS(reportContext),
aggregations: results.aggregations,
auditsByCategory: this._createPWAAuditsByCategory(results.aggregations)
});
Expand Down
7 changes: 7 additions & 0 deletions lighthouse-core/report/scripts/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow us to use /* exported rerunLighthouse */ in perf-x-api.js

"parserOptions": {
"ecmaFeatures": {
"globalReturn": false,
}
}
}
39 changes: 39 additions & 0 deletions lighthouse-core/report/scripts/perf-x-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright 2016 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';
/**
* @fileoverview Report script for Project Performance Experiment.
*
* Include functions for supporting interation between report page and Perf-X server.
* Currently not exposed to users. Only for testing Perf-X server.
*/

/**
* Send request to rerun lighthouse with additional cli-flags.
* Some cli-flags will be ignored.
- Flags which are not applicable to rerun lighthouse (e.g. --list-all-audits, --help)
- config related flags (e.g. --config-path). Always use perf-olny config for rerunning.
* @param {!Object} additionalFlags
*/

/* exported rerunLighthouse */
function rerunLighthouse(additionalFlags={}) {
fetch('/rerun', {method: 'POST', body: JSON.stringify(additionalFlags)})
.then(response => response.json())
.then(console.log.bind(console));
}
6 changes: 3 additions & 3 deletions lighthouse-core/report/templates/report-template.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAEfElEQVR4Aa2WA5B0RxDH/z3zuDp/tq2Lbdu2XUoxKMW2bdu2nZxte/dpplO3hTjZ2s0z5zftpkVrFXJYGCBAggEoUPY2p0XkOLoJNoE+yH5II3vLOQJyHL0bsgpiC9G1meishuiBzJEhchm9HXIL0fu5e949TuX1cvVbxplbiu6O3BjiP19HEFMpuNI9Z4O8XpIlKb6SbrnQOG0WeQGkKATAgAVdz3Sw/G6BenwoXAnls/JH1IqZ0fN70DeN2Q84b4AA0jBWkHewfXsUh0ykKSZTcVGamoincKR7xyrKpGH8uxDGv0vgmOqXZuf1o87ccP6wCpIkgb5ePZw2Yvj0icN+eshdMUcFIRVgAwIyGHTKUGqLEluUOWJqkpISRRi2S+BDgAuyAYHhwYsIxCACM/wMSQEBNXkLAjhvFVnEP2WMYzdvO6/4muDZMVCavSD8ujHDQkmxe90VP8y+5s1oznSoCJSfkRkC07yBhd3XRz+m1bd9/tN14XMd3kutI4/5s1qfnmEPjWgIcL5GZoCQUcbA56Cpw4g8TguxZBYiloomfGRCE1SAigKIxZKfG1rCW7cddOhIFIEEEWutIeLmyy+nXni8bMkMDiORv5uaxHW+NRSbFhbL0AeBI9MOyZyRbkpO2D2WVQ7lgSg/L6IsA8xJ0zMiX5GMpFnc37L+/bs3uX/d8rrPkYJQXFAuIgJ8UNwMLaest7Hy3Vs2e/S4OZ9dB4YV09CgAnOR0IwkzR5p2+SNSzd+9Jhp394M6YbJBRCuISYAFJpN5SQAy2q/SP1wg3JnRIlFDAIRokwi6If4XyoaQzohYgmAASGiCWv4W6QqR5asQgBNVGjBAWDLcSAug1Fj9FOvaH7tztd+fOhDzZXbYoiVKBwgkAiGMdYzPnVN1V6PfXLI9dUb9vHtZBwZ6RAYlHegAVkNBAjnzKzf4om6WVso2zFD3/HGyYZpGimjsJJJgBKUmOCarfdpXLYdAbY/QVplwVLHHMMEc4FGZhg2HBFZfppYg6GF4bkJGXiLWz6aYfg+CyoEoDVcC4ahmaGk6Ttx2x9b9t3Lmz9+9tp3Li5yw0CD8sumTExi8ujGoFwzbRuxgZ45tR/M+OlpZ+ATuHP9otluvxfqJBGzBmn6bwALhmQApAT5knyoYcRKsKC3Y+U3rxZ/8xzGPkNsLcwNSIcp3ZfqFpkxUNIgB7CZDQUAikjTHwEENjSYaFxiEIiACuYZozx1yC8dSZZ31U9cXl9W1bfvosDYkXRoMMcjuyjggcq37cGl0UBK95RQV5LasqOVACnNQiMSxKCFlSFFEp0EG3p1v15Rr+b/oqdWcVETOx0whpXwe3k+yAKHwB+63hR1JDjSUYnwp9PoPNG3TDSvkFVLxA/TMQTMBDsRLZrGPDVUO34TrX9LzfyQ3ToiQBVBJ6BtsCRGjEJiZqLfezDAabY0AaRAAeQE5CgjJG+u6NnS+HFH451NqSZO8879PNjpLl3xFnEMYQWUAzBIAxrEyHUhsJjcQBABzEEWA2Jkc/Ojk38Fe4MpHMjZ+XoAAAAASUVORK5CYII=">
<title>Lighthouse report: {{ url }}</title>
<style>{{{ css }}}</style>
{{#if script }}
<script>{{{ script }}}</script>
{{/if}}
{{#each scripts }}
<script>{{{ this }}}</script>
{{/each}}
</head>
<body>

Expand Down