lighthouse/docs
Paul Irish f19ffd181d docs: fox mobile device testing example (#3887) 2017-11-22 11:47:15 -08:00
..
recipes misc(audits): removed unused audit meta.category (#3554) 2017-10-18 13:56:32 -07:00
architecture.md docs: import audit glossary from SEO doc (#3207) 2017-09-05 12:59:19 -07:00
bug-labels.md docs(bug-labels): Updating "good first bug" (#3535) 2017-10-13 09:57:24 -07:00
configuration.md fix: update plots-config to use newer quiet parameters (#3464) 2017-10-02 20:02:25 -07:00
error-reporting.md docs(error-reporting): improve clarity for opt-out folks (#3876) 2017-11-21 13:16:24 -08:00
hacking-tips.md
headless-chrome.md Switch to using standalone chrome-launcher package (#3410) 2017-09-26 07:14:08 +09:00
new-audits.md docs(new-audits.md): Principles and guidance for new audits (#3617) 2017-11-03 17:05:03 -07:00
readme.md docs: fox mobile device testing example (#3887) 2017-11-22 11:47:15 -08:00
scoring.md Update scoring.md 2017-11-03 15:16:04 -07:00
throttling.md docs(throttling): because comcast throttles the websocket 2017-10-06 13:13:40 -07:00
understanding-results.md core: remove optimalValue (#3774) 2017-11-10 13:44:41 -08:00

readme.md

This directory contains useful documentation, examples (keep reading), and recipes to get you started. For an overview of Lighthouse's internals, see Lighthouse Architecture.

Using programmatically

The example below shows how to run Lighthouse programmatically as a Node module. It assumes you've installed Lighthouse as a dependency (yarn add --dev lighthouse).

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');

function launchChromeAndRunLighthouse(url, flags = {}, config = null) {
  return chromeLauncher.launch().then(chrome => {
    flags.port = chrome.port;
    return lighthouse(url, flags, config).then(results =>
      chrome.kill().then(() => results));
  });
}

const flags = {};

// Usage:
launchChromeAndRunLighthouse('https://example.com', flags).then(results => {
  // Use results!
});

Performance-only Lighthouse run

Many modules consuming Lighthouse are only interested in the performance numbers. Lighthouse ships with a performance-only config that you can use:

const perfConfig: any = require('lighthouse/lighthouse-core/config/perf.json');
// ...
launchChromeAndRunLighthouse(url, flags, perfConfig).then( // ...

You can also craft your own config (e.g. plots-config.js) for completely custom runs. Also see the basic custom audit recipe.

Turn on logging

If you want to see log output as Lighthouse runs, include the lighthouse-logger module and set an appropriate logging level in your code. You'll also need to pass the logLevel flag when calling lighthouse.

const log = require('lighthouse-logger');

const flags = {logLevel: 'info', output: 'json'};
log.setLevel(flags.logLevel);

launchChromeAndRunLighthouse('https://example.com', flags).then(...);

Testing on a site with authentication

When installed globally via npm i -g lighthouse or yarn global add lighthouse, chrome-debug is added to your PATH. This binary launches a standalone Chrome instance with an open debugging port.

  1. Run chrome-debug. This will log the debugging port of your Chrome instance
  2. Navigate to your site and log in.
  3. In a separate terminal tab, run lighthouse http://mysite.com --port port-number using the port number from chrome-debug.

Testing on a mobile device

Lighthouse can run against a real mobile device. You can follow the Remote Debugging on Android (Legacy Workflow) up through step 3.3, but the TL;DR is install & run adb, enable USB debugging, then port forward 9222 from the device to the machine with Lighthouse.

You'll likely want to use the CLI flags --disable-device-emulation --disable-cpu-throttling and potentially --disable-network-throttling.

$ adb kill-server

$ adb devices -l
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
00a2fd8b1e631fcb       device usb:335682009X product:bullhead model:Nexus_5X device:bullhead

$ adb forward tcp:9222 localabstract:chrome_devtools_remote

$ lighthouse --port=9222 --disable-device-emulation --disable-cpu-throttling https://example.com

Lighthouse as trace processor

Lighthouse can be used to analyze trace and performance data collected from other tools (like WebPageTest and ChromeDriver). The traces and devtoolsLogs artifact items can be provided using a string for the absolute path on disk. The devtoolsLogs array is captured from the Network and Page domains (a la ChromeDriver's enableNetwork and enablePage options.

As an example, here's a trace-only run that's reporting on user timings and critical request chains:

config.json

{
  "audits": [
    "user-timings",
    "critical-request-chains"
  ],

  "artifacts": {
    "traces": {
      "defaultPass": "/User/me/lighthouse/lighthouse-core/test/fixtures/traces/trace-user-timings.json"
    },
    "devtoolsLogs": {
      "defaultPass": "/User/me/lighthouse/lighthouse-core/test/fixtures/traces/perflog.json"
    }
  },

  "categories": {
    "performance": {
      "name": "Performance Metrics",
      "description": "These encapsulate your app's performance.",
      "audits": [
        {"id": "user-timings", "weight": 1},
        {"id": "critical-request-chains", "weight": 1}
      ]
    }
  }
}

Then, run with: lighthouse --config-path=config.json http://www.random.url