Skip to content

Instantly share code, notes, and snippets.

@zigapk
Created August 2, 2024 14:52
Show Gist options
  • Save zigapk/4c627c25d9de61d701e674c7bc89ad57 to your computer and use it in GitHub Desktop.
Save zigapk/4c627c25d9de61d701e674c7bc89ad57 to your computer and use it in GitHub Desktop.
A bash script for running lighthouse multiple times using CPU slowdown and printing the average metrices.

Usage instructions

Save the file below as run_lighthouse.sh, then make it executable with

chmod +x run_lighthouse.sh

Use like this:

./run_lighthouse.sh YOUR_WEBSITE_URL YOUR_CPU_SLOWDOWN N

The script presumes that you already have Lighthouse CLI and jq installed.

More information available in a blog post here.

#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "Usage: $0 YOUR_WEBSITE_URL YOUR_CPU_SLOWDOWN N"
echo "$#"
exit 1
fi
YOUR_WEBSITE_URL=$1
YOUR_CPU_SLOWDOWN=$2
N=$3
declare -a LCPs
declare -a LCPScores
declare -a FCPs
declare -a FCPScores
declare -a SIs
declare -a SIScores
declare -a TBTs
declare -a TBTScores
declare -a CLSs
declare -a CLSScores
declare -a TOTALs
for (( i=0; i<$N; i++ ))
do
echo "Run #$((i+1)):"
RESULT=$(lighthouse $YOUR_WEBSITE_URL --throttling.cpuSlowdownMultiplier $YOUR_CPU_SLOWDOWN --output=json --chrome-flags="--headless" --form-factor=mobile 2> /dev/null | jq -r '{LCP: .audits["largest-contentful-paint"].numericValue, "LCP Score": .audits["largest-contentful-paint"].score, FCP: .audits["first-contentful-paint"].numericValue, "FCP Score": .audits["first-contentful-paint"].score, SI: .audits["speed-index"].numericValue, "SI Score": .audits["speed-index"].score, TBT: .audits["total-blocking-time"].numericValue, "TBT Score": .audits["total-blocking-time"].score, CLS: .audits["cumulative-layout-shift"].numericValue, "CLS Score": .audits["cumulative-layout-shift"].score, "TOTAL": .categories.performance.score} | to_entries[] | "\(.key): \(.value)"')
echo "$RESULT"
echo
LCPs+=($(echo "$RESULT" | grep 'LCP:' | awk '{print $2}'))
LCPScores+=($(echo "$RESULT" | grep 'LCP Score:' | awk '{print $3}'))
FCPs+=($(echo "$RESULT" | grep 'FCP:' | awk '{print $2}'))
FCPScores+=($(echo "$RESULT" | grep 'FCP Score:' | awk '{print $3}'))
SIs+=($(echo "$RESULT" | grep 'SI:' | awk '{print $2}'))
SIScores+=($(echo "$RESULT" | grep 'SI Score:' | awk '{print $3}'))
TBTs+=($(echo "$RESULT" | grep 'TBT:' | awk '{print $2}'))
TBTScores+=($(echo "$RESULT" | grep 'TBT Score:' | awk '{print $3}'))
CLSs+=($(echo "$RESULT" | grep 'CLS:' | awk '{print $2}'))
CLSScores+=($(echo "$RESULT" | grep 'CLS Score:' | awk '{print $3}'))
TOTALs+=($(echo "$RESULT" | grep 'TOTAL:' | awk '{print $2}'))
done
calculate_average() {
local arr=("$@")
local sum=0
local count=${#arr[@]}
for i in "${arr[@]}"; do
sum=$(echo "$sum + $i" | bc)
done
echo "scale=2; $sum / $count" | bc
}
echo "Averages:"
echo "LCP: $(calculate_average "${LCPs[@]}")"
echo "LCP Score: $(calculate_average "${LCPScores[@]}")"
echo "FCP: $(calculate_average "${FCPs[@]}")"
echo "FCP Score: $(calculate_average "${FCPScores[@]}")"
echo "SI: $(calculate_average "${SIs[@]}")"
echo "SI Score: $(calculate_average "${SIScores[@]}")"
echo "TBT: $(calculate_average "${TBTs[@]}")"
echo "TBT Score: $(calculate_average "${TBTScores[@]}")"
echo "CLS: $(calculate_average "${CLSs[@]}")"
echo "CLS Score: $(calculate_average "${CLSScores[@]}")"
echo "TOTAL: $(calculate_average "${TOTALs[@]}")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment