-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.sh
More file actions
131 lines (113 loc) · 6.6 KB
/
commands.sh
File metadata and controls
131 lines (113 loc) · 6.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
#!/bin/bash
# Commands executed during the lab
# ========================================================================
# Environment Setup / Install Required Tools
# ========================================================================
sudo apt update
sudo apt install -y python3 python3-pip python3-venv git
mkdir -p ~/ai-refactor-lab
cd ~/ai-refactor-lab
python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install radon pylint pytest pytest-benchmark black isort aider-chat
radon --version
pylint --version
pytest --version
# ========================================================================
# Task 1: Analyze Legacy Code Complexity / Step 1: Create Legacy Code Sample / Run:
# ========================================================================
nano legacy_calculator.py
# ========================================================================
# Task 1: Analyze Legacy Code Complexity / Step 2: Measure Initial Complexity / Run:
# ========================================================================
nano measure_metrics.py
# ========================================================================
# Task 1: Analyze Legacy Code Complexity / Step 2: Measure Initial Complexity / Run initial analysis
# ========================================================================
echo "=== Cyclomatic Complexity ==="
radon cc legacy_calculator.py -s
echo -e "\n=== Maintainability Index ==="
radon mi legacy_calculator.py -s
echo -e "\n=== Raw Metrics ==="
radon raw legacy_calculator.py
radon cc legacy_calculator.py -j > baseline_complexity.json
radon mi legacy_calculator.py -j > baseline_maintainability.json
python3 measure_metrics.py legacy_calculator.py baseline_metrics_full.json
# ========================================================================
# Task 1: Analyze Legacy Code Complexity / Step 3: Create Performance Benchmark / Run:
# ========================================================================
nano test_performance.py
# ========================================================================
# Task 2: AI-Assisted Refactoring / Step 1: Configure AI Refactoring Tool / Run:
# ========================================================================
nano .aider.conf.yaml
# ========================================================================
# Task 2: AI-Assisted Refactoring / Step 1: Configure AI Refactoring Tool / Initialize git repository for tracking
# ========================================================================
git init
git config user.name "Lab User"
git config user.email "labuser@example.com"
git add legacy_calculator.py
git commit -m "Initial commit: legacy code baseline"
# ========================================================================
# Task 2: AI-Assisted Refactoring / Step 2: Create Refactoring Prompt / Run:
# ========================================================================
nano refactor_instructions.md
# ========================================================================
# Task 2: AI-Assisted Refactoring / Step 3: Perform AI-Assisted Refactoring / Run:
# ========================================================================
nano refactored_calculator.py
# ========================================================================
# Task 3: Measure and Compare Metrics / Step 1: Analyze Refactored Code
# ========================================================================
echo "=== Refactored Code Metrics ==="
radon cc refactored_calculator.py -s
radon mi refactored_calculator.py -s
radon cc refactored_calculator.py -j > refactored_complexity.json
radon mi refactored_calculator.py -j > refactored_maintainability.json
python3 measure_metrics.py refactored_calculator.py refactored_metrics_full.json
# ========================================================================
# Task 3: Measure and Compare Metrics / Step 2: Create Comparison Report / Run:
# ========================================================================
nano compare_metrics.py
# ========================================================================
# Task 3: Measure and Compare Metrics / Step 2: Create Comparison Report / Run the comparison report:
# ========================================================================
python3 compare_metrics.py
# ========================================================================
# Task 3: Measure and Compare Metrics / Step 3: Run Performance Comparison / Run:
# ========================================================================
nano test_refactored_performance.py
# ========================================================================
# Task 3: Measure and Compare Metrics / Step 3: Run Performance Comparison / Run benchmarks
# ========================================================================
pytest test_performance.py --benchmark-only --benchmark-save=baseline_perf --benchmark-json=baseline_perf.json
pytest test_refactored_performance.py --benchmark-only --benchmark-save=refactored_perf --benchmark-json=refactored_perf.json
pytest-benchmark compare --sort=mean \
baseline_run_id=$(find .benchmarks -type f -name '*baseline_perf*.json' -printf '%f\n' | head -n 1 | cut -d'_' -f1)
echo "$baseline_run_id"
pytest test_refactored_performance.py --benchmark-only --benchmark-compare="$baseline_run_id" --benchmark-compare-fail=mean:10%
# ========================================================================
# Verification / Verify Complexity Reduction
# ========================================================================
echo "Checking complexity improvements..."
baseline_cc=$(radon cc legacy_calculator.py -a | grep "Average complexity" | awk '{print $NF}')
refactored_cc=$(radon cc refactored_calculator.py -a | grep "Average complexity" | awk '{print $NF}')
echo "Baseline complexity: $baseline_cc"
echo "Refactored complexity: $refactored_cc"
echo -e "\nMaintainability Index comparison:"
radon mi legacy_calculator.py -s
radon mi refactored_calculator.py -s
# ========================================================================
# Verification / Verify Functional Correctness / Run:
# ========================================================================
nano test_correctness.py
# ========================================================================
# Verification / Verify Functional Correctness / Run correctness tests
# ========================================================================
pytest test_correctness.py -v
# ========================================================================
# Troubleshooting / Issue: Performance degraded
# ========================================================================
python3 -m cProfile -s cumtime refactored_calculator.py