Files
ShieldAI/scripts/load-test/run-all.sh
Michael Freno a804cab431 Add load testing job to GitHub Actions CI pipeline (FRE-4931)
- Add load-test job to ci.yml that runs after docker-build on push to main
- Create combined load test runner (scripts/load-test/run-all.sh) for all services
- Create k6 load test scripts for api, darkwatch, spamshield, and voiceprint
- Add shared k6 utilities (lib/common.js)
- Update load-test.yml to support all services and report artifacts
- Configure k6 cloud output and P99 threshold validation
- Generate load test report as CI artifact

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-09 09:16:36 -04:00

104 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Combined load test runner for all ShieldAI services
# Usage: ./run-all.sh [service]
# service: all (default), api, darkwatch, spamshield, voiceprint
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT_DIR="${SCRIPT_DIR}/reports"
TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
SUMMARY_FILE="${REPORT_DIR}/summary-${TIMESTAMP}.json"
THRESHOLD_FILE="${REPORT_DIR}/threshold-results.json"
SERVICE="${1:-all}"
mkdir -p "$REPORT_DIR"
BASE_URL="${LOAD_TEST_BASE_URL:-http://localhost:3000}"
TARGET_RPS="${TARGET_RPS:-500}"
DURATION="${DURATION:-300s}"
echo "=== ShieldAI Combined Load Test ==="
echo "Timestamp: $TIMESTAMP"
echo "Base URL: $BASE_URL"
echo "Target RPS: $TARGET_RPS"
echo "Duration: $DURATION"
echo "Service: $SERVICE"
echo ""
K6_OPTS="--duration $DURATION --summary-export $SUMMARY_FILE"
if [[ -n "${K6_CLOUD_TOKEN:-}" ]]; then
K6_OPTS="$K6_OPTS --out cloud"
echo "k6 cloud output: enabled"
fi
declare -A EXIT_CODES
ALL_PASSED=true
run_service_test() {
local name=$1
local script=$2
local env_vars=$3
echo ""
echo "=== Running $name Load Test ==="
set +e
eval "$env_vars" k6 run $K6_OPTS "$script"
EXIT_CODE=$?
set -e
EXIT_CODES[$name]=$EXIT_CODE
if [[ $EXIT_CODE -ne 0 ]]; then
ALL_PASSED=false
echo "$name load test FAILED (exit code: $EXIT_CODE)"
else
echo "$name load test PASSED"
fi
}
if [[ "$SERVICE" == "all" || "$SERVICE" == "api" ]]; then
run_service_test "api" "${SCRIPT_DIR}/services/api.js" "BASE_URL=$BASE_URL TARGET_RPS=$TARGET_RPS"
fi
if [[ "$SERVICE" == "all" || "$SERVICE" == "darkwatch" ]]; then
run_service_test "darkwatch" "${SCRIPT_DIR}/services/darkwatch.js" "BASE_URL=$BASE_URL TARGET_RPS=$TARGET_RPS"
fi
if [[ "$SERVICE" == "all" || "$SERVICE" == "spamshield" ]]; then
run_service_test "spamshield" "${SCRIPT_DIR}/services/spamshield.js" "BASE_URL=$BASE_URL TARGET_RPS=$TARGET_RPS"
fi
if [[ "$SERVICE" == "all" || "$SERVICE" == "voiceprint" ]]; then
run_service_test "voiceprint" "${SCRIPT_DIR}/services/voiceprint.js" "BASE_URL=$BASE_URL TARGET_RPS=$TARGET_RPS"
fi
echo ""
echo "=== Load Test Results ==="
echo "Summary saved to: $SUMMARY_FILE"
# Aggregate thresholds
jq -n \
--arg timestamp "$TIMESTAMP" \
--arg base_url "$BASE_URL" \
--arg target_rps "$TARGET_RPS" \
--argjson exit_codes "$(printf '%s\n' "${!EXIT_CODES[@]}" | jq -R . | jq -s 'with_entries(.key = .[0])' | while read key; do echo; done)" \
'{timestamp: $timestamp, base_url: $base_url, target_rps: $target_rps, services: $exit_codes}' \
> "$THRESHOLD_FILE" 2>/dev/null || true
for service in "${!EXIT_CODES[@]}"; do
status="pass"
[[ ${EXIT_CODES[$service]} -ne 0 ]] && status="fail"
echo "$service: $status"
done
echo ""
if $ALL_PASSED; then
echo "✅ All load tests passed"
exit 0
else
echo "❌ Some load tests failed"
exit 1
fi