← back to Watches

lighthouse-test.sh

104 lines

#!/bin/bash

###############################################################################
# LIGHTHOUSE PERFORMANCE TESTING SCRIPT
# Tests Core Web Vitals and generates performance report
###############################################################################

URL="http://45.61.58.125:7600"
OUTPUT_DIR="/root/Projects/watches/lighthouse-reports"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p "$OUTPUT_DIR"

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║            LIGHTHOUSE PERFORMANCE AUDIT                        ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""
echo "Testing: $URL"
echo "Timestamp: $TIMESTAMP"
echo ""

# Check if Lighthouse is installed
if ! command -v lighthouse &> /dev/null; then
    echo "📦 Installing Lighthouse CLI..."
    npm install -g lighthouse
fi

# Run Lighthouse audit
echo "🔍 Running Lighthouse audit (this may take 30-60 seconds)..."
echo ""

lighthouse "$URL" \
    --output=html \
    --output=json \
    --output-path="$OUTPUT_DIR/report_$TIMESTAMP" \
    --quiet \
    --chrome-flags="--headless --no-sandbox --disable-dev-shm-usage" \
    --only-categories=performance,accessibility,best-practices,seo

echo ""
echo "════════════════════════════════════════════════════════════════"
echo "                    LIGHTHOUSE RESULTS                           "
echo "════════════════════════════════════════════════════════════════"
echo ""

# Parse JSON results
if [ -f "$OUTPUT_DIR/report_$TIMESTAMP.report.json" ]; then
    JSON_FILE="$OUTPUT_DIR/report_$TIMESTAMP.report.json"

    # Extract scores using jq if available, otherwise use grep
    if command -v jq &> /dev/null; then
        PERF_SCORE=$(jq -r '.categories.performance.score * 100' "$JSON_FILE")
        ACCESSIBILITY_SCORE=$(jq -r '.categories.accessibility.score * 100' "$JSON_FILE")
        BEST_PRACTICES_SCORE=$(jq -r '.categories["best-practices"].score * 100' "$JSON_FILE")
        SEO_SCORE=$(jq -r '.categories.seo.score * 100' "$JSON_FILE")

        FCP=$(jq -r '.audits["first-contentful-paint"].displayValue' "$JSON_FILE")
        LCP=$(jq -r '.audits["largest-contentful-paint"].displayValue' "$JSON_FILE")
        TBT=$(jq -r '.audits["total-blocking-time"].displayValue' "$JSON_FILE")
        CLS=$(jq -r '.audits["cumulative-layout-shift"].displayValue' "$JSON_FILE")
        SI=$(jq -r '.audits["speed-index"].displayValue' "$JSON_FILE")

        echo "SCORES:"
        echo "  Performance:      ${PERF_SCORE}%"
        echo "  Accessibility:    ${ACCESSIBILITY_SCORE}%"
        echo "  Best Practices:   ${BEST_PRACTICES_SCORE}%"
        echo "  SEO:              ${SEO_SCORE}%"
        echo ""
        echo "CORE WEB VITALS:"
        echo "  First Contentful Paint (FCP):    $FCP"
        echo "  Largest Contentful Paint (LCP):  $LCP"
        echo "  Total Blocking Time (TBT):       $TBT"
        echo "  Cumulative Layout Shift (CLS):   $CLS"
        echo "  Speed Index:                     $SI"
    else
        echo "⚠️  jq not installed - install with: apt-get install jq"
        echo "View full report at: $OUTPUT_DIR/report_$TIMESTAMP.report.html"
    fi
fi

echo ""
echo "Reports saved:"
echo "  HTML: $OUTPUT_DIR/report_$TIMESTAMP.report.html"
echo "  JSON: $OUTPUT_DIR/report_$TIMESTAMP.report.json"
echo ""

# Quick performance check with curl
echo "════════════════════════════════════════════════════════════════"
echo "                  QUICK PERFORMANCE TEST                         "
echo "════════════════════════════════════════════════════════════════"
echo ""

for i in {1..5}; do
    RESULT=$(curl -o /dev/null -s -w "Time: %{time_total}s | DNS: %{time_namelookup}s | Connect: %{time_connect}s | TTFB: %{time_starttransfer}s | Size: %{size_download} bytes\n" "$URL")
    echo "Request $i: $RESULT"
done

echo ""
echo "════════════════════════════════════════════════════════════════"
echo ""
echo "Open HTML report:"
echo "  file://$OUTPUT_DIR/report_$TIMESTAMP.report.html"
echo ""