Last active
April 18, 2025 10:29
-
-
Save peterjaap/19fe6326433330fbfebd58e54ab94a28 to your computer and use it in GitHub Desktop.
Varnish test script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ======================== | |
# Varnish Cache Test Script for Magento 2 | |
# ======================== | |
# --- Configurable URLs --- | |
BASE_URL="https://mydomain.com" | |
# URLs can be comma-separated for multiple testing | |
URL_HOMEPAGE="/" | |
URL_PRODUCT="/first-product,/second-product" | |
URL_CATEGORY="/first-category,/second-category,/second-category/with-subcategory" | |
URL_CMS="/about-us,/contact" | |
URL_CART="/checkout/cart" | |
URL_CHECKOUT="/checkout" | |
URL_LOGIN="/customer/account/login" | |
URL_LOGOUT="/customer/account/logout" | |
URL_ADMIN_LOGIN="/admin" | |
URL_SEARCH="/catalogsearch/result/?q=shirt" | |
URL_404="/thisurldoesnotexistsoitshouldgivea404,/thisshouldalsnotexist" | |
# --- Pages expected to be cached --- | |
CACHED_URLS=( | |
"$URL_HOMEPAGE" | |
"$URL_PRODUCT" | |
"$URL_CATEGORY" | |
"$URL_CMS" | |
) | |
# --- Pages NOT expected to be cached --- | |
NONCACHED_URLS=( | |
"$URL_CART" | |
"$URL_CHECKOUT" | |
"$URL_LOGIN" | |
"$URL_LOGOUT" | |
"$URL_ADMIN_LOGIN" | |
"$URL_SEARCH" | |
"$URL_404" | |
) | |
# --- Functions --- | |
function test_cache_hit { | |
local path=$1 | |
local label=$2 | |
local full_url="${BASE_URL}${path}" | |
# First request (prime cache) | |
curl -s -o /dev/null -H "Host: $(echo $BASE_URL | awk -F/ '{print $3}')" "$full_url" | |
# Second request (check for HIT) | |
local header=$(curl -s -o /dev/null -D - "$full_url" | grep -i 'X-Magento-Cache-Debug') | |
if [ -z "$header" ]; then | |
local status="❌ NO HEADER" | |
else | |
if echo "$header" | grep -q 'HIT'; then | |
local status="✅ HIT - $header" | |
else | |
local status="❌ MISS - $header" | |
fi | |
fi | |
printf "%-100s %s\n" "$full_url" "$status" | |
} | |
function test_cache_miss { | |
local path=$1 | |
local label=$2 | |
local full_url="${BASE_URL}${path}" | |
# Just one request, we expect it to be MISS or not have X-Magento-Cache-Debug | |
local header=$(curl -s -o /dev/null -D - "$full_url" | grep -i 'X-Magento-Cache-Debug') | |
if [ -z "$header" ]; then | |
local status="✅ NO HEADER (as expected)" | |
else | |
if echo "$header" | grep -q 'HIT'; then | |
local status="⚠️ Unexpected HIT - $header" | |
else | |
local status="✅ MISS - $header" | |
fi | |
fi | |
printf "%-100s %s\n" "$full_url" "$status" | |
} | |
# --- Run Tests --- | |
echo "== Cached Pages ==" | |
for url_group in "${CACHED_URLS[@]}"; do | |
# Split comma-separated URLs | |
IFS=',' read -ra url_list <<< "$url_group" | |
for url in "${url_list[@]}"; do | |
# Trim any whitespace | |
url=$(echo "$url" | xargs) | |
label="$url" | |
test_cache_hit "$url" "$label" | |
done | |
done | |
echo | |
echo "== Non-Cached Pages ==" | |
for url_group in "${NONCACHED_URLS[@]}"; do | |
# Split comma-separated URLs | |
IFS=',' read -ra url_list <<< "$url_group" | |
for url in "${url_list[@]}"; do | |
# Trim any whitespace | |
url=$(echo "$url" | xargs) | |
label="$url" | |
test_cache_miss "$url" "$label" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment