#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" BASE_URL="${BASE_URL:-http://localhost:8080}" HEALTH_THRESHOLD_MS="${HEALTH_THRESHOLD_MS:-100}" LOGIN_THRESHOLD_MS="${LOGIN_THRESHOLD_MS:-2000}" cleanup() { true } trap cleanup EXIT cd "$PROJECT_DIR" echo "=== Performance Tests ===" echo "Target: $BASE_URL" echo "" PASS=0 FAIL=0 run_latency_test() { local name="$1" local method="$2" local url="$3" local threshold_ms="$4" local data="${5:-}" local iterations="${6:-10}" local total_ms=0 local max_ms=0 for i in $(seq 1 "$iterations"); do if [[ -n "$data" ]]; then local time_ms time_ms=$(curl -s -o /dev/null -w "%{time_total}" -X "$method" "$url" \ -H "Content-Type: application/json" -d "$data" | awk '{printf "%.0f", $1 * 1000}') else local time_ms time_ms=$(curl -s -o /dev/null -w "%{time_total}" -X "$method" "$url" | awk '{printf "%.0f", $1 * 1000}') fi total_ms=$((total_ms + time_ms)) if (( time_ms > max_ms )); then max_ms=$time_ms fi done local avg_ms=$((total_ms / iterations)) if (( max_ms <= threshold_ms )); then echo "PASS: $name — avg=${avg_ms}ms, max=${max_ms}ms (threshold: ${threshold_ms}ms)" PASS=$((PASS + 1)) else echo "FAIL: $name — avg=${avg_ms}ms, max=${max_ms}ms (threshold: ${threshold_ms}ms)" FAIL=$((FAIL + 1)) fi } run_latency_test "NFT-PERF-01: Health endpoint" "GET" "$BASE_URL/health" "$HEALTH_THRESHOLD_MS" "" 100 echo "" echo "=== Results: $PASS passed, $FAIL failed ===" if (( FAIL > 0 )); then exit 1 fi exit 0