Skip to content

Commit

Permalink
Add a DEFAULT_TRACE
Browse files Browse the repository at this point in the history
  • Loading branch information
surma committed Jul 27, 2016
1 parent 18bebe0 commit 187a709
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 42 deletions.
9 changes: 9 additions & 0 deletions lighthouse-core/audits/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
*/
'use strict';

const DEFAULT_TRACE = 'defaultPass';

class Audit {
/**
* @return {!String}
*/
static get DEFAULT_TRACE() {
return DEFAULT_TRACE;
}

/**
* @return {!AuditMeta}
*/
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/audits/estimated-input-latency.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const Formatter = require('../formatters/formatter');
// https://www.desmos.com/calculator/srv0hqhf7d
const SCORING_POINT_OF_DIMINISHING_RETURNS = 50;
const SCORING_MEDIAN = 100;
const TRACE_NAME = 'firstPass';

class EstimatedInputLatency extends Audit {
/**
Expand All @@ -51,7 +50,8 @@ class EstimatedInputLatency extends Audit {
// Use speedline's first paint as start of range for input latency check.
const startTime = artifacts.Speedline.first;

const trace = artifacts.traces[TRACE_NAME] && artifacts.traces[TRACE_NAME].traceContents;
const trace = artifacts.traces[this.DEFAULT_TRACE] &&
artifacts.traces[this.DEFAULT_TRACE].traceContents;
const tracingProcessor = new TracingProcessor();
const model = tracingProcessor.init(trace);
const latencyPercentiles = TracingProcessor.getRiskToResponsiveness(model, trace, startTime);
Expand Down
4 changes: 1 addition & 3 deletions lighthouse-core/audits/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ const SCORING_MEDIAN = 4000;

const BLOCK_FIRST_MEANINGFUL_PAINT_IF_BLANK_CHARACTERS_MORE_THAN = 200;

const TRACE_NAME = 'firstPass';

class FirstMeaningfulPaint extends Audit {
/**
* @return {!AuditMeta}
Expand All @@ -55,7 +53,7 @@ class FirstMeaningfulPaint extends Audit {
*/
static audit(artifacts) {
return new Promise((resolve, reject) => {
const traceContents = artifacts.traces[TRACE_NAME].traceContents;
const traceContents = artifacts.traces[this.DEFAULT_TRACE].traceContents;
if (!traceContents || !Array.isArray(traceContents)) {
throw new Error(FAILURE_MESSAGE);
}
Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/audits/time-to-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class TTIMetric extends Audit {

// Process the trace
const tracingProcessor = new TracingProcessor();
const model = tracingProcessor.init(artifacts.traceContents);
const traceContents = artifacts.traces[Audit.DEFAULT_TRACE].traceContents;
const model = tracingProcessor.init(traceContents);
const endOfTraceTime = model.bounds.max;

// TODO: Wait for DOMContentLoadedEndEvent
Expand Down Expand Up @@ -109,7 +110,7 @@ class TTIMetric extends Audit {
}
// Get our expected latency for the time window
const latencies = TracingProcessor.getRiskToResponsiveness(
model, artifacts.traceContents, startTime, endTime, percentiles);
model, traceContents, startTime, endTime, percentiles);
const estLatency = latencies[0].time.toFixed(2);
foundLatencies.push({
estLatency: estLatency,
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/audits/user-timings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const Formatter = require('../formatters/formatter');
const TimelineModel = require('../lib/traces/devtools-timeline-model');

const FAILURE_MESSAGE = 'Trace data not found.';
const TRACE_NAME = 'firstPass';

/**
* @param {!Array<!Object>} traceData
Expand Down Expand Up @@ -130,7 +129,8 @@ class UserTimings extends Audit {
static audit(artifacts) {
return new Promise((resolve, reject) => {
const traceContents =
artifacts.traces[TRACE_NAME] && artifacts.traces[TRACE_NAME].traceContents;
artifacts.traces[this.DEFAULT_TRACE] &&
artifacts.traces[this.DEFAULT_TRACE].traceContents;
if (!traceContents || !Array.isArray(traceContents)) {
throw new Error(FAILURE_MESSAGE);
}
Expand Down
1 change: 0 additions & 1 deletion lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"passes": [{
"network": true,
"trace": true,
"traceName": "firstPass",
"loadPage": true,
"gatherers": [
"url",
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/driver/gatherers/screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'use strict';

const Gather = require('./gather');
const Audit = require('../../audits/audit');
const DevtoolsTimelineModel = require('../../lib/traces/devtools-timeline-model');

class ScreenshotFilmstrip extends Gather {
Expand Down Expand Up @@ -46,7 +47,7 @@ class ScreenshotFilmstrip extends Gather {
}

afterPass(options, tracingData) {
return this.getScreenshots(tracingData.traceContents)
return this.getScreenshots(tracingData.traces[Audit.DEFAULT_TRACE].traceContents)
.then(screenshots => {
this.artifact = screenshots;
});
Expand Down
17 changes: 9 additions & 8 deletions lighthouse-core/driver/gatherers/speedline.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
*/
'use strict';

const Audit = require('../../audits/audit');
const Gather = require('./gather');
const speedline = require('speedline');

class Speedline extends Gather {

afterPass(options, tracingData) {
return speedline(tracingData.traceContents).then(results => {
this.artifact = results;
}).catch(err => {
this.artifact = {
debugString: err.message
};
});
return speedline(tracingData.traces[Audit.DEFAULT_TRACE].traceContents)
.then(results => {
this.artifact = results;
}).catch(err => {
this.artifact = {
debugString: err.message
};
});
}
}

Expand Down
19 changes: 10 additions & 9 deletions lighthouse-core/driver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
'use strict';

const log = require('../lib/log.js');
const Audit = require('../audits/audit');

class Driver {

static loadPage(driver, options) {
// Since a Page.reload command does not let a service worker take over, we
// navigate away and then come back to reload. We do not `waitForLoad` on
Expand Down Expand Up @@ -107,15 +107,19 @@ class Driver {
const driver = options.driver;
const config = options.config;
const gatherers = config.gatherers;
const loadData = {};
const loadData = {traces: {}};
let pass = Promise.resolve();

if (config.trace) {
pass = pass.then(_ => {
log.log('status', 'Gathering: trace');
let traceName = Audit.DEFAULT_TRACE;
if (config.traceName) {
traceName = config.traceName;
}
log.log('status', `Gathering: trace "${traceName}"`);
driver.endTrace().then(traceContents => {
loadData.traceContents = traceContents;
log.log('statusEnd', 'Gathering: trace');
loadData.traces[traceName] = {traceContents};
log.log('statusEnd', `Gathering: trace "${traceName}"`);
});
});
}
Expand Down Expand Up @@ -191,10 +195,7 @@ class Driver {
.then(_ => this.afterPass(runOptions))
.then(loadData => {
Object.assign(tracingData, loadData);

if (config.traceName) {
tracingData.traces[config.traceName] = loadData;
}
tracingData.traces[config.traceName || Audit.DEFAULT_TRACE] = loadData;
})
.then(_ => this.tearDown(runOptions));
}, Promise.resolve());
Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/test/audits/estimated-input-latency.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const traceContents = require('../fixtures/traces/progressive-app.json');
describe('Performance: estimated-input-latency audit', () => {
it('scores a -1 with invalid trace data', () => {
const output = Audit.audit({
traces: {firstPass: {traceContents: '[{"pid": 15256,"tid": 1295,"t'}},
traces: {[Audit.DEFAULT_TRACE]: {traceContents: '[{"pid": 15256,"tid": 1295,"t'}},
Speedline: {
first: 500
}
Expand All @@ -35,7 +35,7 @@ describe('Performance: estimated-input-latency audit', () => {

it('evaluates valid input correctly', () => {
const output = Audit.audit({
traces: {firstPass: {traceContents}},
traces: {[Audit.DEFAULT_TRACE]: {traceContents}},
Speedline: {
first: 500
}
Expand Down
9 changes: 5 additions & 4 deletions lighthouse-core/test/audits/first-meaningful-paint.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ describe('Performance: first-meaningful-paint audit', () => {
it('processes a valid trace file', done => {
const traceData = require('../fixtures/traces/progressive-app.json');
assert.doesNotThrow(_ => {
Audit.audit({traces: {firstPass: {traceContents: traceData}}}).then(response => {
fmpResult = response;
done();
});
Audit.audit({traces: {[Audit.DEFAULT_TRACE]: {traceContents: traceData}}})
.then(response => {
fmpResult = response;
done();
});
});
});

Expand Down
12 changes: 10 additions & 2 deletions lighthouse-core/test/audits/time-to-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const speedlineGather = new SpeedlineGather();
describe('Performance: time-to-interactive audit', () => {
it('scores a -1 with invalid trace data', () => {
return Audit.audit({
traceContents: '[{"pid": 15256,"tid": 1295,"t',
traces: {
[Audit.DEFAULT_TRACE]: {
traceContents: '[{"pid": 15256,"tid": 1295,"t'
}
},
Speedline: {
first: 500
}
Expand All @@ -38,7 +42,11 @@ describe('Performance: time-to-interactive audit', () => {

it('evaluates valid input correctly', () => {
let artifacts = {
traceContents: traceContents
traces: {
[Audit.DEFAULT_TRACE]: {
traceContents: traceContents
}
}
};
return speedlineGather.afterPass({}, artifacts).then(_ => {
artifacts.Speedline = speedlineGather.artifact;
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/user-timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Performance: user-timings audit', () => {
});

it('evaluates valid input correctly', () => {
return Audit.audit({traces: {firstPass: {traceContents}}})
return Audit.audit({traces: {[Audit.DEFAULT_TRACE]: {traceContents}}})
.then(response => {
assert.equal(response.score, 2);
assert.ok(!Number.isNaN(response.extendedInfo.value[0].startTime));
Expand Down
9 changes: 8 additions & 1 deletion lighthouse-core/test/driver/gatherers/screenshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
/* eslint-env mocha */

const ScreenshotsGather = require('../../../driver/gatherers/screenshots');
const Audit = require('../../../audits/audit');
const assert = require('assert');
let screenshotsGather = new ScreenshotsGather();

Expand All @@ -27,7 +28,13 @@ describe('Screenshot gatherer', () => {
// Currently this test must rely on knowing the phase hook for the gatherer.
// A little unfortunate, but we need a "run scheduler with this gatherer, this mocked driver,
// and this trace" test class to do that right
return screenshotsGather.afterPass(undefined, {traceContents: traceData}).then(_ => {
return screenshotsGather.afterPass(undefined, {
traces: {
[Audit.DEFAULT_TRACE]: {
traceContents: traceData
}
}
}).then(_ => {
assert.ok(Array.isArray(screenshotsGather.artifact));
assert.equal(screenshotsGather.artifact.length, 7);

Expand Down
17 changes: 15 additions & 2 deletions lighthouse-core/test/driver/gatherers/speedline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@

/* eslint-env mocha */

const Audit = require('../../../audits/audit.js');
const SpeedlineGather = require('../../../driver/gatherers/speedline.js');
const assert = require('assert');

describe('Speedline gatherer', () => {
it('returns an error debugString on faulty trace data', done => {
const speedlineGather = new SpeedlineGather();

speedlineGather.afterPass({}, {traceContents: {boo: 'ya'}}).then(_ => {
speedlineGather.afterPass({}, {
traces: {
[Audit.DEFAULT_TRACE]: {
traceContents: {boo: 'ya'}
}
}
}).then(_ => {
assert.ok(speedlineGather.artifact.debugString);
assert.ok(speedlineGather.artifact.debugString.length);
done();
Expand All @@ -36,7 +43,13 @@ describe('Speedline gatherer', () => {
const speedlineGather = new SpeedlineGather();
const traceContents = require('../../fixtures/traces/progressive-app.json');

return speedlineGather.afterPass({}, {traceContents}).then(_ => {
return speedlineGather.afterPass({}, {
traces: {
[Audit.DEFAULT_TRACE]: {
traceContents
}
}
}).then(_ => {
const speedline = speedlineGather.artifact;
return assert.equal(Math.round(speedline.speedIndex), 831);
});
Expand Down
23 changes: 22 additions & 1 deletion lighthouse-core/test/driver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

const Gather = require('../../driver/gatherers/gather');
const Driver = require('../../driver');
const Audit = require('../../audits/audit');
const assert = require('assert');

class TestGatherer extends Gather {
Expand Down Expand Up @@ -159,7 +160,27 @@ describe('Driver', function() {

return Driver.afterPass({driver, config}).then(vals => {
assert.equal(calledTrace, true);
assert.deepEqual(vals.traceContents, {x: 1});
assert.deepEqual(vals.traces[Audit.DEFAULT_TRACE].traceContents, {x: 1});
});
});

it('respects trace names', () => {
const driver = {
endTrace() {
return Promise.resolve({x: 1});
}
};

const config = {
trace: true,
traceName: 'notTheDefaultPass',
gatherers: [{
afterPass() {}
}]
};

return Driver.afterPass({driver, config}).then(vals => {
assert.deepEqual(vals.traces.notTheDefaultPass.traceContents, {x: 1});
});
});

Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
const Runner = require('../runner');
const fakeDriver = require('./driver/fake-driver');
const Config = require('../config');
const Audit = require('../audits/audit');
const assert = require('assert');
const path = require('path');

Expand Down Expand Up @@ -87,7 +88,7 @@ describe('Runner', () => {

artifacts: {
traces: {
firstPass: {
[Audit.DEFAULT_TRACE]: {
traceContents: path.join(__dirname,
'/fixtures/traces/trace-user-timings.json')
}
Expand Down

0 comments on commit 187a709

Please sign in to comment.