#!/bin/bash

# Usage: network-test [parallel_connections] [duration_seconds]

# Show help message
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
    echo "Network Speed Test for IVS"
    echo ""
    echo "Usage: network-test [parallel_connections] [duration_seconds]"
    echo ""
    echo "Arguments:"
    echo "  parallel_connections  Number of parallel connections (1-10, default: 1)"
    echo "  duration_seconds     Test duration in seconds (1-300, default: 10)"
    echo ""
    echo "Examples:"
    echo "  network-test         # 1 connection for 10 seconds"
    echo "  network-test 5       # 5 connections for 10 seconds"
    echo "  network-test 5 30    # 5 connections for 30 seconds"
    echo ""
    exit 0
fi

# Get IVS IP from environment variable
if [ -z "$imaging_address" ]; then
    echo "Error: imaging_address not set. This script should be run from imaging kernel debug mode."
    exit 1
fi
IVS_IP="$imaging_address"

# Default values
PARALLEL=${1:-1}
DURATION=${2:-10}

# Validate parallel connections (1-10)
if ! [[ "$PARALLEL" =~ ^[0-9]+$ ]] || [ "$PARALLEL" -lt 1 ] || [ "$PARALLEL" -gt 10 ]; then
    echo "Error: Parallel connections must be between 1 and 10. Using default: 1"
    PARALLEL=1
fi

# Validate duration (1-300 seconds)
if ! [[ "$DURATION" =~ ^[0-9]+$ ]] || [ "$DURATION" -lt 1 ] || [ "$DURATION" -gt 300 ]; then
    echo "Error: Duration must be between 1 and 300 seconds. Using default: 10"
    DURATION=10
fi

echo "Running iperf3 speed test to IVS at ${IVS_IP}:5201"
echo "Parallel connections: ${PARALLEL}, Duration: ${DURATION}s"
echo "================================================"

iperf3 -c "${IVS_IP}" -P "${PARALLEL}" -t "${DURATION}"