Giter Site home page Giter Site logo

runt18 / lighthouse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechrome/lighthouse

0.0 2.0 0.0 11.79 MB

Auditing, performance metrics, and best practices for Progressive Web Apps

Home Page: https://developers.google.com/web/tools/lighthouse/

License: Apache License 2.0

JavaScript 87.16% TypeScript 3.35% HTML 4.52% CSS 4.45% Shell 0.21% Ruby 0.30%

lighthouse's Introduction

Lighthouse Build Status Build Status Windows Coverage Status

Lighthouse analyzes web apps and web pages, collecting modern performance metrics and insights on developer best practices.

HTML report:

image

Default CLI output:

image

Lighthouse requires Chrome 56 or later.

Install Chrome extension

Install from the Chrome Web Store: chrome.google.com/webstore/detail/lighthouse/…

Quick-start guide on using the Lighthouse extension: http://bit.ly/lighthouse-quickstart

Install CLI NPM lighthouse package

Requires Node v6+

npm install -g lighthouse
# or if you use yarn:
# yarn global add lighthouse

Run

# Kick off a lighthouse run
lighthouse https://airhorner.com/

# see flags and options
lighthouse --help

Lighthouse Viewer

If you run Lighthouse with the --output=json flag, it will generate a json dump of the run. You can view this report online by visiting https://googlechrome.github.io/lighthouse/viewer/ and dragging the file onto the app. Reports can also be shared by clicking the share icon in the top right corner and signing in to GitHub.

Note: shared reports are stashed as a secret Gist in GitHub, under your account.

Develop

Setup

git clone https://github.com/GoogleChrome/lighthouse

cd lighthouse
npm install
npm run install-all

# The CLI is authored in TypeScript and requires compilation.
# If you need to make changes to the CLI, run the TS compiler in watch mode:
# cd lighthouse-cli && npm run dev

Run

node lighthouse-cli http://example.com

Getting started tip: node --inspect --debug-brk lighthouse-cli http://example.com to open up Chrome DevTools and step through the entire app. See Debugging Node.js with Chrome DevTools for more info.

Custom run configuration

You can supply your own run configuration to customize what audits you want details on. Copy the default.json and start customizing. Then provide to the CLI with lighthouse --config-path=myconfig.json <url>

Custom audits and gatherers

The audits and gatherers checked into the lighthouse repo are available to any configuration. If you're interested in writing your own audits or gatherers, you can use them with Lighthouse without necessarily contributing upstream.

Better docs coming soon, but in the meantime look at PR #593, and the tests valid-custom-audit.js and valid-custom-gatherer.js. If you have questions, please file an issue and we'll help out!

Do Better Web

Do Better Web is an initiative within Lighthouse to help web developers modernize their existing web applications. By running a set of tests, developers can discover new web platform APIs, become aware of performance pitfalls, and learn (newer) best practices. In other words, do better on the web!

DBW is implemented as a set of standalone gatherers and audits that are run alongside the core Lighthouse tests.

To run DBW, just run lighthouse against a URL. The tests show up under "Best Practices" in the report.

If you'd like to contribute, check the list of issues or propose a new audit by filing an issue.

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 performanceLog artifact items can be provided using a string for the absolute path on disk. The performance log is captured from the Network domain (a la ChromeDriver's enableNetwork option) and reformatted slightly. 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"
    },
    "performanceLog": "/User/me/lighthouse/lighthouse-core/test/fixtures/traces/perflog.json"
  },

  "aggregations": [{
    "name": "Performance Metrics",
    "description": "These encapsulate your app's performance.",
    "scored": false,
    "categorizable": false,
    "items": [{
      "audits": {
        "user-timings": { "expectedValue": 0, "weight": 1 },
        "critical-request-chains": { "expectedValue": 0, "weight": 1}
      }
    }]
  }]
}

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

Lighthouse CLI options

$ lighthouse --help

lighthouse <url>

Logging:
  --verbose  Displays verbose logging                                                      [boolean]
  --quiet    Displays no progress or debug logs                                            [boolean]

Configuration:
  --disable-device-emulation    Disable device emulation                                   [boolean]
  --disable-cpu-throttling      Disable cpu throttling                                     [boolean]
  --disable-network-throttling  Disable network throttling                                 [boolean]
  --save-assets                 Save the trace contents & screenshots to disk              [boolean]
  --save-artifacts              Save all gathered artifacts to disk                        [boolean]
  --list-all-audits             Prints a list of all available audits and exits            [boolean]
  --list-trace-categories       Prints a list of all required trace categories and exits   [boolean]
  --config-path                 The path to the config JSON.
  --perf                        Use a performance-test-only configuration                  [boolean]

Output:
  --output       Reporter for the results
                         [choices: "pretty", "json", "html"]                     [default: "pretty"]
  --output-path  The file path to output the results
                 Example: --output-path=./lighthouse-results.html                [default: "stdout"]

Options:
  --help             Show help                                                             [boolean]
  --version          Show version number                                                   [boolean]
  --skip-autolaunch  Skip autolaunch of Chrome when accessing port 9222 fails              [boolean]
  --select-chrome    Interactively choose version of Chrome to use when multiple
                     installations are found                                               [boolean]

