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

updates for using lighthouse v3 #178

Merged
merged 26 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
23 changes: 13 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
language: node_js
matrix:
include:
- node_js: "6"
- node_js: "7"
env: RUN_RECIPES=1
allow_failures:
- node_js: "7"
- node_js: "8"
- node_js: "9"
env: RUN_RECIPES=1
dist: trusty
cache:
yarn: true
directories:
- node_modules
before_install:
- npm install -g --no-progress yarn@v0.22.0
- lighthouse-extension/node_modules
- lighthouse-viewer/node_modules
- /home/travis/.rvm/gems/
Copy link
Owner

Choose a reason for hiding this comment

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

the two lighthouse-* folders dont make sense to me here.
and i dont think you're running ruby, so you can nuke this one too.

install:
- yarn install --no-progress
# if our e2e tests fail in the future it might be that we are not compatible
# with the latest puppeteer api so we probably need to run on chromimum
# @see https://github.com/GoogleChrome/lighthouse/pull/4640/files#r171425004
- export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=1
- yarn
before_script:
- export DISPLAY=:99.0
- export LIGHTHOUSE_CHROMIUM_PATH="$(pwd)/chrome-linux/chrome"
# see comment above about puppeteer
- export CHROME_PATH="$(which google-chrome-stable)"
- sh -e /etc/init.d/xvfb start
- sleep 3 # wait for xvfb to boot
- yarn build
addons:
chrome: stable
4 changes: 1 addition & 3 deletions lib/chart/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const eol = require('os').EOL;

import {Timing, ChartOptions} from '../../types/types';

