#!/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)