#!/usr/bin/env bash set -euo pipefail # Performance profiling script # Set default values REPORT_DIR="${REPORT_DIR:-reports/performance}" BINARY_DIR="${BINARY_DIR:-bin}" BINARY_NAME="${BINARY_NAME:-mev-bot}" PROFILE_TYPES="${PROFILE_TYPES:-cpu,mem,block,mutex}" TIMEOUT="${TIMEOUT:-30s}" echo "Starting performance profile..." # Create report directory mkdir -p "$REPORT_DIR" # Build the application if it doesn't exist if [ ! -f "$BINARY_DIR/$BINARY_NAME" ]; then echo "Building application..." make build fi # Run the application with profiling enabled echo "Running application with profiling for $TIMEOUT..." "$BINARY_DIR/$BINARY_NAME" --config config/development.yaml & APP_PID=$! # Wait for the app to start sleep 2 # Profile the application if command -v go &> /dev/null; then # Use go tool pprof to profile the running application echo "Profiling CPU for $TIMEOUT..." go tool pprof -text -top -nodecount=10 "http://localhost:6060/debug/pprof/profile?seconds=${TIMEOUT%?}" > "$REPORT_DIR/cpu-profile.txt" 2>/dev/null || echo "Could not connect to pprof endpoint, make sure your app has debug/pprof enabled" echo "Profiling memory..." go tool pprof -text -top -nodecount=10 "http://localhost:6060/debug/pprof/heap" > "$REPORT_DIR/mem-profile.txt" 2>/dev/null || echo "Could not connect to heap profile endpoint" fi # Alternative: run a benchmark test if the application doesn't support pprof echo "Running benchmark tests..." go test -bench=. -benchmem -run=^$ ./pkg/... ./cmd/... > "$REPORT_DIR/benchmark-results.txt" echo "Performance profiling complete. Reports saved to $REPORT_DIR"