|
1 |
| -# DEPRECATED - Please use [GoogleChrome/lighthouse-ci](https://github.com/GoogleChrome/lighthouse-ci) instead |
| 1 | +# PageSpeed Insights scripts |
2 | 2 |
|
3 |
| -# pagespeed-score |
| 3 | +Small scripts and recipes to run PageSpeed Insights via the API, save results, summarise and export to tsv. |
4 | 4 |
|
5 |
| -[](https://travis-ci.org/csabapalfi/pagespeed-score/) |
6 |
| -[](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) |
7 | 16 |
|
8 |
| -Google PageSpeed score command line toolkit |
| 17 | +## Setup |
9 | 18 |
|
10 |
| -Get a score and metrics via the Google PageSpeed Insights API or a local Lighthouse run. |
| 19 | +### Dependencies |
11 | 20 |
|
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 |
21 | 24 |
|
22 |
| -## Requirements |
| 25 | +### Get an API key and set `PSI_API_KEY` |
23 | 26 |
|
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 |
26 | 28 |
|
27 |
| -## Usage |
| 29 | +`export PSI_API_KEY=<api-key>` |
28 | 30 |
|
29 |
| -`npx` is the quickest way to try: |
| 31 | +### Save your URLs to test in `urls.tsv` |
30 | 32 |
|
| 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 |
31 | 41 | ```
|
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 |
| -``` |
36 | 42 |
|
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 |
38 | 46 |
|
39 |
| -Use `--help` see the list of all options. |
| 47 | +`./captute-snapshot [snapshotsDirectory] [urlsTsvFile]` |
40 | 48 |
|
41 |
| -### multiple runs |
| 49 | +Save PageSpeed Insights API results |
42 | 50 |
|
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 |
44 | 54 |
|
| 55 | +Example: |
| 56 | + |
| 57 | +```bash |
| 58 | +./capture-snapshot |
45 | 59 | ```
|
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 | + |
56 | 63 | ```
|
| 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/... |
57 | 67 |
|
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 | +``` |
59 | 72 |
|
60 |
| -`--strategy <mobile|desktop>` sets the Lighthouse strategy (default: mobile) |
| 73 | +Files created: |
61 | 74 |
|
62 | 75 | ```
|
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 |
66 | 78 | ```
|
67 | 79 |
|
68 |
| -### save result JSON to disk |
| 80 | +### Capture snapshots every 5 mins |
69 | 81 |
|
70 |
| -`--save-results` will save all Lighthouse results (from the PSI API response or local Lighthouse run) as JSON files. |
| 82 | +Example: |
71 | 83 |
|
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 |
81 | 86 | ```
|
82 | 87 |
|
83 |
| -## Local mode |
| 88 | +Output: |
84 | 89 |
|
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 |
86 | 92 |
|
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/... |
91 | 96 |
|
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 |
94 | 100 | ```
|
95 | 101 |
|
96 |
| -Local results will still differ from the PSI API because of local hardware and network variability. |
| 102 | +Files created: |
97 | 103 |
|
98 |
| -### set CPU slowdown multiplier |
| 104 | +- Every 5 minutes a timestamp directory is created with the latest results |
99 | 105 |
|
100 |
| -`--cpu-slowdown` will allow setting CPU throttling multiplier (default 4x). Only available in [local mode](#local-mode). |
| 106 | +### Show lab data report |
101 | 107 |
|
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]` |
103 | 109 |
|
104 |
| -### save trace & devtools log to disk |
| 110 | +Show PageSpeed Insights **lab data** summary: |
105 | 111 |
|
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) |
107 | 114 |
|
| 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/ |
108 | 126 | ```
|
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 |
112 | 127 |
|
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/ |
116 | 134 | ```
|
117 | 135 |
|
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` |
119 | 139 |
|
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`: |
121 | 141 |
|
| 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 |
122 | 156 | ```
|
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 |
139 | 157 |
|
| 158 | +Files created: |
| 159 | + |
| 160 | +``` |
| 161 | +data.tsv |
140 | 162 | ```
|
141 | 163 |
|
142 |
| -You can drag and drop these traces on the Chrome Devtools Performance tab. |
| 164 | +## Caveats |
143 | 165 |
|
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 |
0 commit comments