CoolFace
Apppublic

findEthics/Atlas

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
test_performance_benchmark.py251 linesDownload Raw Back to performance
1#!/usr/bin/env python32"""3Performance benchmarking for search optimizer refactoring.4 5This script measures the performance impact of the refactoring to ensure6there's no significant performance degradation.7"""8 9import time10import sys11import statistics12from typing import List, Dict, Any13 14def benchmark_function(func, *args, iterations=100):15    """Benchmark a function by running it multiple times and measuring performance"""16    17    times = []18    19    # Warm up20    for _ in range(5):21        try:22            func(*args)23        except:24            pass25    26    # Actual benchmarking27    for _ in range(iterations):28        start_time = time.perf_counter()29        try:30            result = func(*args)31            end_time = time.perf_counter()32            times.append(end_time - start_time)33        except Exception as e:34            # Skip failed iterations35            continue36    37    if not times:38        return None39    40    return {41        'mean': statistics.mean(times),42        'median': statistics.median(times),43        'std_dev': statistics.stdev(times) if len(times) > 1 else 0,44        'min': min(times),45        'max': max(times),46        'iterations': len(times)47    }48 49def test_search_decision_performance():50    """Test performance of search decision functions"""51    52    print("โšก Performance Testing: Search Decision Functions")53    print("=" * 60)54    55    try:56        from search_optimizer import should_perform_search57        58        # Test cases of varying complexity59        test_cases = [60            {61                "name": "Simple prompt, no history",62                "prompt": "What is machine learning?",63                "history": None64            },65            {66                "name": "Follow-up question with history",67                "prompt": "Tell me more about neural networks",68                "history": [69                    {"user": "What is AI?", "assistant": "AI is artificial intelligence that enables machines to perform tasks that typically require human intelligence..."},70                    {"user": "How does machine learning work?", "assistant": "Machine learning works by training algorithms on data to recognize patterns and make predictions..."}71                ]72            },73            {74                "name": "Complex prompt with extensive history",75                "prompt": "Can you elaborate on the differences between supervised and unsupervised learning approaches?",76                "history": [77                    {"user": "What is AI?", "assistant": "AI is artificial intelligence..."},78                    {"user": "Tell me about machine learning", "assistant": "Machine learning is a subset of AI..."},79                    {"user": "What are neural networks?", "assistant": "Neural networks are computing systems inspired by biological neural networks..."},80                    {"user": "How do deep learning models work?", "assistant": "Deep learning models use multiple layers of neural networks..."}81                ]82            }83        ]84        85        for case in test_cases:86            print(f"\n๐Ÿ” Testing: {case['name']}")87            88            # Benchmark the function89            benchmark_result = benchmark_function(90                should_perform_search,91                case["prompt"],92                case["history"],93                iterations=5094            )95            96            if benchmark_result:97                print(f"   โฑ๏ธ  Mean time: {benchmark_result['mean']*1000:.2f}ms")98                print(f"   ๐Ÿ“Š Median time: {benchmark_result['median']*1000:.2f}ms")99                print(f"   ๐Ÿ“ˆ Std deviation: {benchmark_result['std_dev']*1000:.2f}ms")100                print(f"   ๐Ÿ”„ Iterations: {benchmark_result['iterations']}")101                102                # Performance thresholds103                if benchmark_result['mean'] < 0.01:  # Less than 10ms104                    print("   โœ… Excellent performance")105                elif benchmark_result['mean'] < 0.05:  # Less than 50ms106                    print("   ๐ŸŸก Good performance")107                else:108                    print("   โš ๏ธ  Performance may need optimization")109            else:110                print("   โŒ Benchmark failed")111        112        return True113        114    except Exception as e:115        print(f"โŒ Performance test error: {e}")116        return False117 118def test_utility_functions_performance():119    """Test performance of utility functions"""120    121    print("\n๐Ÿ› ๏ธ  Performance Testing: Utility Functions")122    print("=" * 60)123    124    try:125        from search_optimizer import format_search_context, has_meaningful_conversation_history126        127        # Test format_search_context with different sizes128        small_results = [129            {"source": "Brave", "title": "Test", "body": "Short content"}130        ]131        132        large_results = [133            {134                "source": f"Source{i}",135                "title": f"Long Title {i} with lots of text and information",136                "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. " * 10137            }138            for i in range(10)139        ]140        141        print(f"\n๐Ÿ” Testing format_search_context (small dataset)")142        small_benchmark = benchmark_function(format_search_context, small_results, iterations=100)143        144        if small_benchmark:145            print(f"   โฑ๏ธ  Mean time: {small_benchmark['mean']*1000:.2f}ms")146            print(f"   ๐Ÿ”„ Iterations: {small_benchmark['iterations']}")147        148        print(f"\n๐Ÿ” Testing format_search_context (large dataset)")149        large_benchmark = benchmark_function(format_search_context, large_results, iterations=50)150        151        if large_benchmark:152            print(f"   โฑ๏ธ  Mean time: {large_benchmark['mean']*1000:.2f}ms")153            print(f"   ๐Ÿ”„ Iterations: {large_benchmark['iterations']}")154        155        # Test has_meaningful_conversation_history156        print(f"\n๐Ÿ” Testing has_meaningful_conversation_history")157        158        complex_history = [159            {"user": f"Question {i}?", "assistant": f"Answer {i} with detailed explanation about the topic."} 160            for i in range(20)161        ]162        163        history_benchmark = benchmark_function(164            has_meaningful_conversation_history, 165            complex_history, 166            iterations=100167        )168        169        if history_benchmark:170            print(f"   โฑ๏ธ  Mean time: {history_benchmark['mean']*1000:.2f}ms")171            print(f"   ๐Ÿ”„ Iterations: {history_benchmark['iterations']}")172        173        return True174        175    except Exception as e:176        print(f"โŒ Utility performance test error: {e}")177        return False178 179def test_module_import_performance():180    """Test the performance impact of module imports"""181    182    print("\n๐Ÿ“ฆ Performance Testing: Module Import Overhead")183    print("=" * 60)184    185    # Test import time186    import_times = []187    188    for i in range(10):189        start_time = time.perf_counter()190        191        # Simulate fresh import (note: this won't actually re-import due to Python's import cache)192        try:193            import search_optimizer194            end_time = time.perf_counter()195            import_times.append(end_time - start_time)196        except Exception as e:197            print(f"โŒ Import error: {e}")198            return False199    200    if import_times:201        avg_import_time = statistics.mean(import_times)202        print(f"โฑ๏ธ  Average import time: {avg_import_time*1000:.2f}ms")203        204        if avg_import_time < 0.001:  # Less than 1ms205            print("โœ… Excellent import performance")206        elif avg_import_time < 0.01:  # Less than 10ms207            print("๐ŸŸก Good import performance")208        else:209            print("โš ๏ธ  Import time may be higher than expected")210    211    return True212 213def main():214    """Run performance benchmarking suite"""215    216    print("โšก Search Optimizer Performance Benchmarking Suite")217    print("=" * 70)218    print("Measuring performance impact of refactoring...")219    print()220    221    tests_passed = 0222    total_tests = 3223    224    # Run performance tests225    if test_search_decision_performance():226        tests_passed += 1227        228    if test_utility_functions_performance():229        tests_passed += 1230        231    if test_module_import_performance():232        tests_passed += 1233    234    # Summary235    print("\n" + "=" * 70)236    print("๐Ÿ“Š PERFORMANCE BENCHMARK SUMMARY")237    print("=" * 70)238    239    if tests_passed == total_tests:240        print(f"โœ… ALL PERFORMANCE TESTS COMPLETED ({tests_passed}/{total_tests})")241        print("๐Ÿš€ Performance characteristics within acceptable ranges!")242        print("๐Ÿ“ˆ Refactoring maintains good performance while improving code organization")243        return True244    else:245        print(f"โŒ SOME PERFORMANCE TESTS FAILED ({tests_passed}/{total_tests})")246        print("โš ๏ธ  Performance may need attention")247        return False248 249if __name__ == "__main__":250    success = main()251    sys.exit(0 if success else 1)