#!/bin/bash
# parse_results.sh - Extract metrics from ApacheBench logs and format as Markdown tables.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RESULTS_DIR="${SCRIPT_DIR}/results"
CONCURRENCIES=(10 50 100 200 500)
SCENARIOS=("scenario1_single_pm2" "scenario2_clustered" "scenario3_multi_replica")
SCENARIO_NAMES=("Scenario 1: Single Instance Baseline (1 PM2 Process, 1 Container)" "Scenario 2: Single Container Clustered (4 PM2 Processes, 1 Container)" "Scenario 3: Multi-Replica Clustered (4 PM2 Processes per Container x 2 Replicas)")

echo "# Performance Benchmarking Results Summary"
echo ""

for s_idx in "${!SCENARIOS[@]}"; do
    scenario="${SCENARIOS[$s_idx]}"
    scenario_name="${SCENARIO_NAMES[$s_idx]}"
    
    echo "## ${scenario_name}"
    echo ""
    echo "### /health (GET - Lightweight Memory Endpoint)"
    echo "| Concurrency | Requests/sec (RPS) | Avg Latency (ms) | 99% Latency (ms) | Failed Requests |"
    echo "|-------------|---------------------|------------------|------------------|-----------------|"
    
    for c in "${CONCURRENCIES[@]}"; do
        log_file="${RESULTS_DIR}/${scenario}/health_c${c}.log"
        if [ -f "${log_file}" ]; then
            rps=$(grep -i "Requests per second:" "${log_file}" | awk '{print $4}')
            avg_lat=$(grep -i "Time per request:" "${log_file}" | head -n 1 | awk '{print $4}')
            failed=$(grep -i "Failed requests:" "${log_file}" | awk '{print $3}')
            lat_99=$(grep -A 10 "Percentage of the requests served" "${log_file}" | grep "99%" | awk '{print $2}')
            
            if [ -z "$rps" ]; then rps="ERR"; fi
            if [ -z "$avg_lat" ]; then avg_lat="ERR"; fi
            if [ -z "$failed" ]; then failed="0"; fi
            if [ -z "$lat_99" ]; then lat_99="ERR"; fi
            
            echo "| ${c} | ${rps} | ${avg_lat} | ${lat_99} | ${failed} |"
        fi
    done
    
    echo ""
    echo "### /api/about (POST - Database-Bound Endpoint)"
    echo "| Concurrency | Requests/sec (RPS) | Avg Latency (ms) | 99% Latency (ms) | Failed Requests |"
    echo "|-------------|---------------------|------------------|------------------|-----------------|"
    
    for c in "${CONCURRENCIES[@]}"; do
        log_file="${RESULTS_DIR}/${scenario}/about_c${c}.log"
        if [ -f "${log_file}" ]; then
            rps=$(grep -i "Requests per second:" "${log_file}" | awk '{print $4}')
            avg_lat=$(grep -i "Time per request:" "${log_file}" | head -n 1 | awk '{print $4}')
            failed=$(grep -i "Failed requests:" "${log_file}" | awk '{print $3}')
            lat_99=$(grep -A 10 "Percentage of the requests served" "${log_file}" | grep "99%" | awk '{print $2}')
            
            if [ -z "$rps" ]; then rps="ERR"; fi
            if [ -z "$avg_lat" ]; then avg_lat="ERR"; fi
            if [ -z "$failed" ]; then failed="0"; fi
            if [ -z "$lat_99" ]; then lat_99="ERR"; fi
            
            echo "| ${c} | ${rps} | ${avg_lat} | ${lat_99} | ${failed} |"
        fi
    done
    echo ""
done
