#!/bin/bash
# Vegeta load test pour veza-backend-api
# Usage: ./scripts/loadtest/vegeta_load_test.sh [BASE_URL]

set -e

BASE_URL="${1:-http://localhost:8080}"
DURATION="${DURATION:-30s}"
RATE="${RATE:-10/s}"

echo "Load test avec Vegeta"
echo "===================="
echo "URL: $BASE_URL"
echo "Durée: $DURATION"
echo "Taux: $RATE"
echo ""

# Créer fichier targets
TARGETS_FILE=$(mktemp)
trap "rm -f $TARGETS_FILE" EXIT

cat > "$TARGETS_FILE" <<EOF
GET $BASE_URL/health
GET $BASE_URL/readyz
POST $BASE_URL/api/v1/auth/login
Content-Type: application/json
{"email":"test@example.com","password":"invalid"}
GET $BASE_URL/api/v1/tracks
EOF

echo "Exécution du test..."
vegeta attack -duration="$DURATION" -rate="$RATE" -targets="$TARGETS_FILE" | \
  vegeta report

echo ""
echo "Pour un rapport détaillé:"
echo "  vegeta attack -duration=$DURATION -rate=$RATE -targets=$TARGETS_FILE | vegeta report -type=json > results.json"
