#!/bin/bash # Benchmark script to test cache performance improvement # Compares performance with and without cache set -e echo "==================================================" echo "CV Application Cache Performance Benchmark" echo "==================================================" echo "" # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Configuration BASE_URL="http://localhost:1999" REQUESTS=100 CONCURRENT=10 echo -e "${BLUE}Configuration:${NC}" echo " - Requests: $REQUESTS" echo " - Concurrent: $CONCURRENT" echo " - Languages: en, es" echo "" # Test 1: Sequential requests (warm cache) echo -e "${YELLOW}Test 1: Sequential Performance (Cache Warmed)${NC}" echo "Making $REQUESTS sequential requests..." start=$(date +%s.%N) for i in $(seq 1 $REQUESTS); do curl -s -o /dev/null "$BASE_URL/?lang=en" curl -s -o /dev/null "$BASE_URL/?lang=es" done end=$(date +%s.%N) sequential_time=$(echo "$end - $start" | bc) sequential_rps=$(echo "scale=2; ($REQUESTS * 2) / $sequential_time" | bc) echo -e "${GREEN}✓ Sequential Test Complete${NC}" echo " Total time: ${sequential_time}s" echo " Requests/sec: $sequential_rps" echo "" # Test 2: Check cache statistics echo -e "${YELLOW}Test 2: Cache Statistics${NC}" cache_stats=$(curl -s "$BASE_URL/health" | jq '.cache') echo "$cache_stats" echo "" # Test 3: Concurrent load test using Apache Bench (if available) if command -v ab &> /dev/null; then echo -e "${YELLOW}Test 3: Concurrent Load Test (Apache Bench)${NC}" echo "Testing English endpoint..." ab -n $REQUESTS -c $CONCURRENT -q "$BASE_URL/?lang=en" 2>&1 | grep -E "Requests per second|Time per request|Transfer rate" echo "" echo "Testing Spanish endpoint..." ab -n $REQUESTS -c $CONCURRENT -q "$BASE_URL/?lang=es" 2>&1 | grep -E "Requests per second|Time per request|Transfer rate" echo "" else echo -e "${YELLOW}Test 3: Concurrent Load Test (Manual)${NC}" echo "Apache Bench not available, using background curl..." start=$(date +%s.%N) for i in $(seq 1 $CONCURRENT); do ( for j in $(seq 1 10); do curl -s -o /dev/null "$BASE_URL/?lang=en" curl -s -o /dev/null "$BASE_URL/?lang=es" done ) & done wait end=$(date +%s.%N) concurrent_time=$(echo "$end - $start" | bc) concurrent_rps=$(echo "scale=2; ($CONCURRENT * 10 * 2) / $concurrent_time" | bc) echo -e "${GREEN}✓ Concurrent Test Complete${NC}" echo " Total time: ${concurrent_time}s" echo " Requests/sec: $concurrent_rps" echo "" fi # Test 4: Response time percentiles echo -e "${YELLOW}Test 4: Response Time Percentiles (50 requests)${NC}" times=() for i in $(seq 1 50); do time=$(curl -s -o /dev/null -w "%{time_total}" "$BASE_URL/?lang=en") times+=($time) done # Sort times IFS=$'\n' sorted=($(sort -n <<<"${times[*]}")) unset IFS p50_idx=$(echo "(50 * 50 / 100) - 1" | bc) p95_idx=$(echo "(95 * 50 / 100) - 1" | bc) p99_idx=$(echo "(99 * 50 / 100) - 1" | bc) echo " p50 (median): ${sorted[$p50_idx]}s" echo " p95: ${sorted[$p95_idx]}s" echo " p99: ${sorted[$p99_idx]}s" echo "" # Final cache statistics echo -e "${YELLOW}Final Cache Statistics:${NC}" final_stats=$(curl -s "$BASE_URL/health" | jq '.cache') echo "$final_stats" echo "" echo -e "${GREEN}==================================================" echo "Benchmark Complete!" echo "==================================================${NC}"