benchmark-ab.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. PROTOCOL=${PROTOCOL:=http}
  3. declare -A THROUGHPUTS
  4. declare -A LATENCIES
  5. LOOP=5
  6. DOWNLOAD_HOST=$1
  7. DOWNLOAD_FILE=random/10K.1.html
  8. REQUESTS=10000
  9. CONCURRENCY_LIST="1 2 4 8 16 32 64 128 256"
  10. RESULT=result-$(date +%y%m%d-%H%M%S)
  11. URL="$PROTOCOL://$DOWNLOAD_HOST/$DOWNLOAD_FILE"
  12. touch $RESULT
  13. RUN=0
  14. while [ $RUN -lt $LOOP ]
  15. do
  16. for CONCURRENCY in $CONCURRENCY_LIST
  17. do
  18. rm -f OUTPUT
  19. echo "ab -n $REQUESTS -c $CONCURRENCY $URL"
  20. ab -n $REQUESTS -c $CONCURRENCY $URL >> OUTPUT
  21. sleep 5
  22. THROUGHPUT=$(grep -m1 "Requests per second:" OUTPUT | awk '{ print $4 }')
  23. LATENCY=$(grep -m1 "Time per request:" OUTPUT | awk '{ print $4 }')
  24. THROUGHPUTS[$CONCURRENCY]="${THROUGHPUTS[$CONCURRENCY]} $THROUGHPUT"
  25. LATENCIES[$CONCURRENCY]="${LATENCIES[$CONCURRENCY]} $LATENCY"
  26. echo "concurrency=$CONCURRENCY, throughput=$THROUGHPUT, latency=$LATENCY"
  27. done
  28. RUN=$(expr $RUN + 1)
  29. done
  30. for CONCURRENCY in $CONCURRENCY_LIST
  31. do
  32. THROUGHPUT=$(echo ${THROUGHPUTS[$CONCURRENCY]} | tr " " "\n" | sort -n | awk '{a[NR]=$0}END{if(NR%2==1)print a[int(NR/2)+1];else print(a[NR/2-1]+a[NR/2])/2}')
  33. LATENCY=$(echo ${LATENCIES[$CONCURRENCY]} | tr " " "\n" | sort -n | awk '{a[NR]=$0}END{if(NR%2==1)print a[int(NR/2)+1];else print(a[NR/2-1]+a[NR/2])/2}')
  34. echo "$THROUGHPUT,$LATENCY" >> $RESULT
  35. done
  36. echo "Result file: $RESULT"