Repository navigation
Expand file tree
/
Copy pathrun_all_tests.sh
More file actions
executable file
Β·514 lines (465 loc) Β· 14.6 KB
/
Copy pathrun_all_tests.sh
File metadata and controls
executable file
Β·514 lines (465 loc) Β· 14.6 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#!/bin/bash
#
# TFE Load Testing Framework - Run All Tests
#
# This script runs all load test scenarios sequentially and generates
# comprehensive analysis reports for each test.
#
# Usage:
# ./run_all_tests.sh [options]
#
# Options:
# --max-concurrent-runs NUM Maximum concurrent runs (default: 20, auto-calculates users)
# --users NUM Override automatic user calculation (optional)
# --spawn-rate NUM User spawn rate per second (default: 2)
# --run-time TIME Test duration (default: 5m)
# --host URL TFE hostname (default: https://tfe.localdemo.me)
# --with-grafana Include Grafana metrics in analysis
# --grafana-url URL Grafana URL (default: http://localhost:3000)
# --skip-workspace Skip workspace operations test
# --skip-run Skip run operations test
# --skip-state Skip state operations test
# --ci-mode CI/CD mode (fail fast on first failure)
# --help Show this help message
#
# Environment Variables:
# MAX_CONCURRENT_RUNS Can also be set via environment (default: 20)
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Default configuration
MAX_CONCURRENT_RUNS=${MAX_CONCURRENT_RUNS:-20}
USERS="" # Will be auto-calculated if not set
SPAWN_RATE="" # Will be auto-calculated if not set
RUN_TIME="5m"
HOST="https://tfe.localdemo.me"
WITH_GRAFANA=false
GRAFANA_URL="http://localhost:3000"
SKIP_WORKSPACE=false
SKIP_RUN=false
SKIP_STATE=false
CI_MODE=false
# Parse command-line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--max-concurrent-runs)
MAX_CONCURRENT_RUNS="$2"
shift 2
;;
--users)
USERS="$2"
shift 2
;;
--spawn-rate)
SPAWN_RATE="$2"
shift 2
;;
--run-time)
RUN_TIME="$2"
shift 2
;;
--host)
HOST="$2"
shift 2
;;
--with-grafana)
WITH_GRAFANA=true
shift
;;
--grafana-url)
GRAFANA_URL="$2"
shift 2
;;
--skip-workspace)
SKIP_WORKSPACE=true
shift
;;
--skip-run)
SKIP_RUN=true
shift
;;
--skip-state)
SKIP_STATE=true
shift
;;
--ci-mode)
CI_MODE=true
shift
;;
--help)
grep '^#' "$0" | grep -v '#!/bin/bash' | sed 's/^# //'
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Validate environment
echo -e "${CYAN}=================================${NC}"
echo -e "${CYAN}TFE Load Testing Framework${NC}"
echo -e "${CYAN}=================================${NC}"
echo ""
# Check if virtual environment is activated
if [[ -z "$VIRTUAL_ENV" ]]; then
echo -e "${YELLOW}β οΈ Virtual environment not activated. Activating...${NC}"
if [[ -f "venv/bin/activate" ]]; then
source venv/bin/activate
else
echo -e "${RED}β Virtual environment not found. Run: python3 -m venv venv && source venv/bin/activate && pip install -r requirements.txt${NC}"
exit 1
fi
fi
# Load environment variables from .env if it exists
if [[ -f ".env" ]]; then
echo -e "${BLUE}π Loading environment from .env...${NC}"
set -a
source .env
set +a
# Override HOST with TFE_HOSTNAME from .env if set
if [[ -n "$TFE_HOSTNAME" ]]; then
HOST="https://$TFE_HOSTNAME"
fi
else
echo -e "${YELLOW}β οΈ No .env file found. Using environment variables from shell.${NC}"
fi
# Check required environment variables
echo -e "${BLUE}π Checking required environment variables...${NC}"
MISSING_VARS=()
if [[ -z "$TFE_TOKEN" ]]; then
MISSING_VARS+=("TFE_TOKEN")
fi
if [[ -z "$TFE_ORGANIZATION" ]]; then
MISSING_VARS+=("TFE_ORGANIZATION")
fi
if [[ ${#MISSING_VARS[@]} -gt 0 ]]; then
echo -e "${RED}β Missing required environment variables:${NC}"
for var in "${MISSING_VARS[@]}"; do
echo " - $var"
done
echo ""
echo -e "${YELLOW}Please set the required variables:${NC}"
echo " export TFE_TOKEN='your-tfe-api-token'"
echo " export TFE_ORGANIZATION='your-org-name'"
echo ""
echo -e "${YELLOW}Optional variables:${NC}"
echo " export TFE_HOSTNAME='tfe.localdemo.me' # Default: tfe.localdemo.me"
echo " export TFE_VERIFY_SSL='false' # Default: true"
echo ""
exit 1
fi
echo -e "${GREEN}β
All required environment variables are set${NC}"
echo ""
# Auto-calculate users if not explicitly set
if [[ -z "$USERS" ]]; then
USERS=$((MAX_CONCURRENT_RUNS > 5 ? MAX_CONCURRENT_RUNS : 5))
echo -e "${GREEN}π§ Auto-calculated users: $USERS (based on max_concurrent_runs=$MAX_CONCURRENT_RUNS)${NC}"
fi
# Auto-calculate spawn_rate if not explicitly set
if [[ -z "$SPAWN_RATE" ]]; then
SPAWN_RATE=$MAX_CONCURRENT_RUNS
echo -e "${GREEN}π§ Auto-calculated spawn_rate: $SPAWN_RATE/s (instant spawn for max_concurrent_runs=$MAX_CONCURRENT_RUNS)${NC}"
fi
# Create reports directory
mkdir -p reports
# Generate timestamp for this test run
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
REPORT_DIR="reports/run_${TIMESTAMP}"
mkdir -p "$REPORT_DIR"
echo -e "${BLUE}π Test Configuration${NC}"
echo " Max Concurrent Runs: $MAX_CONCURRENT_RUNS"
echo " Users: $USERS"
echo " Spawn Rate: $SPAWN_RATE/s"
echo " Run Time: $RUN_TIME"
echo " Host: $HOST"
echo " Grafana: $WITH_GRAFANA"
echo " Report Directory: $REPORT_DIR"
echo ""
# Track test results
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
FAILED_TESTS=()
# Function to run a single test
run_test() {
local test_name=$1
local locustfile=$2
local description=$3
echo -e "${CYAN}=================================${NC}"
echo -e "${CYAN}Running: $description${NC}"
echo -e "${CYAN}=================================${NC}"
TESTS_RUN=$((TESTS_RUN + 1))
# Run Locust test
echo -e "${BLUE}π Starting load test...${NC}"
if MAX_CONCURRENT_RUNS="$MAX_CONCURRENT_RUNS" locust -f "$locustfile" \
--headless \
--users "$USERS" \
--spawn-rate "$SPAWN_RATE" \
--run-time "$RUN_TIME" \
--host "$HOST" \
--csv "$REPORT_DIR/${test_name}" \
--html "$REPORT_DIR/${test_name}_locust.html" \
--exit-code-on-error 0; then
echo -e "${GREEN}β
Load test completed${NC}"
else
echo -e "${RED}β Load test failed${NC}"
TESTS_FAILED=$((TESTS_FAILED + 1))
FAILED_TESTS+=("$description (load test)")
if [[ "$CI_MODE" == "true" ]]; then
echo -e "${RED}CI mode: Stopping on first failure${NC}"
exit 1
fi
return 1
fi
# Analyze results
echo ""
echo -e "${BLUE}π Analyzing results...${NC}"
local analysis_args=(
"--locust-stats" "$REPORT_DIR/${test_name}_stats.csv"
"--html-report" "$REPORT_DIR/${test_name}_analysis.html"
"--users" "$USERS"
"--spawn-rate" "$SPAWN_RATE"
"--run-time" "$RUN_TIME"
"--host" "$HOST"
)
if [[ "$WITH_GRAFANA" == "true" ]]; then
analysis_args+=("--grafana-url" "$GRAFANA_URL")
else
analysis_args+=("--no-grafana")
fi
# Always use CI mode for proper exit codes
analysis_args+=("--ci-mode")
if python analyze_results.py "${analysis_args[@]}"; then
echo -e "${GREEN}β
Analysis passed${NC}"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}β Analysis failed (metrics exceeded thresholds)${NC}"
TESTS_FAILED=$((TESTS_FAILED + 1))
FAILED_TESTS+=("$description (analysis)")
if [[ "$CI_MODE" == "true" ]]; then
echo -e "${RED}CI mode: Stopping on first failure${NC}"
exit 1
fi
fi
echo ""
echo -e "${GREEN}Reports generated:${NC}"
echo " - Locust HTML: $REPORT_DIR/${test_name}_locust.html"
echo " - Analysis HTML: $REPORT_DIR/${test_name}_analysis.html"
echo " - CSV Stats: $REPORT_DIR/${test_name}_stats.csv"
echo ""
}
# Run tests
START_TIME=$(date +%s)
if [[ "$SKIP_WORKSPACE" == "false" ]]; then
run_test "workspace_operations" \
"src/locustfiles/workspace_operations.py" \
"Workspace Operations Test"
fi
if [[ "$SKIP_RUN" == "false" ]]; then
run_test "run_operations" \
"src/locustfiles/run_operations.py" \
"Run Operations Test"
fi
if [[ "$SKIP_STATE" == "false" ]]; then
run_test "state_operations" \
"src/locustfiles/state_operations.py" \
"State Operations Test"
fi
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
# Generate summary report
echo -e "${CYAN}=================================${NC}"
echo -e "${CYAN}Test Run Summary${NC}"
echo -e "${CYAN}=================================${NC}"
echo ""
echo -e "${BLUE}π Results${NC}"
echo " Tests Run: $TESTS_RUN"
echo -e " Tests Passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e " Tests Failed: ${RED}$TESTS_FAILED${NC}"
echo " Duration: ${DURATION}s"
echo ""
if [[ ${#FAILED_TESTS[@]} -gt 0 ]]; then
echo -e "${RED}β Failed Tests:${NC}"
for test in "${FAILED_TESTS[@]}"; do
echo " - $test"
done
echo ""
fi
echo -e "${BLUE}π All Reports Location${NC}"
echo " $REPORT_DIR/"
echo ""
# Create index.html for easy navigation
cat > "$REPORT_DIR/index.html" <<EOF
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TFE Load Test Results - $TIMESTAMP</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
line-height: 1.6;
color: #333;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: #f5f5f5;
}
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 40px;
border-radius: 8px;
margin-bottom: 30px;
}
.summary {
background: white;
padding: 30px;
border-radius: 8px;
margin-bottom: 30px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.test-card {
background: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.test-card h3 {
margin-top: 0;
color: #667eea;
}
.links {
display: flex;
gap: 10px;
margin-top: 15px;
}
.btn {
display: inline-block;
padding: 10px 20px;
background: #667eea;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
}
.btn:hover {
background: #5568d3;
}
.btn-secondary {
background: #6b7280;
}
.btn-secondary:hover {
background: #4b5563;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 20px;
margin-top: 20px;
}
.stat {
text-align: center;
}
.stat-value {
font-size: 2em;
font-weight: bold;
color: #667eea;
}
.stat-label {
color: #6b7280;
font-size: 0.9em;
}
.passed { color: #10b981; }
.failed { color: #ef4444; }
</style>
</head>
<body>
<div class="header">
<h1>π§ͺ TFE Load Test Results</h1>
<p>Test Run: $TIMESTAMP</p>
<p>Duration: ${DURATION}s</p>
</div>
<div class="summary">
<h2>π Summary</h2>
<div class="stats">
<div class="stat">
<div class="stat-value">$TESTS_RUN</div>
<div class="stat-label">Tests Run</div>
</div>
<div class="stat">
<div class="stat-value passed">$TESTS_PASSED</div>
<div class="stat-label">Passed</div>
</div>
<div class="stat">
<div class="stat-value failed">$TESTS_FAILED</div>
<div class="stat-label">Failed</div>
</div>
</div>
</div>
EOF
# Add test cards
if [[ "$SKIP_WORKSPACE" == "false" ]]; then
cat >> "$REPORT_DIR/index.html" <<EOF
<div class="test-card">
<h3>π’ Workspace Operations Test</h3>
<p>Tests workspace lifecycle operations: create, list, get, delete</p>
<div class="links">
<a href="workspace_operations_analysis.html" class="btn">π Analysis Report</a>
<a href="workspace_operations_locust.html" class="btn btn-secondary">π Locust Report</a>
</div>
</div>
EOF
fi
if [[ "$SKIP_RUN" == "false" ]]; then
cat >> "$REPORT_DIR/index.html" <<EOF
<div class="test-card">
<h3>π Run Operations Test</h3>
<p>Tests Terraform run lifecycle and queue management</p>
<div class="links">
<a href="run_operations_analysis.html" class="btn">π Analysis Report</a>
<a href="run_operations_locust.html" class="btn btn-secondary">π Locust Report</a>
</div>
</div>
EOF
fi
if [[ "$SKIP_STATE" == "false" ]]; then
cat >> "$REPORT_DIR/index.html" <<EOF
<div class="test-card">
<h3>πΎ State Operations Test</h3>
<p>Tests state management and file handling operations</p>
<div class="links">
<a href="state_operations_analysis.html" class="btn">π Analysis Report</a>
<a href="state_operations_locust.html" class="btn btn-secondary">π Locust Report</a>
</div>
</div>
EOF
fi
cat >> "$REPORT_DIR/index.html" <<EOF
</body>
</html>
EOF
echo -e "${GREEN}β
Index page created: $REPORT_DIR/index.html${NC}"
echo ""
# Open index page if not in CI mode
if [[ "$CI_MODE" == "false" ]] && command -v open &> /dev/null; then
echo -e "${BLUE}Opening results in browser...${NC}"
open "$REPORT_DIR/index.html"
fi
# Exit with appropriate code
if [[ $TESTS_FAILED -gt 0 ]]; then
echo -e "${RED}β Some tests failed${NC}"
exit 1
else
echo -e "${GREEN}β
All tests passed!${NC}"
exit 0
fi