-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·259 lines (223 loc) · 5.56 KB
/
run.sh
File metadata and controls
executable file
·259 lines (223 loc) · 5.56 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
INFRA_DIR="$SCRIPT_DIR/infra"
APP_BINARY="$ROOT/target/release/forge-bench"
LOADGEN_BINARY="$ROOT/target/release/loadgen"
JWT_SECRET_VALUE="${JWT_SECRET:-bench-secret-not-for-production}"
FORGE_INSTANCES=2
BASE_PORT=9081
BASE_GRPC_PORT=9000
POOL_SIZE=40
GATEWAY_MAX_CONNECTIONS=16000
SSE_MAX_SESSIONS=12000
LOCAL_PRIMARY_URL="postgres://postgres:postgres@localhost:5432/app"
LOCAL_REPLICA_URLS=(
"postgres://postgres:postgres@localhost:5433/app"
)
DB_URL=""
REPLICA_URLS=()
FORGE_URLS=()
APP_PIDS=()
APP_CONFIGS=()
APP_LOGS=()
STARTED_LOCAL_DB=0
MAX_DURATION=""
usage() {
cat <<'EOF'
Usage:
./benchmarks/app/run.sh
./benchmarks/app/run.sh --max-duration 30m
./benchmarks/app/run.sh --database-url URL [--replica-url URL]
./benchmarks/app/run.sh --forge-url URL [--forge-url URL ...]
Modes:
no args
Start 1 local primary, 1 local replica, 2 local Forge instances, then ramp until stop.
--max-duration
Optional upper bound. By default the run is unlimited and only stops on p90/error thresholds.
--database-url [+ --replica-url]
Use that database, start 2 local Forge instances against it, then ramp until stop.
--forge-url
Skip local database and local Forge. Hit the supplied Forge URL(s) directly.
EOF
}
cleanup() {
for pid in "${APP_PIDS[@]:-}"; do
kill "$pid" 2>/dev/null || true
done
for pid in "${APP_PIDS[@]:-}"; do
wait "$pid" 2>/dev/null || true
done
for config in "${APP_CONFIGS[@]:-}"; do
rm -f "$config"
done
for log in "${APP_LOGS[@]:-}"; do
rm -f "$log"
done
if [[ "$STARTED_LOCAL_DB" -eq 1 ]]; then
docker compose -f "$INFRA_DIR/docker-compose.yml" down -v >/dev/null 2>&1 || true
fi
}
trap cleanup EXIT
while [[ $# -gt 0 ]]; do
case "$1" in
--database-url)
DB_URL="$2"
shift
;;
--replica-url)
REPLICA_URLS+=("$2")
shift
;;
--forge-url)
FORGE_URLS+=("$2")
shift
;;
--max-duration)
MAX_DURATION="$2"
shift
;;
--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage
exit 1
;;
esac
shift
done
quote_toml_array() {
local values=("$@")
local out="["
local first=1
local value
for value in "${values[@]}"; do
if [[ "$first" -eq 0 ]]; then
out+=", "
fi
out+="\"$value\""
first=0
done
out+="]"
printf '%s' "$out"
}
wait_for_postgres() {
local container="$1"
echo "Waiting for ${container}..."
for _ in $(seq 1 90); do
if docker exec "$container" sh -lc "psql -U postgres -d app -Atqc 'select 1'" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
echo "${container} did not become ready" >&2
docker logs "$container" || true
exit 1
}
start_local_database() {
STARTED_LOCAL_DB=1
DB_URL="$LOCAL_PRIMARY_URL"
REPLICA_URLS=("${LOCAL_REPLICA_URLS[@]}")
echo ""
echo "=== Starting PostgreSQL (1 primary + 1 replica) ==="
docker compose -f "$INFRA_DIR/docker-compose.yml" down -v >/dev/null 2>&1 || true
docker compose -f "$INFRA_DIR/docker-compose.yml" up -d >/dev/null
wait_for_postgres bench-pg-primary
wait_for_postgres bench-pg-replica
}
write_config() {
local file="$1"
local port="$2"
local grpc_port="$3"
local name="$4"
cat >"$file" <<EOF
[project]
name = "$name"
[node]
roles = ["gateway", "function"]
[database]
url = "$DB_URL"
pool_size = $POOL_SIZE
test_before_acquire = false
read_from_replica = true
replica_urls = $(quote_toml_array "${REPLICA_URLS[@]}")
[database.pools.default]
size = $POOL_SIZE
timeout_secs = 10
test_before_acquire = false
[gateway]
port = $port
grpc_port = $grpc_port
max_connections = $GATEWAY_MAX_CONNECTIONS
sse_max_sessions = $SSE_MAX_SESSIONS
request_timeout_secs = 60
[auth]
jwt_algorithm = "HS256"
jwt_secret = "$JWT_SECRET_VALUE"
[observability]
enabled = false
EOF
}
start_local_forge() {
echo ""
echo "=== Starting Forge instances ==="
for ((i=0; i<FORGE_INSTANCES; i++)); do
local port=$((BASE_PORT + i))
local grpc_port=$((BASE_GRPC_PORT + i))
local config_file
local log_file
config_file="$(mktemp "${TMPDIR:-/tmp}/forge-bench-${port}.toml.XXXXXX")"
log_file="$(mktemp "${TMPDIR:-/tmp}/forge-bench-${port}.log.XXXXXX")"
APP_CONFIGS+=("$config_file")
APP_LOGS+=("$log_file")
FORGE_URLS+=("http://127.0.0.1:${port}")
write_config "$config_file" "$port" "$grpc_port" "forge-bench-${port}"
(
cd "$SCRIPT_DIR"
FORGE_CONFIG="$config_file" \
JWT_SECRET="$JWT_SECRET_VALUE" \
RUST_LOG=error \
"$APP_BINARY"
) >"$log_file" 2>&1 &
APP_PIDS+=("$!")
done
for url in "${FORGE_URLS[@]}"; do
echo "Waiting for ${url}..."
for _ in $(seq 1 60); do
if curl -sf "${url}/_api/ready" >/dev/null 2>&1; then
break
fi
sleep 1
done
curl -sf "${url}/_api/ready" >/dev/null
done
}
echo "=== Building benchmark binaries ==="
cargo build --release -p forge-bench --bins
if [[ ${#FORGE_URLS[@]} -gt 0 ]]; then
echo ""
echo "=== Using external Forge ==="
else
if [[ -z "$DB_URL" && ${#REPLICA_URLS[@]} -eq 0 ]]; then
command -v docker >/dev/null 2>&1 || { echo "Missing: docker"; exit 1; }
start_local_database
else
if [[ -z "$DB_URL" ]]; then
echo "External database mode requires --database-url." >&2
exit 1
fi
fi
start_local_forge
fi
echo ""
echo "=== Running benchmark ==="
CMD=("$LOADGEN_BINARY")
if [[ -n "$MAX_DURATION" ]]; then
CMD+=(--max-duration "$MAX_DURATION")
fi
CMD+=("${FORGE_URLS[@]}")
"${CMD[@]}"