|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Benchmark to reproduce performance regression reported in #253. |
| 3 | +
|
| 4 | +Simulates the reporter's workload: 50 patterns scanning 500KB documents |
| 5 | +in block mode. Reports throughput (MB/s) and average time per scan. |
| 6 | +
|
| 7 | +Usage: |
| 8 | + python tools/bench_regression.py |
| 9 | + python tools/bench_regression.py --patterns 100 --doc-size 1048576 |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import os |
| 14 | +import random |
| 15 | +import statistics |
| 16 | +import string |
| 17 | +import time |
| 18 | + |
| 19 | +import hyperscan |
| 20 | + |
| 21 | + |
| 22 | +def generate_patterns(count): |
| 23 | + """Generate realistic regex patterns for benchmarking.""" |
| 24 | + templates = [ |
| 25 | + rb"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b", |
| 26 | + rb"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", |
| 27 | + rb"\b(https?|ftp)://[^\s/$.?#].[^\s]*\b", |
| 28 | + rb"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b", |
| 29 | + rb"\b[A-Z][a-z]+\s[A-Z][a-z]+\b", |
| 30 | + rb"[0-9a-fA-F]{32}", |
| 31 | + rb"\b(error|warning|critical|fatal)\b", |
| 32 | + rb"\b\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\b", |
| 33 | + rb"<[^>]+>", |
| 34 | + rb"\$\d+[\.,]?\d*", |
| 35 | + ] |
| 36 | + keyword_bases = [ |
| 37 | + b"password", b"secret", b"token", b"api.key", |
| 38 | + b"authorization", b"credential", b"private", |
| 39 | + b"admin", b"root", b"config", b"database", |
| 40 | + b"server", b"client", b"session", b"cookie", |
| 41 | + b"header", b"payload", b"request", b"response", |
| 42 | + b"encrypt", b"decrypt", b"hash", b"salt", |
| 43 | + b"certificate", b"key.file", b"login", b"logout", |
| 44 | + b"access", b"permission", b"role", b"user", |
| 45 | + b"account", b"profile", b"setting", b"option", |
| 46 | + b"enable", b"disable", b"start", b"stop", |
| 47 | + b"create", b"delete", b"update", b"select", |
| 48 | + ] |
| 49 | + |
| 50 | + patterns = [] |
| 51 | + for i in range(count): |
| 52 | + if i < len(templates): |
| 53 | + patterns.append(templates[i]) |
| 54 | + else: |
| 55 | + base = keyword_bases[i % len(keyword_bases)] |
| 56 | + suffix = str(i).encode() |
| 57 | + patterns.append(rb"\b" + base + suffix + rb"\b") |
| 58 | + return patterns |
| 59 | + |
| 60 | + |
| 61 | +def generate_document(size): |
| 62 | + """Generate a synthetic document of approximately the given size.""" |
| 63 | + words = [ |
| 64 | + "the", "quick", "brown", "fox", "jumps", "over", "lazy", "dog", |
| 65 | + "server", "error", "warning", "request", "response", "data", |
| 66 | + "user", "admin", "config", "session", "token", "password", |
| 67 | + "https://example.com/path", "192.168.1.100", "test@email.com", |
| 68 | + "2025-01-15T10:30:00", "$1,234.56", "abcdef1234567890abcdef", |
| 69 | + "authorization", "credential", "certificate", "encrypted", |
| 70 | + ] |
| 71 | + rng = random.Random(42) |
| 72 | + chunks = [] |
| 73 | + total = 0 |
| 74 | + while total < size: |
| 75 | + line_words = rng.choices(words, k=rng.randint(5, 20)) |
| 76 | + line = " ".join(line_words) + "\n" |
| 77 | + chunks.append(line) |
| 78 | + total += len(line) |
| 79 | + return "".join(chunks)[:size].encode("utf-8") |
| 80 | + |
| 81 | + |
| 82 | +def run_benchmark(db, document, num_scans, warmup=3): |
| 83 | + """Run the benchmark and return per-scan times.""" |
| 84 | + match_count = 0 |
| 85 | + |
| 86 | + def on_match(id, start, end, flags, ctx): |
| 87 | + nonlocal match_count |
| 88 | + match_count += 1 |
| 89 | + |
| 90 | + # warmup |
| 91 | + for _ in range(warmup): |
| 92 | + db.scan(document, match_event_handler=on_match) |
| 93 | + |
| 94 | + match_count = 0 |
| 95 | + times = [] |
| 96 | + for _ in range(num_scans): |
| 97 | + t0 = time.perf_counter() |
| 98 | + db.scan(document, match_event_handler=on_match) |
| 99 | + t1 = time.perf_counter() |
| 100 | + times.append(t1 - t0) |
| 101 | + |
| 102 | + return times, match_count |
| 103 | + |
| 104 | + |
| 105 | +def main(): |
| 106 | + parser = argparse.ArgumentParser( |
| 107 | + description="Benchmark for hyperscan regression #253" |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--patterns", type=int, default=50, |
| 111 | + help="Number of regex patterns (default: 50)", |
| 112 | + ) |
| 113 | + parser.add_argument( |
| 114 | + "--doc-size", type=int, default=500_000, |
| 115 | + help="Document size in bytes (default: 500000)", |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "--scans", type=int, default=100, |
| 119 | + help="Number of scans to perform (default: 100)", |
| 120 | + ) |
| 121 | + parser.add_argument( |
| 122 | + "--warmup", type=int, default=5, |
| 123 | + help="Number of warmup scans (default: 5)", |
| 124 | + ) |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + print("=" * 60) |
| 128 | + print("hyperscan regression benchmark (#253)") |
| 129 | + print("=" * 60) |
| 130 | + |
| 131 | + db_info = hyperscan.Database(mode=hyperscan.HS_MODE_BLOCK) |
| 132 | + patterns = generate_patterns(args.patterns) |
| 133 | + db_info.compile( |
| 134 | + expressions=patterns, |
| 135 | + ids=list(range(len(patterns))), |
| 136 | + flags=[hyperscan.HS_FLAG_CASELESS | hyperscan.HS_FLAG_SINGLEMATCH] |
| 137 | + * len(patterns), |
| 138 | + ) |
| 139 | + |
| 140 | + print(f"engine info: {db_info.info().decode()}") |
| 141 | + print(f"database size: {db_info.size():,} bytes") |
| 142 | + print(f"pattern count: {args.patterns}") |
| 143 | + print(f"document size: {args.doc_size:,} bytes") |
| 144 | + print(f"scan iterations: {args.scans}") |
| 145 | + print(f"warmup scans: {args.warmup}") |
| 146 | + print() |
| 147 | + |
| 148 | + document = generate_document(args.doc_size) |
| 149 | + |
| 150 | + print("running benchmark...") |
| 151 | + times, match_count = run_benchmark( |
| 152 | + db_info, document, args.scans, args.warmup |
| 153 | + ) |
| 154 | + |
| 155 | + avg_time = statistics.mean(times) |
| 156 | + median_time = statistics.median(times) |
| 157 | + stdev_time = statistics.stdev(times) if len(times) > 1 else 0 |
| 158 | + min_time = min(times) |
| 159 | + max_time = max(times) |
| 160 | + doc_mb = args.doc_size / (1024 * 1024) |
| 161 | + throughput_avg = doc_mb / avg_time if avg_time > 0 else float("inf") |
| 162 | + throughput_median = ( |
| 163 | + doc_mb / median_time if median_time > 0 else float("inf") |
| 164 | + ) |
| 165 | + |
| 166 | + print() |
| 167 | + print("-" * 60) |
| 168 | + print("results") |
| 169 | + print("-" * 60) |
| 170 | + print(f"total matches: {match_count:,}") |
| 171 | + print(f"avg time/scan: {avg_time * 1000:.3f} ms") |
| 172 | + print(f"median time/scan: {median_time * 1000:.3f} ms") |
| 173 | + print(f"min time/scan: {min_time * 1000:.3f} ms") |
| 174 | + print(f"max time/scan: {max_time * 1000:.3f} ms") |
| 175 | + print(f"stdev: {stdev_time * 1000:.3f} ms") |
| 176 | + print(f"throughput (avg): {throughput_avg:.1f} MB/s") |
| 177 | + print(f"throughput (median):{throughput_median:.1f} MB/s") |
| 178 | + print() |
| 179 | + |
| 180 | + if avg_time * 1000 > 10: |
| 181 | + print("!! REGRESSION DETECTED !!") |
| 182 | + print( |
| 183 | + f"avg scan time {avg_time*1000:.1f}ms is way above the " |
| 184 | + f"expected ~3ms baseline from v0.7.21" |
| 185 | + ) |
| 186 | + print( |
| 187 | + "likely cause: SIMDE_BACKEND=ON forcing SSE2-only code " |
| 188 | + "paths on x86-64" |
| 189 | + ) |
| 190 | + else: |
| 191 | + print("performance looks healthy") |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + main() |
0 commit comments