#!/bin/bash
# Simple test: measure actual job completion times and GPU state

echo "=== GAP TIMING TEST ==="
echo "Testing to find minimum gap that maintains performance"
echo ""

rm -f /tmp/soprano_timing_debug.txt

test_with_gap() {
    local gap=$1
    local runs=$2
    
    echo "========================================"
    echo "Testing ${gap}s gap (${runs} runs)"
    echo "========================================"
    
    for i in $(seq 1 $runs); do
        echo "Run $i: Starting at $(date +%H:%M:%S)"
        
        # Submit job and measure response time
        start=$(date +%s.%N)
        curl -s -X POST http://localhost:8765/api/speak \
            -H "Content-Type: application/json" \
            -d "{\"text\": \"Gap test run number $i with ${gap} second interval to verify performance consistency.\"}" \
            > /dev/null
        end=$(date +%s.%N)
        
        response_time=$(echo "$end - $start" | bc)
        echo "  Response time: ${response_time}s"
        
        # Wait for specified gap
        echo "  Waiting ${gap}s before next run..."
        sleep $gap
    done
    
    echo ""
    echo "Waiting 8s for last job to complete..."
    sleep 8
    
    echo ""
    echo "Results:"
    tail -$((runs * 6)) /tmp/soprano_timing_debug.txt | grep -E "Realtime|Avg RVC" | \
        paste - - | awk '{print "  Run " NR ": " $3 " realtime, RVC: " $6}'
    echo ""
}

# Test different gaps
echo "TEST 1: 10s gap (baseline)"
test_with_gap 10 3

echo "TEST 2: 8s gap"
test_with_gap 8 3

echo "TEST 3: 6s gap"  
test_with_gap 6 3

echo "TEST 4: 5s gap"
test_with_gap 5 3

echo ""
echo "========================================"
echo "SUMMARY"
echo "========================================"
cat /tmp/soprano_timing_debug.txt | grep -E "Realtime factor" | tail -12
