File size: 9,197 Bytes
4b28fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Performance benchmarking for search optimizer refactoring.

This script measures the performance impact of the refactoring to ensure
there's no significant performance degradation.
"""

import time
import sys
import statistics
from typing import List, Dict, Any

def benchmark_function(func, *args, iterations=100):
    """Benchmark a function by running it multiple times and measuring performance"""
    
    times = []
    
    # Warm up
    for _ in range(5):
        try:
            func(*args)
        except:
            pass
    
    # Actual benchmarking
    for _ in range(iterations):
        start_time = time.perf_counter()
        try:
            result = func(*args)
            end_time = time.perf_counter()
            times.append(end_time - start_time)
        except Exception as e:
            # Skip failed iterations
            continue
    
    if not times:
        return None
    
    return {
        'mean': statistics.mean(times),
        'median': statistics.median(times),
        'std_dev': statistics.stdev(times) if len(times) > 1 else 0,
        'min': min(times),
        'max': max(times),
        'iterations': len(times)
    }

def test_search_decision_performance():
    """Test performance of search decision functions"""
    
    print("⚑ Performance Testing: Search Decision Functions")
    print("=" * 60)
    
    try:
        from search_optimizer import should_perform_search
        
        # Test cases of varying complexity
        test_cases = [
            {
                "name": "Simple prompt, no history",
                "prompt": "What is machine learning?",
                "history": None
            },
            {
                "name": "Follow-up question with history",
                "prompt": "Tell me more about neural networks",
                "history": [
                    {"user": "What is AI?", "assistant": "AI is artificial intelligence that enables machines to perform tasks that typically require human intelligence..."},
                    {"user": "How does machine learning work?", "assistant": "Machine learning works by training algorithms on data to recognize patterns and make predictions..."}
                ]
            },
            {
                "name": "Complex prompt with extensive history",
                "prompt": "Can you elaborate on the differences between supervised and unsupervised learning approaches?",
                "history": [
                    {"user": "What is AI?", "assistant": "AI is artificial intelligence..."},
                    {"user": "Tell me about machine learning", "assistant": "Machine learning is a subset of AI..."},
                    {"user": "What are neural networks?", "assistant": "Neural networks are computing systems inspired by biological neural networks..."},
                    {"user": "How do deep learning models work?", "assistant": "Deep learning models use multiple layers of neural networks..."}
                ]
            }
        ]
        
        for case in test_cases:
            print(f"\nπŸ” Testing: {case['name']}")
            
            # Benchmark the function
            benchmark_result = benchmark_function(
                should_perform_search,
                case["prompt"],
                case["history"],
                iterations=50
            )
            
            if benchmark_result:
                print(f"   ⏱️  Mean time: {benchmark_result['mean']*1000:.2f}ms")
                print(f"   πŸ“Š Median time: {benchmark_result['median']*1000:.2f}ms")
                print(f"   πŸ“ˆ Std deviation: {benchmark_result['std_dev']*1000:.2f}ms")
                print(f"   πŸ”„ Iterations: {benchmark_result['iterations']}")
                
                # Performance thresholds
                if benchmark_result['mean'] < 0.01:  # Less than 10ms
                    print("   βœ… Excellent performance")
                elif benchmark_result['mean'] < 0.05:  # Less than 50ms
                    print("   🟑 Good performance")
                else:
                    print("   ⚠️  Performance may need optimization")
            else:
                print("   ❌ Benchmark failed")
        
        return True
        
    except Exception as e:
        print(f"❌ Performance test error: {e}")
        return False

def test_utility_functions_performance():
    """Test performance of utility functions"""
    
    print("\nπŸ› οΈ  Performance Testing: Utility Functions")
    print("=" * 60)
    
    try:
        from search_optimizer import format_search_context, has_meaningful_conversation_history
        
        # Test format_search_context with different sizes
        small_results = [
            {"source": "Brave", "title": "Test", "body": "Short content"}
        ]
        
        large_results = [
            {
                "source": f"Source{i}",
                "title": f"Long Title {i} with lots of text and information",
                "body": "This is a very long body content that simulates real search results with comprehensive information about various topics including technology, science, and other subjects. " * 10
            }
            for i in range(10)
        ]
        
        print(f"\nπŸ” Testing format_search_context (small dataset)")
        small_benchmark = benchmark_function(format_search_context, small_results, iterations=100)
        
        if small_benchmark:
            print(f"   ⏱️  Mean time: {small_benchmark['mean']*1000:.2f}ms")
            print(f"   πŸ”„ Iterations: {small_benchmark['iterations']}")
        
        print(f"\nπŸ” Testing format_search_context (large dataset)")
        large_benchmark = benchmark_function(format_search_context, large_results, iterations=50)
        
        if large_benchmark:
            print(f"   ⏱️  Mean time: {large_benchmark['mean']*1000:.2f}ms")
            print(f"   πŸ”„ Iterations: {large_benchmark['iterations']}")
        
        # Test has_meaningful_conversation_history
        print(f"\nπŸ” Testing has_meaningful_conversation_history")
        
        complex_history = [
            {"user": f"Question {i}?", "assistant": f"Answer {i} with detailed explanation about the topic."} 
            for i in range(20)
        ]
        
        history_benchmark = benchmark_function(
            has_meaningful_conversation_history, 
            complex_history, 
            iterations=100
        )
        
        if history_benchmark:
            print(f"   ⏱️  Mean time: {history_benchmark['mean']*1000:.2f}ms")
            print(f"   πŸ”„ Iterations: {history_benchmark['iterations']}")
        
        return True
        
    except Exception as e:
        print(f"❌ Utility performance test error: {e}")
        return False

def test_module_import_performance():
    """Test the performance impact of module imports"""
    
    print("\nπŸ“¦ Performance Testing: Module Import Overhead")
    print("=" * 60)
    
    # Test import time
    import_times = []
    
    for i in range(10):
        start_time = time.perf_counter()
        
        # Simulate fresh import (note: this won't actually re-import due to Python's import cache)
        try:
            import search_optimizer
            end_time = time.perf_counter()
            import_times.append(end_time - start_time)
        except Exception as e:
            print(f"❌ Import error: {e}")
            return False
    
    if import_times:
        avg_import_time = statistics.mean(import_times)
        print(f"⏱️  Average import time: {avg_import_time*1000:.2f}ms")
        
        if avg_import_time < 0.001:  # Less than 1ms
            print("βœ… Excellent import performance")
        elif avg_import_time < 0.01:  # Less than 10ms
            print("🟑 Good import performance")
        else:
            print("⚠️  Import time may be higher than expected")
    
    return True

def main():
    """Run performance benchmarking suite"""
    
    print("⚑ Search Optimizer Performance Benchmarking Suite")
    print("=" * 70)
    print("Measuring performance impact of refactoring...")
    print()
    
    tests_passed = 0
    total_tests = 3
    
    # Run performance tests
    if test_search_decision_performance():
        tests_passed += 1
        
    if test_utility_functions_performance():
        tests_passed += 1
        
    if test_module_import_performance():
        tests_passed += 1
    
    # Summary
    print("\n" + "=" * 70)
    print("πŸ“Š PERFORMANCE BENCHMARK SUMMARY")
    print("=" * 70)
    
    if tests_passed == total_tests:
        print(f"βœ… ALL PERFORMANCE TESTS COMPLETED ({tests_passed}/{total_tests})")
        print("πŸš€ Performance characteristics within acceptable ranges!")
        print("πŸ“ˆ Refactoring maintains good performance while improving code organization")
        return True
    else:
        print(f"❌ SOME PERFORMANCE TESTS FAILED ({tests_passed}/{total_tests})")
        print("⚠️  Performance may need attention")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)