rateLimit.bash 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env bash
  2. # Configuration
  3. URL="https://api.cloud.sciphi.ai/v3/health"
  4. TOTAL_REQUESTS=60
  5. SLEEP_INTERVAL=0.05
  6. REQUIRED_429_COUNT=20
  7. # Initialize counters
  8. count_429=0
  9. count_total=0
  10. # Function to handle exit codes
  11. check_exit_status() {
  12. if [ $count_429 -ge $REQUIRED_429_COUNT ]; then
  13. echo "✅ Test passed: Got $count_429 rate limits (429s), which meets the minimum requirement of $REQUIRED_429_COUNT"
  14. exit 0
  15. else
  16. echo "❌ Test failed: Only got $count_429 rate limits (429s), which is less than the required $REQUIRED_429_COUNT"
  17. exit 1
  18. fi
  19. }
  20. # Trap Ctrl+C and call check_exit_status
  21. trap check_exit_status INT
  22. echo "Starting rate limit test for $URL"
  23. echo "Target: At least $REQUIRED_429_COUNT rate limits (HTTP 429)"
  24. for ((i=1; i<=TOTAL_REQUESTS; i++)); do
  25. RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
  26. count_total=$((count_total + 1))
  27. # Color coding for different responses
  28. if [ "$RESPONSE" = "429" ]; then
  29. count_429=$((count_429 + 1))
  30. echo -e "\033[33mRequest $i: HTTP $RESPONSE (Rate limit) - Total 429s: $count_429\033[0m"
  31. elif [ "$RESPONSE" = "200" ]; then
  32. echo -e "\033[32mRequest $i: HTTP $RESPONSE (Success)\033[0m"
  33. else
  34. echo -e "\033[31mRequest $i: HTTP $RESPONSE (Error)\033[0m"
  35. fi
  36. sleep $SLEEP_INTERVAL
  37. done
  38. # Check final results
  39. check_exit_status