-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench-examples.sh
More file actions
executable file
·171 lines (156 loc) · 4.6 KB
/
Copy pathbench-examples.sh
File metadata and controls
executable file
·171 lines (156 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env sh
set -eu
BASE_URL="${BASE_URL:-http://127.0.0.1:${NGX_SQLITE_EXAMPLE_PORT:-8080}}"
BASE_URL="${BASE_URL%/}"
TOKEN="${TOKEN:-change-me}"
TYPE="all"
REQUESTS="${REQUESTS:-100}"
DEVICE_ID="${DEVICE_ID:-bench-device-$(date +%s)}"
# This benchmark uses curl's measured total time to stay dependency-free
usage() {
cat <<EOF
usage: scripts/bench-examples.sh [--type status|json|json_rows|ndjson|csv|all] [--requests N]
environment:
BASE_URL=http://127.0.0.1:8080
TOKEN=change-me
DEVICE_ID=bench-device-id
EOF
}
fail() {
echo "bench-examples: $*" >&2
exit 1
}
while [ "$#" -gt 0 ]; do
case "$1" in
--type)
[ "$#" -ge 2 ] || fail "--type needs status, json, json_rows, ndjson, csv, or all"
TYPE="$2"
shift 2
;;
--requests | -n)
[ "$#" -ge 2 ] || fail "--requests needs a positive integer"
REQUESTS="$2"
shift 2
;;
--base-url)
[ "$#" -ge 2 ] || fail "--base-url needs a URL"
BASE_URL="${2%/}"
shift 2
;;
--token)
[ "$#" -ge 2 ] || fail "--token needs a bearer token"
TOKEN="$2"
shift 2
;;
--help | -h)
usage
exit 0
;;
*)
fail "unknown option: $1"
;;
esac
done
case "$TYPE" in
status | json | json_rows | ndjson | csv | all) ;;
*) fail "--type must be status, json, json_rows, ndjson, csv, or all" ;;
esac
case "$REQUESTS" in
'' | *[!0-9]*) fail "--requests must be a positive integer" ;;
esac
[ "$REQUESTS" -gt 0 ] || fail "--requests must be greater than zero"
seed_example_data() {
# Seed once so read benchmarks measure the module path, not setup failures
curl -fsS "$BASE_URL/examples/bootstrap" >/dev/null
curl -fsS -X POST "$BASE_URL/examples/devices/json" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d "{\"id\":\"$DEVICE_ID\",\"name\":\"Benchmark Device\",\"attrs\":{\"bench\":true}}" >/dev/null
}
request_once() {
# Keep status-only and JSON benchmarks on separate endpoints so response shape
# costs can be compared independently
case "$1" in
status)
curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' \
-X POST "$BASE_URL/examples/devices/json" \
-H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d "{\"id\":\"$DEVICE_ID\",\"name\":\"Benchmark Device\",\"attrs\":{\"bench\":true,\"format\":\"status\"}}"
;;
json)
curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' \
"$BASE_URL/examples/devices/detail?id=$DEVICE_ID"
;;
json_rows)
curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' \
"$BASE_URL/examples/devices/list?limit=50&offset=0"
;;
ndjson)
curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' \
"$BASE_URL/examples/devices/list.ndjson?limit=50&offset=0"
;;
csv)
curl -sS -o /dev/null -w '%{http_code} %{time_total}\n' \
"$BASE_URL/examples/devices/list.csv?limit=50&offset=0"
;;
esac
}
run_benchmark() {
bench_type="$1"
tmp="${TMPDIR:-/tmp}/ngx-sqlite-bench-$$-$bench_type.log"
rm -f "$tmp"
i=1
while [ "$i" -le "$REQUESTS" ]; do
# Failed curl requests are recorded as status 000 so awk can summarize them
if ! request_once "$bench_type" >>"$tmp"; then
printf '000 0\n' >>"$tmp"
fi
i=$((i + 1))
done
if ! awk -v bench_type="$bench_type" '
{
total++;
code = $1;
elapsed = $2 + 0;
codes[code]++;
sum += elapsed;
if (total == 1 || elapsed < min) min = elapsed;
if (elapsed > max) max = elapsed;
if (code >= 200 && code < 400) ok++;
else failed++;
}
END {
avg = total ? sum / total : 0;
rate = sum > 0 ? ok / sum : 0;
printf "%s benchmark\n", bench_type;
printf "requests: %d\n", total;
printf "success: %d\n", ok;
printf "failed: %d\n", failed;
printf "avg_time: %.6fs\n", avg;
printf "min_time: %.6fs\n", min;
printf "max_time: %.6fs\n", max;
printf "throughput: %.2f req/s\n", rate;
printf "status_codes:";
for (code in codes) {
printf " %s=%d", code, codes[code];
}
printf "\n\n";
if (failed > 0) exit 1;
}
' "$tmp"; then
rm -f "$tmp"
return 1
fi
rm -f "$tmp"
}
seed_example_data
if [ "$TYPE" = "all" ]; then
run_benchmark status
run_benchmark json
run_benchmark json_rows
run_benchmark ndjson
run_benchmark csv
else
run_benchmark "$TYPE"
fi