Skip to content

Commit 9f2988a

Browse files
committed
major rework to simplify + PSI only
1 parent e96b59a commit 9f2988a

33 files changed

+535
-5754
lines changed

.eslintrc.js

-16
This file was deleted.

.gitignore

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
node_modules
2-
coverage
3-
*.trace.json
4-
*.devtoolslog.json
5-
*.result.json
1+
snapshots/*
2+
*.tsv

.npmignore

-8
This file was deleted.

.travis.yml

-4
This file was deleted.

CHANGELOG.md

-21
This file was deleted.

LICENSE

-21
This file was deleted.

README.md

+118-94
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,168 @@
1-
# DEPRECATED - Please use [GoogleChrome/lighthouse-ci](https://github.com/GoogleChrome/lighthouse-ci) instead
1+
# PageSpeed Insights scripts
22

3-
# pagespeed-score
3+
Small scripts and recipes to run PageSpeed Insights via the API, save results, summarise and export to tsv.
44

5-
[![Build Status](https://travis-ci.org/csabapalfi/pagespeed-score.svg?branch=master)](https://travis-ci.org/csabapalfi/pagespeed-score/)
6-
[![Coverage Status](https://coveralls.io/repos/github/csabapalfi/pagespeed-score/badge.svg?2)](https://coveralls.io/github/csabapalfi/pagespeed-score)
5+
- [PageSpeed Insights scripts](#pagespeed-insights-scripts)
6+
- [Setup](#setup)
7+
- [Dependencies](#dependencies)
8+
- [Get an API key and set `PSI_API_KEY`](#get-an-api-key-and-set-psi_api_key)
9+
- [Save your URLs to test in `urls.tsv`](#save-your-urls-to-test-in-urlstsv)
10+
- [Usage](#usage)
11+
- [Capture a snapshot](#capture-a-snapshot)
12+
- [Capture snapshots every 5 mins](#capture-snapshots-every-5-mins)
13+
- [Show lab data report](#show-lab-data-report)
14+
- [Save lab data report as tsv](#save-lab-data-report-as-tsv)
15+
- [Caveats](#caveats)
716

8-
Google PageSpeed score command line toolkit
17+
## Setup
918

10-
Get a score and metrics via the Google PageSpeed Insights API or a local Lighthouse run.
19+
### Dependencies
1120

12-
- [Requirements](#requirements)
13-
- [Usage](#usage)
14-
- [multiple runs](#multiple-runs)
15-
- [mobile or desktop strategy](#mobile-or-desktop-strategy)
16-
- [save result JSON to disk](#save-result-json-to-disk)
17-
- [Local mode](#local-mode)
18-
- [set CPU slowdown multiplier](#set-cpu-slowdown-multiplier)
19-
- [save trace & devtools log to disk](#save-trace--devtools-log-to-disk)
20-
- [save Lantern metrics estimation traces](#save-lantern-metrics-estimation-traces)
21+
- `bash`
22+
- `node>12`
23+
- no node modules or anything else to install
2124

22-
## Requirements
25+
### Get an API key and set `PSI_API_KEY`
2326

24-
* **node.js 10+** - because of [`for-await...of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of)
25-
* (**Google Chrome**) - in case you want to run [local mode](#local-mode)
27+
See https://developers.google.com/speed/docs/insights/v5/get-started
2628

27-
## Usage
29+
`export PSI_API_KEY=<api-key>`
2830

29-
`npx` is the quickest way to try:
31+
### Save your URLs to test in `urls.tsv`
3032

33+
- the expected format is `url\tname`, e.g. `https://web.dev/ homepage`
34+
- name is optional, the default name is the URL path
35+
36+
```bash
37+
cat > urls.tsv <<EOF
38+
https://web.dev/ homepage
39+
https://web.dev/fast/
40+
EOF
3141
```
32-
$ npx pagespeed-score http://example.com/
33-
name score FCP SI LCP TTI TBT CLS
34-
run 1 100 0.7 0.7 0.7 0.7 0.0 0.00
35-
```
3642

37-
FCP, SI, LCP, TTI, TBT and CLS are the values for the 6 metrics that affect the score.
43+
## Usage
44+
45+
### Capture a snapshot
3846

39-
Use `--help` see the list of all options.
47+
`./captute-snapshot [snapshotsDirectory] [urlsTsvFile]`
4048

41-
### multiple runs
49+
Save PageSpeed Insights API results
4250

43-
`--runs <N>` overrides the number of runs (default: 1). For more than 1 runs stats will be calculated.
51+
- in `snapshotsDirectory` (default: `snapshots`)
52+
- for all URLs in `urlsTsvFile` (default: `urls.tsv`)
53+
- then show PageSpeed Insights **lab data** summary for current snapshot
4454

55+
Example:
56+
57+
```bash
58+
./capture-snapshot
4559
```
46-
$ npx pagespeed-score --runs 3 https://www.ft.com/
47-
name score FCP SI LCP TTI TBT CLS
48-
run 1 36 6.3 7.1 7.9 10.8 0.4 0.00
49-
run 2 36 6.3 7.1 8.0 10.8 0.4 0.00
50-
run 3 36 6.3 7.3 7.9 10.8 0.4 0.00
51-
52-
median 36 6.3 7.1 7.9 10.8 0.4 0.00
53-
stddev 0.0 0.0 0.1 0.0 0.0 0.0 0.00
54-
min 36 6.3 7.1 7.9 10.8 0.4 0.00
55-
max 36 6.3 7.3 8.0 10.8 0.4 0.00
60+
61+
Output:
62+
5663
```
64+
created snapshots/2021-03-19_21-34-27/
65+
running pagespeed insights for https://web.dev/...
66+
running pagespeed insights for https://web.dev/fast/...
5767
58-
### mobile or desktop strategy
68+
Score FCP LCP TBT CLS Timestamp URL
69+
95 1.1 s 2.8 s 70 ms 0 2021-03-19T21:34:28.785Z /fast/
70+
99 1.1 s 1.6 s 30 ms 0 2021-03-19T21:34:29.159Z homepage
71+
```
5972

60-
`--strategy <mobile|desktop>` sets the Lighthouse strategy (default: mobile)
73+
Files created:
6174

6275
```
63-
$ npx pagespeed-score --strategy desktop https://www.google.com
64-
name score FCP FMP SI FCI TTI
65-
run 1 100 0.5 0.5 0.5 0.9 0.9
76+
snapshots/2021-03-19_21-34-27/homepage.json
77+
snapshots/2021-03-19_21-34-27/fast.json
6678
```
6779

68-
### save result JSON to disk
80+
### Capture snapshots every 5 mins
6981

70-
`--save-results` will save all Lighthouse results (from the PSI API response or local Lighthouse run) as JSON files.
82+
Example:
7183

72-
```
73-
$ npx pagespeed-score --save-results --runs=2 https://www.google.com
74-
name score FCP FMP SI FCI TTI
75-
run 1 96 0.9 1.0 1.2 3.1 3.9
76-
run 2 96 0.9 1.0 1.0 3.1 3.7
77-
78-
$ ls
79-
1-0.result.json
80-
2-0.result.json
84+
```bash
85+
watch -c -n 300 ./capture-snapshot
8186
```
8287

83-
## Local mode
88+
Output:
8489

85-
Switches to running Lighthouse locally instead of calling the PSI API. This can be useful for non-public URLs (e.g. staging environment on a private network) or debugging. To ensure the local results are close to the PSI API results this module:
90+
```
91+
Every 300.0s: ./capture-snapshot
8692
87-
* uses the same version of LightHouse as PSI (6.0.0 as of 2020-05-30)
88-
* uses the [LightRider mobile config](https://github.com/GoogleChrome/lighthouse/blob/master/lighthouse-core/config/lr-mobile-config.js) like PSI
89-
* allows throttling of CPU to better match PSI infrastructure limits
90-
* you can also use the same Chrome version as PSI (81 as of 2020-05-30) by specifying CHROME_PATH (and ensuring you have the correct version installed)
93+
created snapshots/2021-03-20_00-28-01/
94+
running pagespeed insights for https://web.dev/...
95+
running pagespeed insights for https://web.dev/fast/...
9196
92-
```sh
93-
CHROME_PATH="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" npx pagespeed-score --local "<url>"
97+
Timestamp FCP LCP CLS TBT Score URL
98+
2021-03-20 00:28:02 1.8 s 2.1 s 0.001 30 ms 98 /fast/
99+
2021-03-20 00:28:02 1.8 s 2.6 s 0 40 ms 96 homepage
94100
```
95101

96-
Local results will still differ from the PSI API because of local hardware and network variability.
102+
Files created:
97103

98-
### set CPU slowdown multiplier
104+
- Every 5 minutes a timestamp directory is created with the latest results
99105

100-
`--cpu-slowdown` will allow setting CPU throttling multiplier (default 4x). Only available in [local mode](#local-mode).
106+
### Show lab data report
101107

102-
Please note that PSI infrastructure already runs on a slower CPU (that's like a mobile device) hence the need to slow our machines CPU down for local runs.
108+
`./lab-report [snapshotsDirectory] [filter]`
103109

104-
### save trace & devtools log to disk
110+
Show PageSpeed Insights **lab data** summary:
105111

106-
`--save-assets` will save trace & devtools log to disk. Only available in [local mode](#local-mode).
112+
- for all results in `snapshotsDirectory` recursively (default: current directory)
113+
- if `filter` is set then only show results where URL or path or name matches `filter` (default: show all URLs)
107114

115+
The summary shows:
116+
117+
- `Timestamp`: human readable timestamp
118+
- `FCP`, `LCP`, `CLS` and `TBT` formatted (and colored) lab data values
119+
- `Score` 0-100 colored performance score
120+
- `URL` the requested URL path (or name), or in case of a redirect `<requestPathOrName> -> <finalPath>`
121+
122+
Example:
123+
124+
```bash
125+
./lab-report snapshots/ /fast/
108126
```
109-
$ npx pagespeed-score --save-assets --local https://www.google.com
110-
name score FCP FMP SI FCI TTI
111-
run 1 95 1.4 1.4 1.7 3.6 3.8
112127

113-
$ ls
114-
1-0.devtoolslog.json
115-
1-0.trace.json
128+
Output:
129+
130+
```
131+
Timestamp FCP LCP CLS TBT Score URL
132+
2021-03-19 21:48:49 1.8 s 2.0 s 0 60 ms 98 /fast/
133+
2021-03-19 21:49:00 1.8 s 2.1 s 0 70 ms 98 /fast/
116134
```
117135

118-
### save Lantern metrics estimation traces
136+
### Save lab data report as tsv
137+
138+
`NO_FORMAT=1 NO_COLOR=1 ./summarize-reports [snapshotsDirectory] [filter] > tsvFileName`
119139

120-
Setting the `LANTERN_DEBUG=true` environment variable along with `--save-assets --local` will save traces for how metrics were simulated by Lantern. Only available in [local mode](#local-mode).
140+
Save PageSpeed Insights **lab data** report into `tsvFileName`:
121141

142+
- without colors or formatting
143+
- ready to be imported into e.g. Google Sheets
144+
145+
The tsv rows will have the following fields:
146+
147+
- `Timestamp`: ISO date time value
148+
- `FCP`, `LCP`, `CLS` and `TBT` lab data raw numeric values
149+
- `Score` 0-100 performance score
150+
- `URL` the requested URL path (or name), or in case of a redirect `<requestPathOrName> -> <finalPath>`
151+
152+
Example:
153+
154+
```bash
155+
NO_FORMAT=1 NO_COLOR=1 ./lab-report snapshots/ > data.csv
122156
```
123-
$ LANTERN_DEBUG=true npx pagespeed-score --local --save-assets https://www.google.com
124-
name score FCP FMP SI FCI TTI
125-
run 1 95 1.4 1.4 1.7 3.6 3.8
126-
127-
$ ls
128-
1-0.devtoolslog.json
129-
1-0.trace.json
130-
1-optimisticFirstContentfulPaint.trace.json
131-
1-optimisticFirstMeaningfulPaint.trace.json
132-
1-optimisticFlexFirstContentfulPaint.trace.json
133-
1-optimisticFlexFirstMeaningfulPaint.trace.json
134-
1-optimisticFlexInteractive.trace.json
135-
1-optimisticInteractive.trace.json
136-
1-pessimisticFirstContentfulPaint.trace.json
137-
1-pessimisticFirstMeaningfulPaint.trace.json
138-
1-pessimisticInteractive.trace.json
139157

158+
Files created:
159+
160+
```
161+
data.tsv
140162
```
141163

142-
You can drag and drop these traces on the Chrome Devtools Performance tab.
164+
## Caveats
143165

144-
See also [lighthouse#5844 Better visualization of Lantern simulation](https://github.com/GoogleChrome/lighthouse/issues/5844).
166+
- few nights hack
167+
- no tests
168+
- not originally intended for public consumption

capture-snapshot

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env bash
2+
3+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
4+
set -euo pipefail
5+
IFS=$'\n\t'
6+
7+
SNAPSHOTS_BASE="${1:-snapshots}"
8+
URLS="${2:-urls.tsv}"
9+
10+
11+
snapshotsPath="$SNAPSHOTS_BASE/$(printf '%(%Y-%m-%d_%H-%M-%S)T' -1)"
12+
mkdir -p "$snapshotsPath"
13+
echo "created $snapshotsPath/"
14+
15+
run() {
16+
./run-pagespeed "$url" | ./save-result "$snapshotsPath" "$url" "$name"
17+
}
18+
19+
while IFS=$'\t' read -r url name
20+
do
21+
echo "running pagespeed insights for ${url}..."
22+
run "$url" "$name" &
23+
done < <(grep -v '^ *#' < "$URLS")
24+
25+
wait
26+
echo ""
27+
./lab-report "$snapshotsPath"

0 commit comments

Comments
 (0)