← back to Handbag Auth Nextjs

scripts/devops/performance-test.sh

116 lines

#!/bin/bash
# Performance Testing Script for Auction Viewer
# Simulates load and measures performance metrics

set -euo pipefail

# Configuration
SERVICE_URL="http://45.61.58.125:7500"
LOG_FILE="/root/Projects/handbag-auth-nextjs/logs/performance-test.log"
CONCURRENT_REQUESTS=50
TOTAL_REQUESTS=1000

# Logging
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "${LOG_FILE}"
}

log "=========================================="
log "Starting performance test"
log "=========================================="
log "Target: ${SERVICE_URL}"
log "Concurrent requests: ${CONCURRENT_REQUESTS}"
log "Total requests: ${TOTAL_REQUESTS}"

# Test endpoints
ENDPOINTS=(
    "/health"
    "/api/stats"
    "/api/auctions"
    "/api/best-values"
    "/metrics"
)

# Function to test single endpoint
test_endpoint() {
    local endpoint=$1
    local url="${SERVICE_URL}${endpoint}"

    log "Testing: ${endpoint}"

    # Use Apache Bench if available, otherwise use curl loop
    if command -v ab &> /dev/null; then
        log "Using Apache Bench for load testing..."
        ab -n "${TOTAL_REQUESTS}" -c "${CONCURRENT_REQUESTS}" -q "${url}" > /tmp/ab_result.txt 2>&1

        # Parse results
        REQUESTS_PER_SEC=$(grep "Requests per second" /tmp/ab_result.txt | awk '{print $4}')
        TIME_PER_REQUEST=$(grep "Time per request" /tmp/ab_result.txt | head -1 | awk '{print $4}')
        FAILED_REQUESTS=$(grep "Failed requests" /tmp/ab_result.txt | awk '{print $3}')

        log "  Requests/sec: ${REQUESTS_PER_SEC}"
        log "  Time/request: ${TIME_PER_REQUEST}ms"
        log "  Failed: ${FAILED_REQUESTS}"

    else
        log "Apache Bench not found, using curl for basic test..."

        # Simple curl-based test
        START_TIME=$(date +%s%3N)
        for i in $(seq 1 10); do
            curl -s -o /dev/null -w "%{http_code}\n" "${url}" >> /tmp/curl_results.txt
        done
        END_TIME=$(date +%s%3N)

        DURATION=$((END_TIME - START_TIME))
        AVG_TIME=$((DURATION / 10))

        SUCCESS_COUNT=$(grep -c "200" /tmp/curl_results.txt || echo "0")

        log "  Average time: ${AVG_TIME}ms"
        log "  Success rate: ${SUCCESS_COUNT}/10"

        rm /tmp/curl_results.txt
    fi
}

# Run tests for each endpoint
for endpoint in "${ENDPOINTS[@]}"; do
    test_endpoint "${endpoint}"
    sleep 2
done

# Memory usage check
log "Checking memory usage..."
MEM_USAGE=$(pm2 show auction-viewer | grep "memory" | awk '{print $4}')
log "Current memory: ${MEM_USAGE}"

# CPU usage check
log "Checking CPU usage..."
CPU_USAGE=$(pm2 show auction-viewer | grep "cpu" | awk '{print $3}')
log "Current CPU: ${CPU_USAGE}"

# Database performance test
log "Testing database query performance..."
DB_PATH="/root/Projects/handbag-auth-nextjs/data/auction-history/auctions.db"

START_TIME=$(date +%s%3N)
RECORD_COUNT=$(sqlite3 "${DB_PATH}" "SELECT COUNT(*) FROM auctions;")
END_TIME=$(date +%s%3N)
QUERY_TIME=$((END_TIME - START_TIME))

log "Database records: ${RECORD_COUNT}"
log "Query time: ${QUERY_TIME}ms"

# Recommendations
log "=========================================="
log "Performance Test Complete"
log "=========================================="

if [ -f /tmp/ab_result.txt ]; then
    cat /tmp/ab_result.txt >> "${LOG_FILE}"
    rm /tmp/ab_result.txt
fi

log "Full results saved to: ${LOG_FILE}"