const drawChart = (timings: Timing[], options: ChartOptions) => {
export const drawChart = (timings: Timing[], options: ChartOptions) => {
const {lmargin, width, xlabel, xmin, xmax} = options;

const normalizedTimings = timings.map(value => {
Expand Down Expand Up @@ -51,5 +51,3 @@ const drawChart = (timings: Timing[], options: ChartOptions) => {
console.log([chartTop, chart, chartBottom, chartScale].join(eol));
console.log();
};

module.exports = drawChart;
21 changes: 8 additions & 13 deletions lib/expectations.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE

import { Timing, ExpectationMetrics, NormalizedExpectationMetrics } from '../types/types';
const { getAssertionMessage, getMessageWithPrefix } = require('./utils/messages');
import {Timing, ExpectationMetrics, NormalizedExpectationMetrics} from '../types/types';

function validateMetrics(metrics: ExpectationMetrics) {
import {getAssertionMessage, getMessageWithPrefix} from './utils/messages';

export const validateMetrics = (metrics: ExpectationMetrics) => {
const metricsKeys = Object.keys(metrics);

if (!metrics || !metricsKeys.length) {
Expand All @@ -18,9 +19,9 @@ function validateMetrics(metrics: ExpectationMetrics) {
process.exit(1);
}
});
}
};

function normalizeMetrics(metrics: ExpectationMetrics): NormalizedExpectationMetrics {
export const normalizeExpectationMetrics = (metrics: ExpectationMetrics): NormalizedExpectationMetrics => {
let normalizedMetrics: NormalizedExpectationMetrics = {};
Object.keys(metrics).forEach(key => {
normalizedMetrics[key] = {
Expand All @@ -29,9 +30,9 @@ function normalizeMetrics(metrics: ExpectationMetrics): NormalizedExpectationMet
};
});
return normalizedMetrics;
}
};

function checkExpectations(metricsData: Timing[], expectationMetrics: NormalizedExpectationMetrics) {
export const checkExpectations = (metricsData: Timing[], expectationMetrics: NormalizedExpectationMetrics) => {
metricsData.forEach(metric => {
const metricName = metric.id;
const expectationValue = expectationMetrics[metricName];
Expand All @@ -50,10 +51,4 @@ function checkExpectations(metricsData: Timing[], expectationMetrics: Normalized
console.log(msg);
}
});
}

module.exports = {
validateMetrics: validateMetrics,
normalizeMetrics: normalizeMetrics,
checkExpectations: checkExpectations
};
163 changes: 37 additions & 126 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE

import {launch, LaunchedChrome} from 'chrome-launcher';
const lighthouse = require('lighthouse');
const parseChromeFlags = require('lighthouse/lighthouse-cli/run').parseChromeFlags;
const perfConfig: any = require('./lh-config');
import {LHRunner} from "./lh-runner";

const opn = require('opn');
const path = require('path');

const Sheets = require('./sheets/index');
const Chart = require('./chart/chart');
const metrics = require('./metrics');
const expectations = require('./expectations');
const {upload} = require('./upload');
const messages = require('./utils/messages');
const drawChart = require('./chart/chart');
import METRICS from './metrics/metrics';
import {Sheets} from './sheets';
import {adaptMetricsData} from './metrics/metrics-adapter';
import {validateMetrics, normalizeExpectationMetrics, checkExpectations} from './expectations';
import {upload} from './upload';
import {getMessage, getMessageWithPrefix} from './utils/messages';
import {drawChart} from './chart/chart';

import {
MainOptions,
FeatureFlags,
AuthorizeCredentials,
LighthouseResults,
MetricsResults,
TermWritableStream,
PWMetricsResults,
SheetsConfig,
ExpectationMetrics,
NormalizedExpectationMetrics,
Timing
} from '../types/types';

const MAX_LIGHTHOUSE_TRIES = 2;
const getTimelineViewerUrl = (id: string) => `https://chromedevtools.github.io/timeline-viewer/?loadTimelineFromURL=https://drive.google.com/file/d//${id}/view?usp=drivesdk`
const getTimelineViewerUrl = (id: string) => `https://chromedevtools.github.io/timeline-viewer/?loadTimelineFromURL=https://drive.google.com/file/d//${id}/view?usp=drivesdk`;

class PWMetrics {
flags: FeatureFlags = {
Expand All @@ -45,33 +40,28 @@ class PWMetrics {
};
runs: number;
sheets: SheetsConfig;
expectations: ExpectationMetrics | NormalizedExpectationMetrics;
normalizedExpectations: NormalizedExpectationMetrics;
clientSecret: AuthorizeCredentials;
tryLighthouseCounter: number;
launcher: LaunchedChrome;
parsedChromeFlags: Array<string>;

constructor(public url: string, opts: MainOptions) {
this.flags = Object.assign({}, this.flags, opts.flags);
this.runs = this.flags.runs;
this.sheets = opts.sheets;
this.expectations = opts.expectations;
this.clientSecret = opts.clientSecret;
this.tryLighthouseCounter = 0;
const expectations: ExpectationMetrics = opts.expectations;

// normalize path if provided
if (this.flags.chromePath) {
this.flags.chromePath = path.normalize(this.flags.chromePath);
this.flags.chromePath = path.normalize(this.flags.chromePath);
}

if (this.flags.expectations) {
if (this.expectations) {
expectations.validateMetrics(this.expectations);
this.expectations = expectations.normalizeMetrics(this.expectations);
} else throw new Error(messages.getMessageWithPrefix('ERROR', 'NO_EXPECTATIONS_FOUND'));
if (expectations) {
validateMetrics(expectations);
this.normalizedExpectations = normalizeExpectationMetrics(expectations);
} else throw new Error(getMessageWithPrefix('ERROR', 'NO_EXPECTATIONS_FOUND'));
}

this.parsedChromeFlags = parseChromeFlags(this.flags.chromeFlags);
}

async start() {
Expand All @@ -82,23 +72,25 @@ class PWMetrics {

for (let runIndex of runs) {
try {
const currentMetricResult: MetricsResults = await this.run();
const lhRunner = new LHRunner(this.url, this.flags);
const lhTrace = await lhRunner.run();
const currentMetricResult: MetricsResults = await this.recordLighthouseTrace(lhTrace);
if (!resultHasExpectationErrors && this.flags.expectations) {
resultHasExpectationErrors = this.resultHasExpectationErrors(currentMetricResult);
}
metricsResults[runIndex] = currentMetricResult;
console.log(messages.getMessageWithPrefix('SUCCESS', 'SUCCESS_RUN', runIndex, runs.length));
console.log(getMessageWithPrefix('SUCCESS', 'SUCCESS_RUN', runIndex, runs.length));
} catch (error) {
metricsResults[runIndex] = error;
console.error(messages.getMessageWithPrefix('ERROR', 'FAILED_RUN', runIndex, runs.length, error.message));
console.error(getMessageWithPrefix('ERROR', 'FAILED_RUN', runIndex, runs.length, error.message));
}
}

let results: PWMetricsResults = { runs: metricsResults.filter(r => !(r instanceof Error)) };
let results: PWMetricsResults = {runs: metricsResults.filter(r => !(r instanceof Error))};
if (results.runs.length > 0) {
if (this.runs > 1 && !this.flags.submit) {
results.median = this.findMedianRun(results.runs);
console.log(messages.getMessage('MEDIAN_RUN'));
console.log(getMessage('MEDIAN_RUN'));
this.displayOutput(results.median);
} else if (this.flags.submit) {
const sheets = new Sheets(this.sheets, this.clientSecret);
Expand All @@ -107,15 +99,15 @@ class PWMetrics {
}

if (resultHasExpectationErrors && this.flags.expectations) {
throw new Error(messages.getMessage('HAS_EXPECTATION_ERRORS'));
throw new Error(getMessage('HAS_EXPECTATION_ERRORS'));
}

return results;
}

resultHasExpectationErrors(metrics: MetricsResults): boolean {
return metrics.timings.some((timing: Timing) => {
const expectation = this.expectations[timing.id];
const expectation = this.normalizedExpectations[timing.id];
if (!expectation) {
return false;
}
Expand All @@ -124,90 +116,9 @@ class PWMetrics {
});
}

async run(): Promise<MetricsResults> {
try {
let lhResults: LighthouseResults;
await this.launchChrome();

if (process.env.CI) {
// handling CRI_TIMEOUT issue - https://github.com/GoogleChrome/lighthouse/issues/833
this.tryLighthouseCounter = 0;
lhResults = await this.runLighthouseOnCI().then((lhResults:LighthouseResults) => {
// fix for https://github.com/paulirish/pwmetrics/issues/63
return new Promise<LighthouseResults>(resolve => {
console.log(messages.getMessage('WAITING'));
setTimeout(_ => {
return resolve(lhResults);
}, 2000);
});
});
} else {
lhResults = await lighthouse(this.url, this.flags, perfConfig);
}

const metricsResults: MetricsResults = await this.recordLighthouseTrace(lhResults);
await this.killLauncher();

return metricsResults;
} catch (error) {
await this.killLauncher();
throw error;
}
}

async killLauncher() {
if (typeof this.launcher !== 'undefined') {
await this.launcher!.kill();
}
}

async runLighthouseOnCI(): Promise<LighthouseResults> {
try {
return await lighthouse(this.url, this.flags, perfConfig);
} catch(error) {
if (error.code === 'CRI_TIMEOUT' && this.tryLighthouseCounter <= MAX_LIGHTHOUSE_TRIES) {
return await this.retryLighthouseOnCI();
}

if (this.tryLighthouseCounter > MAX_LIGHTHOUSE_TRIES) {
throw new Error(messages.getMessage('CRI_TIMEOUT_REJECT'));
}
}
}

async retryLighthouseOnCI(): Promise<LighthouseResults> {
this.tryLighthouseCounter++;
console.log(messages.getMessage('CRI_TIMEOUT_RELAUNCH'));

try {
return await this.runLighthouseOnCI();
} catch(error) {
console.error(error.message);
console.error(messages.getMessage('CLOSING_CHROME'));
await this.killLauncher();
}
}

async launchChrome(): Promise<LaunchedChrome|Error> {
try {
console.log(messages.getMessage('LAUNCHING_CHROME'));
this.launcher = await launch({
port: this.flags.port,
chromeFlags: this.parsedChromeFlags,
chromePath: this.flags.chromePath
});
this.flags.port = this.launcher.port;
return this.launcher;
} catch(error) {
console.error(error);
await this.killLauncher();
return error;
}
}

async recordLighthouseTrace(data: LighthouseResults): Promise<MetricsResults> {
async recordLighthouseTrace(data: LH.RunnerResult): Promise<MetricsResults> {
try {
const preparedData = metrics.prepareData(data);
const preparedData = adaptMetricsData(data.lhr);

if (this.flags.upload) {
const driveResponse = await upload(data, this.clientSecret);
Expand All @@ -219,7 +130,7 @@ class PWMetrics {
}

if (this.flags.expectations) {
expectations.checkExpectations(preparedData.timings, this.expectations);
checkExpectations(preparedData.timings, this.normalizedExpectations);
}

return preparedData;
Expand All @@ -242,20 +153,20 @@ class PWMetrics {
timings = timings.filter(r => {
// filter out metrics that failed to record
if (r.timing === undefined || isNaN(r.timing)) {
console.error(messages.getMessageWithPrefix('ERROR', 'METRIC_IS_UNAVAILABLE', r.title));
console.error(getMessageWithPrefix('ERROR', 'METRIC_IS_UNAVAILABLE', r.title));
return false;
} else {
return true;
}
// don't chart hidden metrics, but include in json
return !metrics.hiddenMetrics.includes(r.id);
});

const fullWidthInMs = Math.max(...timings.map(result => result.timing));
const maxLabelWidth = Math.max(...timings.map(result => result.title.length));
const stdout = <TermWritableStream>(process.stdout);
const terminalWidth = process.stdout.columns || 90;

drawChart(timings, {
// 90% of terminal width to give some right margin
width: stdout.columns * 0.9 - maxLabelWidth,
width: terminalWidth * 0.9 - maxLabelWidth,
xlabel: 'Time (ms) since navigation start',

xmin: 0,
Expand All @@ -268,12 +179,12 @@ class PWMetrics {
}

findMedianRun(results: MetricsResults[]): MetricsResults {
const ttfiValues = results.map(r => r.timings.find(timing => timing.id === metrics.ids.TTFI).timing);
const ttfiValues = results.map(r => r.timings.find(timing => timing.id === METRICS.TTF_CPU_IDLE).timing);
const medianTTFI = this.median(ttfiValues);
// in the case of duplicate runs having the exact same TTFI, we naively pick the first
// @fixme, but any for now...
return results.find((result: any) => result.timings.find((timing:any) =>
timing.id === metrics.ids.TTFI && timing.timing === medianTTFI
return results.find((result: any) => result.timings.find((timing: any) =>
timing.id === METRICS.TTF_CPU_IDLE && timing.timing === medianTTFI
)
);
}
Expand Down
Loading