Working with results | lighthouse-rust | Spatie       

 SPATIE  

lighthouse-rust
===============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Lighthouse-rust](https://spatie.be/docs/lighthouse-rust/v1)  Usage  Working with results

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/lighthouse-rust/v1) 

  Working with results    
- [ Introduction ](https://spatie.be/docs/lighthouse-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/lighthouse-rust/v1/support-us)
- [ Requirements ](https://spatie.be/docs/lighthouse-rust/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/lighthouse-rust/v1/installation-setup)
- [ Testing ](https://spatie.be/docs/lighthouse-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/lighthouse-rust/v1/changelog)

Usage
-----

- [ Generating your first report ](https://spatie.be/docs/lighthouse-rust/v1/usage/generating-your-first-report)
- [ Configuring a run ](https://spatie.be/docs/lighthouse-rust/v1/usage/configuring-a-run)
- [ Working with results ](https://spatie.be/docs/lighthouse-rust/v1/usage/working-with-results)
- [ Saving an HTML report ](https://spatie.be/docs/lighthouse-rust/v1/usage/saving-an-html-report)
- [ Using performance budgets ](https://spatie.be/docs/lighthouse-rust/v1/usage/using-performance-budgets)

 Working with results
====================

###  On this page 

1. [ Getting the category scores ](#content-getting-the-category-scores)
2. [ Getting audit results ](#content-getting-audit-results)
3. [ Performance metrics convenience methods ](#content-performance-metrics-convenience-methods)
4. [ Getting the configuration used to generate the result ](#content-getting-the-configuration-used-to-generate-the-result)
5. [ Getting the raw results ](#content-getting-the-raw-results)
6. [ Getting the benchmark index ](#content-getting-the-benchmark-index)
7. [ Restoring saved reports ](#content-restoring-saved-reports)

Running Lighthouse will return a `LighthouseResult`. On this page, we'll cover the various methods you can call on it.

```
let result = Lighthouse::url("https://example.com")?.run()?;
```

Getting the category scores
-----------------------------------------------------------------------------------------------------------------------------------------

You can use `scores()` to get scores of the four categories Lighthouse runs audits for. Use `score()` for a single category.

```
let scores = result.scores();
let accessibility = result.score(Category::Accessibility);
```

The map uses category identifiers as keys and integer percentages as values. `scores()` retains real zero scores and omits missing/null scores. `score()` returns `Some(0)` for a real zero and `None` for missing/null. Values round to the nearest percentage, matching Lighthouse's displayed scores. Map keys are sorted lexicographically.

Getting audit results
-----------------------------------------------------------------------------------------------------------------------

Lighthouse will run various audits on your website. You can learn more about the meaning of each audit in [the Lighthouse docs](https://developer.chrome.com/docs/lighthouse/overview).

You can get the names of all audits using `audit_names()`. Here's how you can get the results of an audit:

```
let all_audit_names = result.audit_names();
let audit = result.audit("first-contentful-paint")?;
let all_audits = result.audits();
```

`audit()` returns a borrowed `serde_json::Value`, or `Error::AuditDoesNotExist`. `audits()` returns an optional audit map. Both preserve Lighthouse's audit order. The example above uses the original PHP fixture, not a current benchmark.

Performance metrics convenience methods
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

There are a few convenience methods to get the most interesting metrics for the performance category. You can call all these methods on `result`. The ones ending in `_in_ms` return results in milliseconds.

- `first_contentful_paint_in_ms()`
- `largest_contentful_paint_in_ms()`
- `speed_index_in_ms()`
- `total_blocking_time_in_ms()`
- `time_to_interactive_in_ms()`
- `cumulative_layout_shift()`
- `total_page_size_in_bytes()`

Some of these methods have equivalents that return a formatted string such as `1.3 s`.

- `formatted_first_contentful_paint()`
- `formatted_largest_contentful_paint()`
- `formatted_speed_index()`
- `formatted_total_blocking_time()`
- `formatted_time_to_interactive()`
- `formatted_cumulative_layout_shift()`

```
let milliseconds = result.first_contentful_paint_in_ms()?;
let formatted = result.formatted_first_contentful_paint()?;
```

These return `Result<Option<T>, Error>`. A missing audit is an error; an audit with no numeric/display value returns `None`. Availability depends on the Lighthouse version and selected audits. Time to interactive is no longer a scored performance metric, but Lighthouse 13 still includes its hidden audit. Formatted cumulative layout shift returns the original display string.

Getting the configuration used to generate the result
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The result also contains the settings used to run Lighthouse. Use dot notation to get a specific setting, such as the browser width.

```
let settings = result.config_settings();
let width = result.config_setting("screenEmulation.width");
```

There are also convenience methods for interesting pieces of configuration:

- `form_factor()`: returns `Result<Option<FormFactor>, Error>`.
- `user_agent()`: returns an optional user agent string.
- `headers()`: returns an optional map of extra headers.
- `network_throttling_was_enabled()`: returns a boolean from the recorded flag.
- `cpu_throttling_was_enabled()`: returns a boolean from the recorded flag.

Absent throttling-disable flags count as enabled. These flags reflect recorded configuration, not independently verified browser behavior.

Getting the raw results
-----------------------------------------------------------------------------------------------------------------------------

To get everything returned by the wrapper, use `raw_results()`. It contains `report: [json, html]` and `runtimeError: null` for successful live runs.

```
let raw = result.raw_results();
let version = result.raw_result("report.0.lighthouseVersion");
let score = result.get("categories.accessibility.score");
let json = result.json_report();
```

`get()` reads a JSON report property. `json_report()` borrows the whole JSON report and `to_array()` returns an owned copy. `raw_result()` and `config_setting()` read paths within the raw envelope and recorded configuration. Dot paths support objects and array indexes; exact object keys take precedence. Missing paths return `None`; a present JSON null remains `Some(Value::Null)`.

Use `report.0.lighthouseVersion` or `lighthouse_version()` to read the version. Live runs return a small envelope with `report` and `runtimeError`, not Lighthouse's internal runner object.

Getting the benchmark index
-----------------------------------------------------------------------------------------------------------------------------------------

Lighthouse computes a benchmark index as a rough approximation of the host device's CPU performance. `benchmark_index()` returns an optional floating-point number, preserving fractional values from current Lighthouse versions.

Restoring saved reports
-----------------------------------------------------------------------------------------------------------------------------

Use `from_reports(json, html)` for separately stored reports, or `from_raw_results(raw)` for an envelope. The latter also accepts a JSON string in `report[0]` and normalizes it to an object. Malformed reports return an error.

```
use spatie_lighthouse::LighthouseResult;

let result = LighthouseResult::default()
    .set_json_report(json_report)?
    .set_html_report(html_report);
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
