#!/bin/bash

# Debug script for performance_benchmarks.sh

source ./performance_benchmarks.sh

echo "Testing benchmark_request function..."
result=$(benchmark_request '["REQ","test",{}]')
echo "Result: $result"

echo "Testing full client subprocess..."
(
    client_start=$(date +%s)
    client_requests=0
    client_total_response_time=0
    client_successful_requests=0
    client_min_time=999999
    client_max_time=0

    while [[ $(($(date +%s) - client_start)) -lt 3 ]]; do
        result=$(benchmark_request '["REQ","test",{}]')
        IFS=':' read -r response_time success <<< "$result"

        client_total_response_time=$((client_total_response_time + response_time))
        client_requests=$((client_requests + 1))

        if [[ "$success" == "1" ]]; then
            client_successful_requests=$((client_successful_requests + 1))
        fi

        if [[ $response_time -lt client_min_time ]]; then
            client_min_time=$response_time
        fi

        if [[ $response_time -gt client_max_time ]]; then
            client_max_time=$response_time
        fi

        echo "Request $client_requests: ${response_time}ms, success=$success"
        sleep 0.1
    done

    echo "$client_requests:$client_successful_requests:$client_total_response_time:$client_min_time:$client_max_time"
) &
pid=$!
echo "Waiting for client..."
wait "$pid"
echo "Client finished."