Repository navigation
Expand file tree
/
Copy pathrun-test.sh
More file actions
438 lines (361 loc) · 12.8 KB
/
Copy pathrun-test.sh
File metadata and controls
438 lines (361 loc) · 12.8 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
#!/bin/bash
# Comprehensive test script for Gathera contracts with gas usage reporting
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if we're in the right directory
if [ ! -d "contract" ]; then
print_error "Contract directory not found. Please run from project root."
exit 1
fi
# Parse command line arguments
RUN_STANDARD_TESTS=true
RUN_GAS_TESTS=true
RUN_BENCHMARKS=true
RUN_LIMIT_TESTS=true
RUN_REGRESSION_TESTS=true
GENERATE_REPORTS=true
TEST_TYPE="all"
while [[ $# -gt 0 ]]; do
case $1 in
--standard-only)
RUN_STANDARD_TESTS=true
RUN_GAS_TESTS=false
RUN_BENCHMARKS=false
RUN_LIMIT_TESTS=false
RUN_REGRESSION_TESTS=false
TEST_TYPE="standard"
shift
;;
--gas-only)
RUN_STANDARD_TESTS=false
RUN_GAS_TESTS=true
RUN_BENCHMARKS=false
RUN_LIMIT_TESTS=false
RUN_REGRESSION_TESTS=false
TEST_TYPE="gas"
shift
;;
--benchmark-only)
RUN_STANDARD_TESTS=false
RUN_GAS_TESTS=false
RUN_BENCHMARKS=true
RUN_LIMIT_TESTS=false
RUN_REGRESSION_TESTS=false
TEST_TYPE="benchmark"
shift
;;
--limit-only)
RUN_STANDARD_TESTS=false
RUN_GAS_TESTS=false
RUN_BENCHMARKS=false
RUN_LIMIT_TESTS=true
RUN_REGRESSION_TESTS=false
TEST_TYPE="limit"
shift
;;
--regression-only)
RUN_STANDARD_TESTS=false
RUN_GAS_TESTS=false
RUN_BENCHMARKS=false
RUN_LIMIT_TESTS=false
RUN_REGRESSION_TESTS=true
TEST_TYPE="regression"
shift
;;
--no-reports)
GENERATE_REPORTS=false
shift
;;
--help|-h)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --standard-only Run only standard contract tests"
echo " --gas-only Run only gas usage tests"
echo " --benchmark-only Run only gas benchmark tests"
echo " --limit-only Run only gas limit scenario tests"
echo " --regression-only Run only gas regression tests"
echo " --no-reports Skip report generation"
echo " --help, -h Show this help message"
echo ""
echo "Default: Run all tests and generate reports"
exit 0
;;
*)
print_error "Unknown option: $1"
echo "Use --help for usage information"
exit 1
;;
esac
done
# Create reports directory
REPORTS_DIR="reports"
GAS_REPORTS_DIR="$REPORTS_DIR/gas"
mkdir -p "$GAS_REPORTS_DIR"
# Function to run standard tests
run_standard_tests() {
print_status "Running standard contract tests..."
cd contract
# Test ticket contract
print_status "Testing ticket contract..."
if cargo test --package ticket_contract 2>&1 | tee "$GAS_REPORTS_DIR/ticket_standard.log"; then
print_success "Ticket contract tests passed"
else
print_error "Ticket contract tests failed"
return 1
fi
# Test escrow contract
print_status "Testing escrow contract..."
if cargo test --package escrow_contract 2>&1 | tee "$GAS_REPORTS_DIR/escrow_standard.log"; then
print_success "Escrow contract tests passed"
else
print_error "Escrow contract tests failed"
return 1
fi
cd ..
}
# Function to run gas usage tests
run_gas_tests() {
print_status "Running gas usage tests..."
cd contract
# Test ticket contract gas usage
print_status "Testing ticket contract gas usage..."
if cargo test --package ticket_contract test_gas 2>&1 | tee "$GAS_REPORTS_DIR/ticket_gas.log"; then
print_success "Ticket contract gas tests passed"
else
print_error "Ticket contract gas tests failed"
return 1
fi
# Test escrow contract gas usage
print_status "Testing escrow contract gas usage..."
if cargo test --package escrow_contract test_gas 2>&1 | tee "$GAS_REPORTS_DIR/escrow_gas.log"; then
print_success "Escrow contract gas tests passed"
else
print_error "Escrow contract gas tests failed"
return 1
fi
cd ..
}
# Function to run gas benchmarks
run_gas_benchmarks() {
print_status "Running gas benchmark tests..."
cd contract
# Run comprehensive benchmarks
print_status "Running comprehensive gas benchmarks..."
if cargo test --package test gas_benchmarks 2>&1 | tee "$GAS_REPORTS_DIR/benchmarks.log"; then
print_success "Gas benchmarks completed"
else
print_error "Gas benchmarks failed"
return 1
fi
cd ..
}
# Function to run gas limit tests
run_gas_limit_tests() {
print_status "Running gas limit scenario tests..."
cd contract
# Run limit scenario tests
print_status "Testing gas limit scenarios..."
if cargo test --package test gas_limits 2>&1 | tee "$GAS_REPORTS_DIR/limits.log"; then
print_success "Gas limit tests completed"
else
print_error "Gas limit tests failed"
return 1
fi
cd ..
}
# Function to run gas regression tests
run_gas_regression_tests() {
print_status "Running gas regression tests..."
cd contract
# Run regression monitoring tests
print_status "Testing for gas regressions..."
if cargo test --package test gas_regression 2>&1 | tee "$GAS_REPORTS_DIR/regression.log"; then
print_success "Gas regression tests completed"
else
print_error "Gas regression tests failed"
return 1
fi
cd ..
}
# Function to generate comprehensive reports
generate_reports() {
if [ "$GENERATE_REPORTS" = false ]; then
return
fi
print_status "Generating comprehensive gas usage reports..."
local timestamp=$(date +"%Y%m%d_%H%M%S")
local summary_file="$GAS_REPORTS_DIR/gas_summary_$timestamp.md"
cat > "$summary_file" << EOF
# Gas Usage Test Report
**Generated:** $(date)
**Test Type:** $TEST_TYPE
**Environment:** $(uname -a)
## Test Results Summary
EOF
# Add test results to summary
if [ "$RUN_STANDARD_TESTS" = true ]; then
echo "### Standard Tests" >> "$summary_file"
if [ -f "$GAS_REPORTS_DIR/ticket_standard.log" ]; then
echo "- Ticket Contract: $(grep "test result" "$GAS_REPORTS_DIR/ticket_standard.log" | tail -1)" >> "$summary_file"
fi
if [ -f "$GAS_REPORTS_DIR/escrow_standard.log" ]; then
echo "- Escrow Contract: $(grep "test result" "$GAS_REPORTS_DIR/escrow_standard.log" | tail -1)" >> "$summary_file"
fi
echo "" >> "$summary_file"
fi
if [ "$RUN_GAS_TESTS" = true ]; then
echo "### Gas Usage Tests" >> "$summary_file"
if [ -f "$GAS_REPORTS_DIR/ticket_gas.log" ]; then
echo "- Ticket Contract Gas Tests: $(grep "test result" "$GAS_REPORTS_DIR/ticket_gas.log" | tail -1)" >> "$summary_file"
fi
if [ -f "$GAS_REPORTS_DIR/escrow_gas.log" ]; then
echo "- Escrow Contract Gas Tests: $(grep "test result" "$GAS_REPORTS_DIR/escrow_gas.log" | tail -1)" >> "$summary_file"
fi
echo "" >> "$summary_file"
fi
if [ "$RUN_BENCHMARKS" = true ]; then
echo "### Gas Benchmarks" >> "$summary_file"
if [ -f "$GAS_REPORTS_DIR/benchmarks.log" ]; then
echo "- Comprehensive Benchmarks: $(grep "test result" "$GAS_REPORTS_DIR/benchmarks.log" | tail -1)" >> "$summary_file"
fi
echo "" >> "$summary_file"
fi
if [ "$RUN_LIMIT_TESTS" = true ]; then
echo "### Gas Limit Tests" >> "$summary_file"
if [ -f "$GAS_REPORTS_DIR/limits.log" ]; then
echo "- Limit Scenario Tests: $(grep "test result" "$GAS_REPORTS_DIR/limits.log" | tail -1)" >> "$summary_file"
fi
echo "" >> "$summary_file"
fi
if [ "$RUN_REGRESSION_TESTS" = true ]; then
echo "### Gas Regression Tests" >> "$summary_file"
if [ -f "$GAS_REPORTS_DIR/regression.log" ]; then
echo "- Regression Monitoring: $(grep "test result" "$GAS_REPORTS_DIR/regression.log" | tail -1)" >> "$summary_file"
fi
echo "" >> "$summary_file"
fi
# Add gas usage analysis
cat >> "$summary_file" << EOF
## Gas Usage Analysis
### Key Metrics Extracted from Logs
EOF
# Extract gas usage information from logs
for log_file in "$GAS_REPORTS_DIR"/*.log; do
if [ -f "$log_file" ]; then
echo "#### $(basename "$log_file" .log)" >> "$summary_file"
echo '```' >> "$summary_file"
# Extract gas-related lines
grep -i "gas\|benchmark\|regression\|limit" "$log_file" | head -20 >> "$summary_file" 2>/dev/null || echo "No gas metrics found" >> "$summary_file"
echo '```' >> "$summary_file"
echo "" >> "$summary_file"
fi
done
# Add recommendations
cat >> "$summary_file" << EOF
## Recommendations
### Gas Optimization Opportunities
Based on the test results, consider the following optimization strategies:
1. **Batch Operations**: Ensure batch operations are more gas-efficient than individual operations
2. **Storage Optimization**: Review storage patterns for potential gas savings
3. **Computation Efficiency**: Identify computationally intensive operations that can be optimized
4. **Contract Size**: Monitor contract size and consider modular patterns if needed
### Monitoring Guidelines
1. **Continuous Monitoring**: Set up automated gas regression monitoring in CI/CD
2. **Baseline Updates**: Update gas baselines when intentional optimizations are made
3. **Alert Thresholds**: Configure appropriate alert thresholds for different operation types
4. **Regular Benchmarks**: Run comprehensive benchmarks regularly to track trends
## Files Generated
- Standard test logs: \`ticket_standard.log\`, \`escrow_standard.log\`
- Gas test logs: \`ticket_gas.log\`, \`escrow_gas.log\`
- Benchmark results: \`benchmarks.log\`
- Limit test results: \`limits.log\`
- Regression test results: \`regression.log\`
## Next Steps
1. Review any failed tests and address issues
2. Analyze gas usage patterns for optimization opportunities
3. Update baselines if significant improvements are made
4. Configure automated monitoring for production deployments
EOF
print_success "Comprehensive report generated: $summary_file"
# Generate a quick summary for console output
echo ""
print_status "Quick Test Summary:"
echo "======================"
if [ -f "$summary_file" ]; then
grep -A 20 "### " "$summary_file" | head -20
fi
echo ""
print_status "Full report available at: $summary_file"
}
# Main execution
main() {
print_status "Starting Gathera contract test suite..."
print_status "Test configuration: $TEST_TYPE"
local start_time=$(date +%s)
local failed_tests=0
# Run tests based on configuration
if [ "$RUN_STANDARD_TESTS" = true ]; then
if ! run_standard_tests; then
((failed_tests++))
fi
fi
if [ "$RUN_GAS_TESTS" = true ]; then
if ! run_gas_tests; then
((failed_tests++))
fi
fi
if [ "$RUN_BENCHMARKS" = true ]; then
if ! run_gas_benchmarks; then
((failed_tests++))
fi
fi
if [ "$RUN_LIMIT_TESTS" = true ]; then
if ! run_gas_limit_tests; then
((failed_tests++))
fi
fi
if [ "$RUN_REGRESSION_TESTS" = true ]; then
if ! run_gas_regression_tests; then
((failed_tests++))
fi
fi
# Generate reports
generate_reports
local end_time=$(date +%s)
local duration=$((end_time - start_time))
# Final summary
echo ""
echo "=================================="
if [ $failed_tests -eq 0 ]; then
print_success "All tests completed successfully!"
print_success "Total execution time: ${duration}s"
else
print_error "$failed_tests test suites failed!"
print_warning "Total execution time: ${duration}s"
echo ""
print_status "Check the log files in $GAS_REPORTS_DIR for details"
fi
echo "=================================="
exit $failed_tests
}
# Run main function
main "$@"