Skip to content

Instantly share code, notes, and snippets.

@nikitawootten-nist
Last active February 9, 2023 22:10
Show Gist options
  • Save nikitawootten-nist/76dfdb45ad68f7d49c396780c90dbf63 to your computer and use it in GitHub Desktop.
Save nikitawootten-nist/76dfdb45ad68f7d49c396780c90dbf63 to your computer and use it in GitHub Desktop.
Lighthouse performance test harness
#!/usr/bin/env bash
# OSCAL Performance Test Scipt
#
# The following packages are required for this script to work:
# - jq <https://stedolan.github.io/jq/>
# - Lighthouse <https://www.npmjs.com/package/lighthouse>
#
# NOTE: on MacOS, you may get a dialog box asking if "node can make external web requests"
set -Eeuo pipefail
DEFAULT_TEST_PATHS="/;/reference/1.0.0/complete/json-reference/;/reference/1.0.0/complete/xml-reference/"
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") TARGET_BASEURL OUTPUT_FOLDER
Runs a lighthouse report on the specified TARGET_HOST (e.g. localhost:1313 or https://pages.nist.gov/OSCAL).
The script runs through the list of test paths specified by the ";"-delimited env var \$TEST_PATHS defaulting to:
${DEFAULT_TEST_PATHS}
This script outputs a CSV of PATH->Score values to STDOUT. All logs are outputed to STDERR
EOF
}
# Utility that prints to STDERR
msg() {
echo >&2 -e "${1-}"
}
[[ -z "${1-}" ]] && { echo "Error: TARGET_BASEURL not specified"; usage; exit 1; }
TARGET_BASEURL=$1
[[ -z "${2-}" ]] && { echo "Error: OUTPUT_FOLDER not specified"; usage; exit 1; }
OUTPUT_FOLDER=$2
TEST_PATHS=${TEST_PATHS-$DEFAULT_TEST_PATHS}
# Used in the file names for all outputs
TIMESTAMP=$(date "+%Y.%m.%d-%H.%M.%S")
# Check output folder exists
if [[ ! -d "$OUTPUT_FOLDER" ]]
then
msg "$OUTPUT_FOLDER does not exist, creating..."
mkdir -p $OUTPUT_FOLDER
fi
echo "path,score"
for TARGET_PATH in $(echo $TEST_PATHS | tr ";" "\n")
do
TARGET_URL="${TARGET_BASEURL}${TARGET_PATH}"
msg "Testing: $TARGET_URL"
# Used in output path of run (A bit ugly but it works!)
SANITIZED_PATH_NAME=${TARGET_PATH//\//%2F}
OUTPUT_PATH="${OUTPUT_FOLDER}/lighthouse-${TIMESTAMP}-${SANITIZED_PATH_NAME}"
lighthouse \
"${TARGET_URL}" \
--output=json,html \
--output-path="${OUTPUT_PATH}" \
--only-categories=performance \
--chrome-flags="--headless" \
--screenEmulation.disabled \
--form-factor=desktop
# Display the performance score
SCORE=$(jq ".categories.performance.score" < "${OUTPUT_PATH}.report.json")
echo "${TARGET_URL},${SCORE}"
done
@nikitawootten-nist
Copy link
Author

nikitawootten-nist commented Feb 9, 2023

Example script output (excluding STDERR log output):

$ ./run_lighthouse.sh https://pages.nist.gov/OSCAL /tmp/oscal_lighthouse_test 2> /dev/null
path,score
https://pages.nist.gov/OSCAL/,0.52
https://pages.nist.gov/OSCAL/reference/1.0.0/complete/json-reference/,0.15
https://pages.nist.gov/OSCAL/reference/1.0.0/complete/xml-reference/,0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment