@echo off
setlocal

REM Get the directory where the script is located
set SCRIPT_DIR=%~dp0
set HEY_BINARY=%SCRIPT_DIR%hey_linux_amd64

echo ==========================================
echo Distributed Stock Provider - Load Test
echo =========================================
echo.

REM Check if local hey binary exists
if not exist "%HEY_BINARY%" (
    echo Error: hey binary not found at %HEY_BINARY%
    echo Please ensure hey_linux_amd64 is in the load-test directory
    exit /b 1
)

echo Starting MAXIMUM LOAD test with Hey...
echo.
echo Test Configuration:
echo   - 2000 concurrent workers
echo   - 1 minute duration
echo   - Maximum throughput test
echo   - Target: http://localhost:80/api/stocks/cached/all
echo.
echo WARNING: This will generate MASSIVE load on your system!
echo.

REM First, get an auth token
echo Getting authentication token...
curl -s -X POST http://localhost:80/api/auth/register ^
  -H "Content-Type: application/json" ^
  -d "{\"username\":\"loadtest_%RANDOM%\",\"password\":\"testpass123\"}" > temp_auth.json

REM Extract token (basic parsing for Windows)
for /f "tokens=2 delims=:," %%a in ('findstr "accessToken" temp_auth.json') do set TOKEN=%%a
set TOKEN=%TOKEN:"=%
set TOKEN=%TOKEN: =%

if "%TOKEN%"=="" (
    echo Failed to get auth token. Trying with existing user...
    curl -s -X POST http://localhost:80/api/auth/login ^
      -H "Content-Type: application/json" ^
      -d "{\"username\":\"testuser\",\"password\":\"testpass123\"}" > temp_auth.json
    
    for /f "tokens=2 delims=:," %%a in ('findstr "accessToken" temp_auth.json') do set TOKEN=%%a
    set TOKEN=%TOKEN:"=%
    set TOKEN=%TOKEN: =%
)

del temp_auth.json

if "%TOKEN%"=="" (
    echo ERROR: Could not obtain authentication token
    exit /b 1
)

echo Token obtained successfully!
echo.
echo Running load test...
echo.

REM Run the load test with hey
"%HEY_BINARY%" -z 60s -c 2000 -H "Authorization: Bearer %TOKEN%" http://localhost:80/api/stocks/cached/all

echo.
echo ==========================================
echo Load test completed!
echo ==========================================
echo.
echo Check the summary above for:
echo   - Total requests and requests/sec
echo   - Response time distribution
echo   - Status code distribution
echo   - Load balancing across backend instances
echo ==========================================
pause

@REM Made with Bob
