Skip to content

Commit

Permalink
Add TTI < 10s audit for PWA
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Mar 11, 2017
1 parent 5966b66 commit 8680289
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
62 changes: 62 additions & 0 deletions lighthouse-core/audits/load-fast-enough-for-pwa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @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 Audit = require('./audit');
const TTIMetric = require('./time-to-interactive');
const Formatter = require('../formatters/formatter');

// Maximum TTI to be considered "fast" for PWA baseline checklist
// https://developers.google.com/web/progressive-web-apps/checklist
const MAXIMUM_TTI = 10 * 1000;

class LoadFastEnough4Pwa extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'PWA',
name: 'load-fast-enough-for-pwa',
description: 'Page load is fast enough on 3G',
helpText: 'Satisfied if the _Time To Interactive_ duration is shorter than _10 seconds_, as defined by the [PWA Baseline Checklist](https://developers.google.com/web/progressive-web-apps/checklist).',
requiredArtifacts: ['traces']
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
return TTIMetric.audit(artifacts).then(ttiResult => {
const timeToInteractive = ttiResult.extendedInfo.value.timings.timeToInteractive;
const isFast = timeToInteractive < MAXIMUM_TTI;

const extendedInfo = {
formatter: Formatter.SUPPORTED_FORMATS.NULL,
value: {timeToInteractive}
};

if (!isFast) {
return LoadFastEnough4Pwa.generateAuditResult({
rawValue: false,
debugString: `Under mobile conditions, the TTI was at ${ttiResult.displayValue}. ` +
'More details in "Performance" section.',
extendedInfo
});
}

return LoadFastEnough4Pwa.generateAuditResult({
rawValue: true,
extendedInfo
});
});
}
}

module.exports = LoadFastEnough4Pwa;
5 changes: 5 additions & 0 deletions lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"manifest-display",
"without-javascript",
"first-meaningful-paint",
"load-fast-enough-for-pwa",
"speed-index-metric",
"estimated-input-latency",
"time-to-interactive",
Expand Down Expand Up @@ -134,6 +135,10 @@
"name": "Page load performance is fast",
"description": "Users notice if sites and apps don't perform well. These top-level metrics capture the most important perceived performance concerns.",
"audits": {
"load-fast-enough-for-pwa": {
"expectedValue": true,
"weight": 1
},
"first-meaningful-paint": {
"expectedValue": 100,
"weight": 1
Expand Down
49 changes: 49 additions & 0 deletions lighthouse-core/test/audits/load-fast-enough-for-pwa-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @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 FastPWAAudit = require('../../audits/load-fast-enough-for-pwa');
const TTIAudit = require('../../audits/time-to-interactive');
const Audit = require('../../audits/audit.js');
const assert = require('assert');
const traceEvents = require('../fixtures/traces/progressive-app.json');

const GatherRunner = require('../../gather/gather-runner.js');
const computedArtifacts = GatherRunner.instantiateComputedArtifacts();

function generateArtifactsWithTrace(trace) {
return Object.assign(computedArtifacts, {
traces: {
[Audit.DEFAULT_PASS]: {traceEvents: Array.isArray(trace) ? trace : trace.traceEvents}
}
});
}

/* eslint-env mocha */
describe('PWA: load-fast-enough-for-pwa audit', () => {
it('returns boolean based on TTI value', () => {
return FastPWAAudit.audit(generateArtifactsWithTrace(traceEvents)).then(result => {
assert.equal(result.rawValue, true, 'fixture trace is not passing audit');
}).catch(err => {
assert.ok(false, err);
});
});

it('fails a bad TTI value', () => {
// mock a return for the TTI Audit
const origTTI = TTIAudit.audit;
const mockTTIResult = {extendedInfo: {value: {timings: {timeToInteractive: 15000}}}};
TTIAudit.audit = _ => Promise.resolve(mockTTIResult);

return FastPWAAudit.audit(generateArtifactsWithTrace(traceEvents)).then(result => {
assert.equal(result.rawValue, false, 'not failing a long TTI value');
TTIAudit.audit = origTTI;
}).catch(err => {
assert.ok(false, err);
});
});
});

0 comments on commit 8680289

Please sign in to comment.