time_test.sh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. if [ $# -lt 3 ]; then
  3. echo "Usage: time_test.sh <domain> <key> <count> [<model>]"
  4. exit 1
  5. fi
  6. domain=$1
  7. key=$2
  8. count=$3
  9. model=${4:-"gpt-3.5-turbo"} # 设置默认模型为 gpt-3.5-turbo
  10. total_time=0
  11. times=()
  12. for ((i=1; i<=count; i++)); do
  13. result=$(curl -o /dev/null -s -w "%{http_code} %{time_total}\\n" \
  14. https://"$domain"/v1/chat/completions \
  15. -H "Content-Type: application/json" \
  16. -H "Authorization: Bearer $key" \
  17. -d '{"messages": [{"content": "echo hi", "role": "user"}], "model": "'"$model"'", "stream": false, "max_tokens": 1}')
  18. http_code=$(echo "$result" | awk '{print $1}')
  19. time=$(echo "$result" | awk '{print $2}')
  20. echo "HTTP status code: $http_code, Time taken: $time"
  21. total_time=$(bc <<< "$total_time + $time")
  22. times+=("$time")
  23. done
  24. average_time=$(echo "scale=4; $total_time / $count" | bc)
  25. sum_of_squares=0
  26. for time in "${times[@]}"; do
  27. difference=$(echo "scale=4; $time - $average_time" | bc)
  28. square=$(echo "scale=4; $difference * $difference" | bc)
  29. sum_of_squares=$(echo "scale=4; $sum_of_squares + $square" | bc)
  30. done
  31. standard_deviation=$(echo "scale=4; sqrt($sum_of_squares / $count)" | bc)
  32. echo "Average time: $average_time±$standard_deviation"