alexsusanu@docs:Curl Performance Testing Script $
alexsusanu@docs
:~$ cat Curl Performance Testing Script.md

HomePERFORMANCE → Curl Performance Testing Script

Curl Performance Testing Script

Bash script for testing website performance using curl with session cookie support.

Usage

./curl_performance_script.sh <URL> [cookie_file]

What This Script Does

  • Tests website performance using curl (command-line HTTP client)
  • Runs 5 performance tests against your target URL
  • Measures detailed timing metrics for each request
  • Calculates averages across all tests
  • Uses session cookies for authenticated sites (SSO/corporate sites)
  • Saves detailed results to timestamped log file

What It's Testing

  • DNS Resolution time: How long to look up the server IP address
  • TCP Connection time: How long to establish network connection
  • SSL Handshake time: How long for HTTPS encryption setup
  • Time to First Byte (TTFB): How long until server starts responding
  • Total Response time: Complete request/response cycle
  • Download speeds and data sizes
  • HTTP response codes (200=success, 4xx/5xx=errors)
  • Network details (IPs, ports, redirects)

Performance Benchmarks

  • Good: Total time < 1s, TTFB < 500ms
  • Acceptable: Total time < 3s, TTFB < 1s
  • Poor: Total time > 3s (script will warn you)

How to Get Session Cookies from Browser

Chrome/Edge

  1. Login to your SSO site in browser
  2. Press F12 to open DevTools
  3. Go to Application tab > Storage section > Cookies
  4. Click on your domain name
  5. Find session/auth cookies (usually named: session_id, JSESSIONID, auth_token, etc.)
  6. Copy the Name and Value columns
  7. Save to file as: cookie_name=cookie_value; another_cookie=another_value

Firefox

  1. Login to your SSO site in browser
  2. Press F12 to open DevTools
  3. Go to Storage tab > Cookies
  4. Select your domain from the list
  5. Copy the session cookies
  6. Save to file in same format as above

Safari

  1. Login to your SSO site in browser
  2. Enable Developer menu: Safari > Preferences > Advanced > Show Develop menu
  3. Develop menu > Web Inspector (or press Cmd+Option+I)
  4. Go to Storage tab > Cookies
  5. Select your domain and copy session cookies
  6. Save to file in same format as above

Just paste the raw cookie value from browser DevTools:

abc123def456789xyz

Script Code

#!/bin/bash

# Curl Performance Testing Script with Session Cookie Support
# Usage: ./curl_performance_script.sh <URL> [cookie_file]

