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