#!/bin/bash
# Diagnostic test to understand 7s vs 10s gap difference

echo "=== JOB TRANSITION DIAGNOSTIC TEST ==="
echo "Testing with different gap times while monitoring GPU state"
echo ""

rm -f /tmp/soprano_timing_debug.txt
rm -f /tmp/job_transition_log.txt

# Function to test with specific gap
test_gap() {
    local gap=$1
    local num_tests=$2
    
    echo "========================================" | tee -a /tmp/job_transition_log.txt
    echo "Testing with ${gap}s gap between jobs (${num_tests} runs)" | tee -a /tmp/job_transition_log.txt
    echo "========================================" | tee -a /tmp/job_transition_log.txt
    
    for i in $(seq 1 $num_tests); do
        echo -n "Job $i: Submitting..." | tee -a /tmp/job_transition_log.txt
        
        # Record start time
        start_time=$(date +%s.%N)
        
        # Submit job
        curl -s -X POST http://localhost:8765/api/speak \
            -H "Content-Type: application/json" \
            -d "{\"text\": \"Test job number $i with ${gap} second gap to measure performance consistency.\"}" \
            > /dev/null
        
        echo " submitted at $(date +%H:%M:%S.%N)" | tee -a /tmp/job_transition_log.txt
        
        # Monitor GPU during the gap
        echo "  Monitoring GPU during ${gap}s wait..." | tee -a /tmp/job_transition_log.txt
        
        for t in $(seq 1 $gap); do
            sleep 1
            gpu_state=$(rocm-smi --showclocks --showuse 2>/dev/null | grep -E "sclk|mclk|GPU use" | tr '\n' ' ')
            echo "    T+${t}s: $gpu_state" | tee -a /tmp/job_transition_log.txt
        done
    done
    
    # Wait for last job to complete
    echo "Waiting 10s for last job to complete..." | tee -a /tmp/job_transition_log.txt
    sleep 10
    
    # Show results
    echo "" | tee -a /tmp/job_transition_log.txt
    echo "Results for ${gap}s gap:" | tee -a /tmp/job_transition_log.txt
    tail -$((num_tests * 6)) /tmp/soprano_timing_debug.txt | grep -E "Realtime|Avg RVC" | \
        paste - - | awk -v gap=$gap '{print "  Job " NR ": " $3 " realtime, RVC: " $6}'  | tee -a /tmp/job_transition_log.txt
    
    echo "" | tee -a /tmp/job_transition_log.txt
}

# Test 1: 10 second gap (known good)
echo "TEST 1: 10 second gap (baseline - should be good)"
test_gap 10 3

sleep 5

# Test 2: 7 second gap (known bad)
echo ""
echo "TEST 2: 7 second gap (should show degradation)"
test_gap 7 3

sleep 5

# Test 3: 5 second gap (extreme - what happens?)
echo ""
echo "TEST 3: 5 second gap (extreme test)"
test_gap 5 3

sleep 5

# Test 4: 8 second gap (boundary test)
echo ""
echo "TEST 4: 8 second gap (finding the threshold)"
test_gap 8 3

echo ""
echo "========================================"
echo "ANALYSIS"
echo "========================================"
echo ""
echo "Full log saved to: /tmp/job_transition_log.txt"
echo ""
echo "Summary of results:"
grep "Job [0-9]:" /tmp/job_transition_log.txt | tail -12

echo ""
echo "GPU state patterns during gaps:"
echo "Look for GPU clock/memory differences between good and bad runs"
