This repository was archived by the owner on Jun 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance-regression-test.sh
More file actions
executable file
·215 lines (175 loc) · 6.46 KB
/
Copy pathperformance-regression-test.sh
File metadata and controls
executable file
·215 lines (175 loc) · 6.46 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/bin/bash
# SPDX-FileCopyrightText: 2026 Github
# SPDX-FileCopyrightText: 2026 Knitli Inc.
#
# SPDX-License-Identifier: MIT
# SPDX-License-Identifier: MIT OR Apache-2.0
# Performance Regression Detection Script
# Compares current deployment performance against baseline
set -e
DEPLOYMENT_ID="${1:-unknown}"
BASELINE_FILE="${2:-baseline.json}"
DURATION="${3:-300}" # Default 5-minute test
ENDPOINT="${THREAD_ENDPOINT:-https://api.thread.io}"
# Thresholds
WARNING_THRESHOLD=25 # 25% degradation
CRITICAL_THRESHOLD=50 # 50% degradation
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
success() {
echo -e "${GREEN}✓${NC} $*"
}
warn() {
echo -e "${YELLOW}⚠${NC} $*"
}
fail() {
echo -e "${RED}✗${NC} $*"
}
alert_slack() {
if [[ -n "$SLACK_WEBHOOK_URL" ]]; then
curl -s -X POST "$SLACK_WEBHOOK_URL" \
-H 'Content-Type: application/json' \
-d "{\"text\":\"$1\"}" >/dev/null 2>&1
fi
}
# Run k6 load test
run_load_test() {
log "Running ${DURATION}s load test against $ENDPOINT"
k6 run --quiet --out json=results.json --duration "${DURATION}s" - <<EOF
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '30s', target: 50 },
{ duration: '${DURATION - 60}s', target: 100 },
{ duration: '30s', target: 0 },
],
};
export default function() {
const response = http.post('${ENDPOINT}/api/query', JSON.stringify({
pattern: 'function \$NAME() {}',
language: 'javascript'
}), {
headers: { 'Content-Type': 'application/json' },
});
check(response, {
'status is 200': (r) => r.status === 200,
});
sleep(0.1);
}
EOF
# Convert k6 JSON output to summary
jq -s '[.[] | select(.type=="Point")] |
group_by(.metric) |
map({metric: .[0].metric,
values: ([.[] | .data.value] |
{p50: (sort | .[length/2 | floor]),
p95: (sort | .[length * 0.95 | floor]),
p99: (sort | .[length * 0.99 | floor]),
avg: (add / length)})})' \
results.json > summary.json
log "Load test completed"
}
# Compare results with baseline
compare_results() {
local baseline="$1"
local current="summary.json"
if [[ ! -f "$baseline" ]]; then
log "No baseline found - this will become the new baseline"
cp "$current" "$baseline"
success "Baseline created: $baseline"
return 0
fi
log "Comparing performance with baseline..."
# Extract P95 latency
baseline_p95=$(jq -r '.[] | select(.metric=="http_req_duration") | .values.p95' "$baseline" 2>/dev/null || echo "0")
current_p95=$(jq -r '.[] | select(.metric=="http_req_duration") | .values.p95' "$current" 2>/dev/null || echo "0")
# Extract P99 latency
baseline_p99=$(jq -r '.[] | select(.metric=="http_req_duration") | .values.p99' "$baseline" 2>/dev/null || echo "0")
current_p99=$(jq -r '.[] | select(.metric=="http_req_duration") | .values.p99' "$current" 2>/dev/null || echo "0")
# Calculate deviations
p95_deviation=$(echo "scale=2; ($current_p95 - $baseline_p95) / $baseline_p95 * 100" | bc 2>/dev/null || echo "0")
p99_deviation=$(echo "scale=2; ($current_p99 - $baseline_p99) / $baseline_p99 * 100" | bc 2>/dev/null || echo "0")
echo ""
echo "========================================="
echo "Performance Comparison Results"
echo "========================================="
echo "Deployment ID: $DEPLOYMENT_ID"
echo ""
echo "P95 Latency:"
echo " Baseline: ${baseline_p95}ms"
echo " Current: ${current_p95}ms"
echo " Deviation: ${p95_deviation}%"
echo ""
echo "P99 Latency:"
echo " Baseline: ${baseline_p99}ms"
echo " Current: ${current_p99}ms"
echo " Deviation: ${p99_deviation}%"
echo ""
# Evaluate regression
regression_level="none"
exit_code=0
if (( $(echo "$p95_deviation > $CRITICAL_THRESHOLD" | bc -l 2>/dev/null || echo 0) )); then
regression_level="critical"
fail "CRITICAL: P95 latency regression > ${CRITICAL_THRESHOLD}%"
alert_slack "🚨 CRITICAL Performance Regression: P95 latency +${p95_deviation}% on deployment $DEPLOYMENT_ID"
exit_code=2
elif (( $(echo "$p95_deviation > $WARNING_THRESHOLD" | bc -l 2>/dev/null || echo 0) )); then
regression_level="warning"
warn "WARNING: P95 latency regression > ${WARNING_THRESHOLD}%"
alert_slack "⚠️ WARNING: Performance Regression: P95 latency +${p95_deviation}% on deployment $DEPLOYMENT_ID"
exit_code=1
else
success "P95 latency within acceptable range"
fi
if (( $(echo "$p99_deviation > $CRITICAL_THRESHOLD" | bc -l 2>/dev/null || echo 0) )); then
regression_level="critical"
fail "CRITICAL: P99 latency regression > ${CRITICAL_THRESHOLD}%"
alert_slack "🚨 CRITICAL Performance Regression: P99 latency +${p99_deviation}% on deployment $DEPLOYMENT_ID"
exit_code=2
elif (( $(echo "$p99_deviation > $WARNING_THRESHOLD" | bc -l 2>/dev/null || echo 0) )); then
if [[ "$regression_level" != "critical" ]]; then
regression_level="warning"
warn "WARNING: P99 latency regression > ${WARNING_THRESHOLD}%"
exit_code=1
fi
else
success "P99 latency within acceptable range"
fi
echo ""
echo "Regression Level: $regression_level"
echo "========================================="
# Output for CI/CD integration
echo "p95_deviation=$p95_deviation" >> "$GITHUB_OUTPUT" 2>/dev/null || true
echo "p99_deviation=$p99_deviation" >> "$GITHUB_OUTPUT" 2>/dev/null || true
echo "regression_level=$regression_level" >> "$GITHUB_OUTPUT" 2>/dev/null || true
return $exit_code
}
# Main execution
main() {
log "Performance Regression Test - Deployment: $DEPLOYMENT_ID"
# Check dependencies
if ! command -v k6 &> /dev/null; then
fail "k6 not installed. Install from: https://k6.io/docs/getting-started/installation/"
exit 1
fi
if ! command -v jq &> /dev/null; then
fail "jq not installed. Install from package manager."
exit 1
fi
# Run load test
run_load_test
# Compare with baseline
compare_results "$BASELINE_FILE"
exit_code=$?
# Cleanup
rm -f results.json summary.json
exit $exit_code
}
main