benchmark-http.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/bin/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. RESULT=result-$(date +%y%m%d-%H%M%S)
  13. touch $RESULT
  14. RUN=0
  15. while [ $RUN -lt $LOOP ]
  16. do
  17. for CONCURRENCY in $CONCURRENCY_LIST
  18. do
  19. rm -f OUTPUT
  20. echo "ab -n $REQUESTS -c $CONCURRENCY http://$DOWNLOAD_HOST/$DOWNLOAD_FILE"
  21. ab -n $REQUESTS -c $CONCURRENCY http://$DOWNLOAD_HOST/$DOWNLOAD_FILE > OUTPUT
  22. sleep 5
  23. THROUGHPUT=$(grep -m1 "Requests per second:" OUTPUT | awk '{ print $4 }')
  24. LATENCY=$(grep -m1 "Time per request:" OUTPUT | awk '{ print $4 }')
  25. THROUGHPUTS[$CONCURRENCY]="${THROUGHPUTS[$CONCURRENCY]} $THROUGHPUT"
  26. LATENCIES[$CONCURRENCY]="${LATENCIES[$CONCURRENCY]} $LATENCY"
  27. echo "concurrency=$CONCURRENCY, throughput=$THROUGHPUT, latency=$LATENCY"
  28. done
  29. RUN=$(expr $RUN + 1)
  30. done
  31. for CONCURRENCY in $CONCURRENCY_LIST
  32. do
  33. 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}')
  34. 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}')
  35. echo "$THROUGHPUT,$LATENCY" >> $RESULT
  36. done
  37. echo "Result file: $RESULT"