lighthouse

Architecture

Some incomplete notes

Lighthouse Architecture

Components & Terminology

Audit/Report terminology

Protocol

// will NOT work
driver.defaultSession.sendCommand('Security.enable').then(_ => {
  driver.defaultSession.on('Security.securityStateChanged', state => { /* ... */ });
})

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

Understanding a Trace

core/lib/tracehouse/trace-processor.js provides the core transformation of a trace into more meaningful objects. Each raw trace event has a monotonically increasing timestamp in microseconds, a thread ID, a process ID, a duration in microseconds (potentially), and other applicable metadata properties such as the event type, the task name, the frame, etc. Learn more about trace events.

Example Trace Event

{
  'pid': 41904, // process ID
  'tid': 1295, // thread ID
  'ts': 1676836141, // timestamp in microseconds
  'ph': 'X', // trace event type
  'cat': 'toplevel', // trace category from which this event came
  'name': 'MessageLoop::RunTask', // relatively human-readable description of the trace event
  'dur': 64, // duration of the task in microseconds
  'args': {}, // contains additional data such as frame when applicable
}

Processed trace

The processed trace identifies trace events for key moments (navigation start, FCP, LCP, DOM content loaded, trace end, etc) and provides filtered views of just the main process and the main thread events. Because the timestamps are not necessarily interesting in isolation, the processed trace also calculates the times in milliseconds of key moments relative to navigation start, thus providing the typical interpretation of metrics in ms.

{
  processEvents: [/* all trace events in the main process */],
  mainThreadEvents: [/* all trace events on the main thread */],
  timings: {
    timeOrigin: 0, // timeOrigin is always 0 ms
    firstContentfulPaint: 150, // firstContentfulPaint time in ms after time origin
    /* other key moments */
    traceEnd: 16420, // traceEnd time in ms after time origin
  },
  timestamps: {
    timeOrigin: 623000000, // timeOrigin timestamp in microseconds, marks the start of the navigation of interest
    firstContentfulPaint: 623150000, // firstContentfulPaint timestamp in microseconds
    /* other key moments */
    traceEnd: 639420000, // traceEnd timestamp in microseconds
  },
}

Audits

The return value of each audit takes this shape.

The details object is parsed in report-renderer.js. View other audits for guidance on how to structure details.

Core internal module dependencies

image

(Generated May 17, 2022 via madge core/index.js --image arch.png --layout dot --exclude="(locales\/)|(stack-packs\/packs)")

Lantern

Lantern is how Lighthouse simulates network and cpu throttling.