if [ $# -eq 0 ]; then
    echo "Usage: $0 <URL> [cookie_file]"
    echo ""
    echo "To get session cookie from browser:"
    echo "1. Login to your site"
    echo "2. Open DevTools (F12)"
    echo "3. Go to Application/Storage tab > Cookies"
    echo "4. Copy session cookie and save to file"
    echo ""
    exit 1
fi

URL="$1"
COOKIE_FILE="${2:-cookies.txt}"
RESULTS_FILE="performance_$(date +%Y%m%d_%H%M%S).log"

echo "=== CURL PERFORMANCE TEST ===" | tee $RESULTS_FILE
echo "URL: $URL" | tee -a $RESULTS_FILE
echo "Cookie File: $COOKIE_FILE" | tee -a $RESULTS_FILE
echo "Timestamp: $(date)" | tee -a $RESULTS_FILE
echo "=========================" | tee -a $RESULTS_FILE

if [ ! -f "$COOKIE_FILE" ]; then
    echo "⚠ Cookie file not found: $COOKIE_FILE"
    echo "Creating empty cookie file. Add your session cookies manually."
    touch "$COOKIE_FILE"
fi

run_performance_test() {
    local test_num=$1
    echo "=== TEST $test_num ===" | tee -a $RESULTS_FILE

    curl -w "\
DNS Lookup:           %{time_namelookup}s\n\
TCP Connect:          %{time_connect}s\n\
SSL Handshake:        %{time_appconnect}s\n\
Pre-transfer:         %{time_pretransfer}s\n\
Redirect Time:        %{time_redirect}s\n\
Time to First Byte:   %{time_starttransfer}s\n\
Total Time:           %{time_total}s\n\
Download Speed:       %{speed_download} bytes/sec\n\
Upload Speed:         %{speed_upload} bytes/sec\n\
Size Downloaded:      %{size_download} bytes\n\
Size Uploaded:        %{size_upload} bytes\n\
Size Request:         %{size_request} bytes\n\
Size Header:          %{size_header} bytes\n\
HTTP Response Code:   %{http_code}\n\
Redirect Count:       %{num_redirects}\n\
Final URL:            %{url_effective}\n\
Content Type:         %{content_type}\n\
Remote IP:            %{remote_ip}\n\
SSL Verify Result:    %{ssl_verify_result}\n\
\n" \
         -H "User-Agent: Mozilla/5.0 (compatible; PerformanceTest/1.0)" \
         -H "Cookie: session=$(cat "$COOKIE_FILE" | tr -d '\n')" \
         -o "/tmp/response_$test_num.html" \
         -s \
         -L \
         -f \
         "$URL" | tee -a $RESULTS_FILE

    if [ $? -ne 0 ]; then
        echo "❌ Test $test_num failed" | tee -a $RESULTS_FILE
    fi

    echo "" | tee -a $RESULTS_FILE
}

echo "Running performance tests..." | tee -a $RESULTS_FILE
for i in {1..5}; do
    run_performance_test $i
    sleep 2
done

echo "=== CALCULATING AVERAGES ===" | tee -a $RESULTS_FILE

DNS_TIMES=($(grep "DNS Lookup:" $RESULTS_FILE | awk '{print $3}' | sed 's/s//'))
CONNECT_TIMES=($(grep "TCP Connect:" $RESULTS_FILE | awk '{print $3}' | sed 's/s//'))
SSL_TIMES=($(grep "SSL Handshake:" $RESULTS_FILE | awk '{print $3}' | sed 's/s//'))
TTFB_TIMES=($(grep "Time to First Byte:" $RESULTS_FILE | awk '{print $6}' | sed 's/s//'))
TOTAL_TIMES=($(grep "Total Time:" $RESULTS_FILE | awk '{print $3}' | sed 's/s//'))
DOWNLOAD_SIZES=($(grep "Size Downloaded:" $RESULTS_FILE | awk '{print $3}'))
DOWNLOAD_SPEEDS=($(grep "Download Speed:" $RESULTS_FILE | awk '{print $3}'))

if [ ${#DNS_TIMES[@]} -gt 0 ]; then
    AVG_DNS=$(printf '%s\n' "${DNS_TIMES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_CONNECT=$(printf '%s\n' "${CONNECT_TIMES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_SSL=$(printf '%s\n' "${SSL_TIMES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_TTFB=$(printf '%s\n' "${TTFB_TIMES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_TOTAL=$(printf '%s\n' "${TOTAL_TIMES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_SIZE=$(printf '%s\n' "${DOWNLOAD_SIZES[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')
    AVG_SPEED=$(printf '%s\n' "${DOWNLOAD_SPEEDS[@]}" | awk '{sum+=$1} END {if(NR>0) print sum/NR; else print 0}')

    echo "Average DNS Lookup:         ${AVG_DNS}s" | tee -a $RESULTS_FILE
    echo "Average TCP Connect:        ${AVG_CONNECT}s" | tee -a $RESULTS_FILE
    echo "Average SSL Handshake:      ${AVG_SSL}s" | tee -a $RESULTS_FILE
    echo "Average Time to First Byte: ${AVG_TTFB}s" | tee -a $RESULTS_FILE
    echo "Average Total Time:         ${AVG_TOTAL}s" | tee -a $RESULTS_FILE
    echo "Average Download Size:      ${AVG_SIZE} bytes" | tee -a $RESULTS_FILE
    echo "Average Download Speed:     ${AVG_SPEED} bytes/sec" | tee -a $RESULTS_FILE
fi

echo "" | tee -a $RESULTS_FILE
echo "=== PERFORMANCE SUMMARY ===" | tee -a $RESULTS_FILE
echo "Total Tests: 5" | tee -a $RESULTS_FILE
echo "Results saved to: $RESULTS_FILE" | tee -a $RESULTS_FILE
echo "Session cookies: $COOKIE_FILE" | tee -a $RESULTS_FILE

if [ -n "$AVG_TOTAL" ] && (( $(awk "BEGIN {print ($AVG_TOTAL > 3.0)}") )); then
    echo "⚠ WARNING: Average load time exceeds 3 seconds" | tee -a $RESULTS_FILE
fi

if grep -q "HTTP Response Code:   [45]" $RESULTS_FILE; then
    echo "⚠ WARNING: HTTP errors detected" | tee -a $RESULTS_FILE
fi

if [ -f "$COOKIE_FILE" ] && [ -s "$COOKIE_FILE" ]; then
    echo "✓ Session cookies available" | tee -a $RESULTS_FILE
else
    echo "⚠ No session cookies found" | tee -a $RESULTS_FILE
fi

echo "" | tee -a $RESULTS_FILE
echo "Performance test completed: $(date)" | tee -a $RESULTS_FILE

echo ""
echo "Results summary:"
echo "- Detailed log: $RESULTS_FILE"
echo "- Session cookies: $COOKIE_FILE" 
echo "- Average total time: ${AVG_TOTAL}s"
Last updated: 2025-08-26 20:00 UTC