94 lines
2.7 KiB
Bash
Executable File
94 lines
2.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Benchmark HTTP latency + tài nguyên process (local hoặc container qua URL).
|
|
set -euo pipefail
|
|
|
|
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
BASE="${1:-http://127.0.0.1:8080}"
|
|
REQUESTS="${BENCH_REQUESTS:-100}"
|
|
WARMUP="${BENCH_WARMUP:-10}"
|
|
LABEL="${2:-}"
|
|
|
|
echo "=== lidar_manager_web benchmark ==="
|
|
echo "URL: $BASE"
|
|
echo "Requests/endpoint: $REQUESTS (warmup $WARMUP)"
|
|
[[ -n "$LABEL" ]] && echo "Label: $LABEL"
|
|
echo
|
|
|
|
if ! curl -sf "$BASE/api/health" >/dev/null; then
|
|
echo "Server không phản hồi tại $BASE" >&2
|
|
exit 1
|
|
fi
|
|
|
|
bench_endpoint() {
|
|
local name="$1"
|
|
local method="$2"
|
|
local path="$3"
|
|
local body="${4:-}"
|
|
python3 - "$name" "$method" "$BASE$path" "$REQUESTS" "$WARMUP" "$body" <<'PY'
|
|
import statistics
|
|
import sys
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
name, method, url, n_req, n_warm, body = sys.argv[1:7]
|
|
n_req = int(n_req)
|
|
n_warm = int(n_warm)
|
|
data = body.encode() if body else None
|
|
headers = {"Content-Type": "application/json"} if data else {}
|
|
|
|
def once():
|
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
|
t0 = time.perf_counter()
|
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
|
resp.read()
|
|
return (time.perf_counter() - t0) * 1000.0
|
|
|
|
for _ in range(n_warm):
|
|
try:
|
|
once()
|
|
except Exception:
|
|
pass
|
|
|
|
samples = []
|
|
errors = 0
|
|
for _ in range(n_req):
|
|
try:
|
|
samples.append(once())
|
|
except Exception as exc:
|
|
errors += 1
|
|
|
|
if not samples:
|
|
print(f"{name}: FAIL errors={errors}")
|
|
else:
|
|
samples.sort()
|
|
def pct(p):
|
|
i = max(0, min(len(samples) - 1, int(len(samples) * p / 100.0) - 1))
|
|
return samples[i]
|
|
print(
|
|
f"{name}: ok={len(samples)} err={errors} "
|
|
f"p50={pct(50):.2f}ms p95={pct(95):.2f}ms avg={statistics.mean(samples):.2f}ms max={samples[-1]:.2f}ms"
|
|
)
|
|
PY
|
|
}
|
|
|
|
echo "=== HTTP latency ==="
|
|
bench_endpoint "GET /api/health" GET "/api/health"
|
|
bench_endpoint "GET /api/state" GET "/api/state"
|
|
bench_endpoint "GET /api/missions" GET "/api/missions"
|
|
bench_endpoint "GET /api/mission_queue" GET "/api/mission_queue"
|
|
bench_endpoint "GET /api/v2.0.0/mission_queue" GET "/api/v2.0.0/mission_queue"
|
|
bench_endpoint "GET /" GET "/"
|
|
bench_endpoint "GET /missions.js" GET "/missions.js"
|
|
|
|
MID="$(curl -sf "$BASE/api/missions" | python3 -c "import json,sys; m=json.load(sys.stdin).get('missions',[]); print(m[0]['id'] if m else '')" 2>/dev/null || true)"
|
|
if [[ -n "$MID" ]]; then
|
|
bench_endpoint "POST /api/v2.0.0/mission_queue" POST "/api/v2.0.0/mission_queue" \
|
|
"{\"mission_id\":\"$MID\",\"priority\":0}"
|
|
fi
|
|
|
|
echo
|
|
echo "=== Process lidar_manager_web ==="
|
|
ps -C lidar_manager_web -o pid,rss,vsz,pcpu,pmem,etime,cmd 2>/dev/null \
|
|
|| pgrep -af '[./]lidar_manager_web' | grep -v pgrep || true
|