File size: 3,075 Bytes
1244914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash

# Performance test script for forge commands
# Runs the command 10 times and collects timing statistics
# Usage: ./benchmark.sh [--threshold MS] [command args...]
# Example: ./benchmark.sh info
# Example: ./benchmark.sh --threshold 50 zsh rprompt
# Example: ./benchmark.sh --version

set -euo pipefail

# Colors and styling
BOLD='\033[1m'
DIM='\033[2m'
RESET='\033[0m'
GREEN='\033[32m'
YELLOW='\033[33m'
CYAN='\033[36m'
GRAY='\033[90m'

# Configuration
BASE_COMMAND="target/debug/forge"
THRESHOLD=""

# Parse arguments
ARGS=()
while [[ $# -gt 0 ]]; do
    case $1 in
        --threshold)
            THRESHOLD="$2"
            shift 2
            ;;
        *)
            ARGS+=("$1")
            shift
            ;;
    esac
done

# Build command
if [ ${#ARGS[@]} -gt 0 ]; then
    COMMAND="$BASE_COMMAND ${ARGS[*]}"
else
    COMMAND="$BASE_COMMAND"
fi
ITERATIONS=10
TIMES=()

# Extract command name for display
DISPLAY_CMD=$(echo "$COMMAND" | sed "s|target/debug/||")

# Header
echo ""
echo -e "πŸš€ ${BOLD}Performance Test${RESET} ${DIM}β€”${RESET} ${CYAN}${DISPLAY_CMD}${RESET}"
echo ""

# Build step
echo -e "${GRAY}πŸ“¦ Building...${RESET}"
cargo build 2>&1 | grep -E "Compiling|Finished" | tail -1
echo ""

# Show sample output
echo -e "${GRAY}πŸ“‹ Sample output:${RESET}"
echo ""
$COMMAND
echo ""

# Run performance tests
echo -e "${GRAY}⏱️  Running ${YELLOW}$ITERATIONS${GRAY} iterations...${RESET}"
echo ""

for i in $(seq 1 $ITERATIONS); do
    # Measure execution time
    START=$(date +%s%N)
    $COMMAND > /dev/null 2>&1
    END=$(date +%s%N)
    
    # Calculate duration in milliseconds
    DURATION=$(( (END - START) / 1000000 ))
    TIMES+=($DURATION)
    
    # Color code based on performance
    if [ $DURATION -lt 50 ]; then
        COLOR=$GREEN
    elif [ $DURATION -lt 100 ]; then
        COLOR=$YELLOW
    else
        COLOR=$GRAY
    fi
    
    printf "  ${DIM}%2d${RESET}  ${COLOR}%5d${RESET} ${DIM}ms${RESET}\n" $i $DURATION
done

echo ""

# Calculate statistics
TOTAL=0
MIN=${TIMES[0]}
MAX=${TIMES[0]}

for time in "${TIMES[@]}"; do
    TOTAL=$((TOTAL + time))
    if [ $time -lt $MIN ]; then
        MIN=$time
    fi
    if [ $time -gt $MAX ]; then
        MAX=$time
    fi
done

AVG=$((TOTAL / ITERATIONS))

# Results summary
echo -e "πŸ“Š ${BOLD}Summary${RESET}"
echo ""
printf "  ${DIM}avg${RESET}  ${CYAN}%5d${RESET} ${DIM}ms${RESET}\n" $AVG
printf "  ${DIM}min${RESET}  ${GREEN}%5d${RESET} ${DIM}ms${RESET}\n" $MIN
printf "  ${DIM}max${RESET}  ${YELLOW}%5d${RESET} ${DIM}ms${RESET}\n" $MAX
echo ""

# Check threshold if provided
if [ -n "$THRESHOLD" ]; then
    if [ $AVG -gt $THRESHOLD ]; then
        echo -e "❌ ${BOLD}Performance regression detected!${RESET}"
        echo -e "   Average time ${CYAN}${AVG}ms${RESET} exceeds threshold ${YELLOW}${THRESHOLD}ms${RESET}"
        echo ""
        exit 1
    else
        echo -e "βœ… ${BOLD}Performance check passed!${RESET}"
        echo -e "   Average time ${CYAN}${AVG}ms${RESET} is within threshold ${YELLOW}${THRESHOLD}ms${RESET}"
        echo ""
    fi
fi