benchmark-http.sh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env bash
  2. ## On Ubuntu, this script requires apache2-utils for the ab binary.
  3. # Run like: ./benchmark-http.sh server
  4. # where server is the host/port running the web server
  5. declare -A THROUGHPUTS
  6. declare -A LATENCIES
  7. LOOP=5
  8. DOWNLOAD_HOST=$1
  9. DOWNLOAD_FILE=random/10K.1.html
  10. REQUESTS=10000
  11. CONCURRENCY_LIST="1 2 4 8 16 32 64 128 256"
  12. OPTIONS="-k"
  13. RESULT=result-$(date +%y%m%d-%H%M%S)
  14. touch $RESULT
  15. RUN=0
  16. while [ $RUN -lt $LOOP ]
  17. do
  18. for CONCURRENCY in $CONCURRENCY_LIST
  19. do
  20. rm -f OUTPUT
  21. echo "ab $OPTIONS -n $REQUESTS -c $CONCURRENCY http://$DOWNLOAD_HOST/$DOWNLOAD_FILE"
  22. ab $OPTIONS -n $REQUESTS -c $CONCURRENCY http://$DOWNLOAD_HOST/$DOWNLOAD_FILE > OUTPUT || exit $?
  23. sleep 5
  24. THROUGHPUT=$(grep -m1 "Requests per second:" OUTPUT | awk '{ print $4 }')
  25. LATENCY=$(grep -m1 "Time per request:" OUTPUT | awk '{ print $4 }')
  26. THROUGHPUTS[$CONCURRENCY]="${THROUGHPUTS[$CONCURRENCY]} $THROUGHPUT"
  27. LATENCIES[$CONCURRENCY]="${LATENCIES[$CONCURRENCY]} $LATENCY"
  28. echo "concurrency=$CONCURRENCY, throughput=$THROUGHPUT, latency=$LATENCY"
  29. done
  30. RUN=$(expr $RUN + 1)
  31. done
  32. for CONCURRENCY in $CONCURRENCY_LIST
  33. do
  34. 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}')
  35. 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}')
  36. echo "$THROUGHPUT,$LATENCY" >> $RESULT
  37. done
  38. echo "Result file: $RESULT"