|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simple performance test that doesn't rely on pytest-benchmark plugin. |
| 4 | +This can be used as a fallback if the benchmark plugin has issues in CI. |
| 5 | +""" |
| 6 | + |
| 7 | +import statistics |
| 8 | +import time |
| 9 | + |
| 10 | +from datafog.services.text_service import TextService |
| 11 | + |
| 12 | + |
| 13 | +def generate_test_text(): |
| 14 | + """Generate consistent test text for performance testing.""" |
| 15 | + base_text = ( |
| 16 | + "Contact John Doe at john.doe@example.com or call (555) 123-4567. " |
| 17 | + "His SSN is 123-45-6789 and credit card 4111-1111-1111-1111. " |
| 18 | + "He lives at 123 Main St, New York, NY 10001. " |
| 19 | + "His IP address is 192.168.1.1 and his birthday is 01/01/1980. " |
| 20 | + "Jane Smith works at Microsoft Corporation in Seattle, Washington. " |
| 21 | + "Her phone number is 555-987-6543 and email is jane.smith@company.org. " |
| 22 | + ) |
| 23 | + # Use consistent moderate size (100 repetitions) |
| 24 | + return base_text * 100 |
| 25 | + |
| 26 | + |
| 27 | +def time_function(func, *args, **kwargs): |
| 28 | + """Time a function execution multiple times and return statistics.""" |
| 29 | + times = [] |
| 30 | + for _ in range(10): # Run 10 times for more stable results |
| 31 | + start = time.perf_counter() |
| 32 | + result = func(*args, **kwargs) |
| 33 | + end = time.perf_counter() |
| 34 | + times.append((end - start) * 1000) # Convert to ms |
| 35 | + |
| 36 | + return { |
| 37 | + "mean": statistics.mean(times), |
| 38 | + "median": statistics.median(times), |
| 39 | + "stdev": statistics.stdev(times) if len(times) > 1 else 0, |
| 40 | + "min": min(times), |
| 41 | + "max": max(times), |
| 42 | + "times": times, |
| 43 | + "result": result, |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | +def test_simple_regex_performance(): |
| 48 | + """Simple regex performance test without pytest-benchmark dependency.""" |
| 49 | + print("Testing regex performance...") |
| 50 | + |
| 51 | + text = generate_test_text() |
| 52 | + regex_service = TextService(engine="regex", text_chunk_length=10000) |
| 53 | + |
| 54 | + stats = time_function(regex_service.annotate_text_sync, text) |
| 55 | + |
| 56 | + print("Regex Performance:") |
| 57 | + print(f" Mean: {stats['mean']:.2f}ms") |
| 58 | + print(f" Median: {stats['median']:.2f}ms") |
| 59 | + print(f" Min: {stats['min']:.2f}ms") |
| 60 | + print(f" Max: {stats['max']:.2f}ms") |
| 61 | + print(f" StdDev: {stats['stdev']:.2f}ms") |
| 62 | + |
| 63 | + # Verify functionality |
| 64 | + assert "EMAIL" in stats["result"] |
| 65 | + assert "PHONE" in stats["result"] |
| 66 | + assert "SSN" in stats["result"] |
| 67 | + |
| 68 | + # Performance sanity check (should be under 50ms for this text size) |
| 69 | + assert stats["mean"] < 50, f"Regex performance too slow: {stats['mean']:.2f}ms" |
| 70 | + |
| 71 | + return stats |
| 72 | + |
| 73 | + |
| 74 | +def test_simple_spacy_performance(): |
| 75 | + """Simple spaCy performance test without pytest-benchmark dependency.""" |
| 76 | + print("Testing spaCy performance...") |
| 77 | + |
| 78 | + text = generate_test_text() |
| 79 | + |
| 80 | + try: |
| 81 | + spacy_service = TextService(engine="spacy", text_chunk_length=10000) |
| 82 | + stats = time_function(spacy_service.annotate_text_sync, text) |
| 83 | + |
| 84 | + print("SpaCy Performance:") |
| 85 | + print(f" Mean: {stats['mean']:.2f}ms") |
| 86 | + print(f" Median: {stats['median']:.2f}ms") |
| 87 | + print(f" Min: {stats['min']:.2f}ms") |
| 88 | + print(f" Max: {stats['max']:.2f}ms") |
| 89 | + print(f" StdDev: {stats['stdev']:.2f}ms") |
| 90 | + |
| 91 | + # Verify functionality |
| 92 | + assert "PERSON" in stats["result"] or "PER" in stats["result"] |
| 93 | + assert "ORG" in stats["result"] |
| 94 | + |
| 95 | + return stats |
| 96 | + |
| 97 | + except ImportError: |
| 98 | + print("SpaCy not available - skipping spaCy performance test") |
| 99 | + return None |
| 100 | + |
| 101 | + |
| 102 | +def run_simple_performance_comparison(): |
| 103 | + """Run simple performance comparison and report results.""" |
| 104 | + print("=" * 60) |
| 105 | + print("SIMPLE PERFORMANCE TEST (no pytest-benchmark)") |
| 106 | + print("=" * 60) |
| 107 | + |
| 108 | + regex_stats = test_simple_regex_performance() |
| 109 | + spacy_stats = test_simple_spacy_performance() |
| 110 | + |
| 111 | + if spacy_stats: |
| 112 | + speedup = spacy_stats["mean"] / regex_stats["mean"] |
| 113 | + print("\nPerformance Comparison:") |
| 114 | + print(f" Regex: {regex_stats['mean']:.2f}ms") |
| 115 | + print(f" SpaCy: {spacy_stats['mean']:.2f}ms") |
| 116 | + print(f" Speedup: {speedup:.1f}x (regex vs spacy)") |
| 117 | + |
| 118 | + # Validate expected performance relationship |
| 119 | + assert ( |
| 120 | + speedup > 5 |
| 121 | + ), f"Regex should be at least 5x faster than spaCy, got {speedup:.1f}x" |
| 122 | + |
| 123 | + print("\n✅ Simple performance tests passed!") |
| 124 | + return {"regex": regex_stats, "spacy": spacy_stats} |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + run_simple_performance_comparison() |
0 commit comments