|
| 1 | +#!/bin/sh |
| 2 | +# Simple nginx/wrk bench script |
| 3 | +# Concept by Brad Davis (brd@FreeBSD.org) |
| 4 | +# In server mode: Generate nginx simple setup with some data files |
| 5 | +# In client mode: Start wrk against given host |
| 6 | +# Usage: |
| 7 | +# - No arg: start in server mode |
| 8 | +# - IP or hostname as arg: start in client mode |
| 9 | + |
| 10 | +set -eu -o pipefail |
| 11 | + |
| 12 | +WWW_DATA=/tmp/www |
| 13 | + |
| 14 | +start_nginx() { |
| 15 | + if ! which nginx; then |
| 16 | + echo "Need nginx installed" |
| 17 | + exit 1 |
| 18 | + fi |
| 19 | + # Create some test files |
| 20 | + TESTCOUNT=0 |
| 21 | + TESTMAX=30 |
| 22 | + mkdir -p ${WWW_DATA} |
| 23 | + while [ ${TESTCOUNT} -le ${TESTMAX} ]; do |
| 24 | + dd if=/dev/urandom bs=1m count=20 of=${WWW_DATA}/test${TESTCOUNT} |
| 25 | + TESTCOUNT=$(( ${TESTCOUNT} + 1 )) |
| 26 | + done |
| 27 | + |
| 28 | + # Configure nginx |
| 29 | + cat << EOF > /usr/local/etc/nginx/nginx.conf |
| 30 | +worker_processes auto; |
| 31 | +events { |
| 32 | + worker_connections 1024; |
| 33 | +} |
| 34 | +http { |
| 35 | + include mime.types; |
| 36 | + default_type application/octet-stream; |
| 37 | + sendfile on; |
| 38 | + error_log off; |
| 39 | + access_log off; |
| 40 | + server { |
| 41 | + listen 80; |
| 42 | + server_name $(hostname); |
| 43 | + location / { |
| 44 | + root ${WWW_DATA}/; |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +EOF |
| 49 | + |
| 50 | + # Start up nginx |
| 51 | + service nginx onestart |
| 52 | +} |
| 53 | + |
| 54 | +start_wrk() { |
| 55 | + if ! which wrk; then |
| 56 | + echo "Need wrk installed" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + SERVERIP=$1 |
| 60 | + echo "Start executing tests against: ${SERVERIP}" |
| 61 | + cat << EOF > /tmp/random.lua |
| 62 | +-- generate a request for a random file named test[0-30] |
| 63 | +
|
| 64 | +request = function() |
| 65 | + num = math.random(0,30) |
| 66 | + path = "/test" .. num |
| 67 | + wrk.headers["X-Test"] = "test" .. num |
| 68 | + return wrk.format(nil, path) |
| 69 | +end |
| 70 | +
|
| 71 | +done = function(summary, latency, requests) |
| 72 | + file = io.open('/tmp/result.json', 'a') |
| 73 | + io.output(file) |
| 74 | + io.write(string.format("{\"requests_sec\":%.2f, \"transfer_sec\":%.2fMB, \"avg_latency_ms\":%.2f, \"errors_sum\":%.2f, \"duration\":%.2f,\"requests\":%.2f, \"bytes\":%.2f, \"latency.min\":%.2f, \"latency.max\":%.2f, \"latency.mean\":%.2f, \"latency.stdev\":%.2f}", |
| 75 | + summary.requests/(summary.duration/1000000), |
| 76 | + summary.bytes/(summary.duration*1048576/1000000), |
| 77 | + (latency.mean/1000), |
| 78 | + summary.errors.connect + summary.errors.read + summary.errors.write + summary.errors.status + summary.errors.timeout, |
| 79 | + summary.duration, |
| 80 | + summary.requests, |
| 81 | + summary.bytes, |
| 82 | + latency.min, |
| 83 | + latency.max, |
| 84 | + latency.mean, |
| 85 | + latency.stdev |
| 86 | + ) |
| 87 | + ) |
| 88 | +end |
| 89 | +EOF |
| 90 | + wrk -c2500 -d5m -t30 -s /tmp/random.lua http://${SERVERIP} |
| 91 | + echo "Tests finished" |
| 92 | +} |
| 93 | + |
| 94 | +if [ $# -eq 1 ]; then |
| 95 | + echo "Client mode targetting $1" |
| 96 | + start_wrk $1 |
| 97 | +else |
| 98 | + echo "Server mode: starting nginx" |
| 99 | + if [ $(id -u) -ne 0 ]; then |
| 100 | + echo "Need root permission to configure and restart nginx" |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + service nginx onestop || true |
| 104 | + start_nginx |
| 105 | +fi |
0 commit comments