Output Path Examples

--output-path=~/mydir/foo.out --save-assets generates

  • ~/mydir/foo.out
  • ~/mydir/foo.report.html
  • ~/mydir/foo-0.trace.json
  • ~/mydir/foo-0.screenshots.html

--output-path=./report.json --output json --save-artifacts generates

  • ./report.json
  • ./report.artifacts.log

--save-artifacts prints a pretty report to stdout and generates

  • ./<HOST>_<DATE>.report.html
  • ./<HOST>_<DATE>.artifacts.log

Lighthouse w/ mobile devices

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 --disable-device-emulation --disable-cpu-throttling https://mysite.com

Tests

Some basic unit tests forked are in /test and run via mocha. ESLint also checks for style violations.

# lint and test all files
npm test

# watch for file changes and run tests
npm run watch

## run linting and unit tests separately
npm run lint
npm run unit

Chrome Extension

The same audits are run against from a Chrome extension. See ./extension.

Architecture

Some incomplete notes

Components

  • Driver - Interfaces with Chrome Debugging Protocol (API viewer)
  • Gathers - Requesting data from the browser (and maybe post-processing)
  • Artifacts - The output of gatherers
  • Audits - Non-performance evaluations of capabilities and issues. Includes a raw value and score of that value.
  • Metrics - Performance metrics summarizing the UX
  • Diagnoses - The perf problems that affect those metrics
  • Aggregators - Pulling audit results, grouping into user-facing components (eg. install_to_homescreen) and applying weighting and overall scoring.

Internal module graph

graph of lighthouse-core module dependencies

npm install -g js-vd; vd --exclude "node_modules|third_party|fs|path|url|log" lighthouse-core/ > graph.html

Protocol

  • Interacting with Chrome: The Chrome protocol connection maintained via WebSocket for the CLI chrome.debuggger API when in the Chrome extension.
  • Event binding & domains: Some domains must be enable()d so they issue events. Once enabled, they flush any events that represent state. As such, network events will only issue after the domain is enabled. All the protocol agents resolve their Domain.enable() callback after they have flushed any pending events. See example:
// will NOT work
driver.sendCommand('Security.enable').then(_ => {
  driver.on('Security.securityStateChanged', state => { /* ... */ });
})

// WILL work! happy happy. :)
driver.on('Security.securityStateChanged', state => { /* ... */ }); // event binding is synchronous
driver.sendCommand('Security.enable');

Gatherers

  • Reading the DOM: We prefer reading the DOM right from the browser (See #77). The driver exposes a querySelector method that can be used along with a getAttribute method to read values.

Audits

The return value of each audit takes this shape:

Promise.resolve({
  name: 'audit-name',
  tags: ['what have you'],
  description: 'whatnot',
  // value: The score. Typically a Boolean, but can be number 0-100
  value: 0,
  // rawValue: Could be anything, as long as it can easily be stringified and displayed,
  //   e.g. 'your score is bad because you wrote ${rawValue}'
  rawValue: {},
  // debugString: Some *specific* error string for helping the user figure out why they failed here.
  //   The reporter can handle *general* feedback on how to fix, e.g. links to the docs
  debugString: 'Your manifest 404ed',
  // fault:  Optional argument when the audit doesn't cover whatever it is you're doing,
  //   e.g. we can't parse your particular corner case out of a trace yet.
  //   Whatever is in `rawValue` and `score` would be N/A in these cases
  fault: 'some reason the audit has failed you, Anakin'
});

Code Style

The .eslintrc defines all.

We're using JSDoc along with closure annotations. Annotations encouraged for all contributions.

const > let > var. Use const wherever possible. Save var for emergencies only.

Trace processing

The traceviewer-based trace processor from node-big-rig was forked into Lighthouse. Additionally, the DevTools' Timeline Model is available as well. There may be advantages for using one model over another.

To update traceviewer source:

cd lighthouse-core
# if not already there, clone catapult and copy license over
git clone --depth=1 https://github.com/catapult-project/catapult.git third_party/src/catapult
cp third_party/src/catapult/LICENSE third_party/traceviewer-js/
# pull for latest
git -C "./third_party/src/catapult/" pull
# run our conversion script
node scripts/build-traceviewer-module.js

Lighthouse logo
Lighthouse, ˈlītˌhous (n): a tower or other structure tool containing a beacon light to warn or guide ships developers at "sea".

lighthouse's People

Contributors

addyosmani avatar beaufortfrancois avatar boopathi avatar brendankenny avatar deepanjanroy avatar dracos avatar ebidel avatar hsingh23 avatar janpot avatar jeffposnick avatar kaycebasques avatar maciej-ka avatar matthiaswinkelmann avatar mixed avatar natduca avatar olingern avatar patrickhulce avatar paulirish avatar paullewis avatar pavelfeldman avatar robdodson avatar samccone avatar samthor avatar shakyshane avatar sindresorhus avatar surma avatar umaar avatar wardpeet avatar weiwei-lin avatar xhmikosr avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.