#!/usr/bin/env python3 """Symbolic Workload: Pure Python Reference Implementation Represents a symbolic computation with: - IF branching based on state - LOOP over multiple items - MATCH pattern detection - CHAIN sequential operations - State updates (resonance) This is the reference implementation that all three benchmarks will execute. """ import time import sys import concurrent.futures from typing import Tuple def symbolic_workload(iterations: int = 100, glyph_count: int = 8) -> float: """Execute a representative symbolic workload. Mimics XIC control flow: - IF: branching on resonance threshold - LOOP: iterate over glyphs - MATCH: pattern matching (every 3rd iteration) - CHAIN: sequential state updates Args: iterations: Number of loop iterations glyph_count: Number of glyphs to process Returns: Final resonance score (0.0 to 1.0) """ resonance = 0.0 for i in range(iterations): # IF: Branch based on resonance state if resonance < 0.5: resonance += 0.02 else: resonance *= 0.99 # LOOP: Process each glyph for g in range(glyph_count): if g % 2 == 0: resonance += 0.001 else: resonance -= 0.0005 # MATCH: Pattern matching (every 3rd iteration) pattern_hit = (i % 3 == 0) if pattern_hit: resonance = resonance * 1.01 # CHAIN: Clamp resonance to valid range resonance = max(0.0, min(1.0, resonance)) return resonance def benchmark_single_threaded(runs: int = 10000) -> Tuple[int, float, float]: """Single-threaded benchmark. Args: runs: Number of workload executions Returns: (runs, elapsed_time, throughput_exec_per_sec) """ start = time.time() for _ in range(runs): symbolic_workload() elapsed = time.time() - start throughput = runs / elapsed if elapsed > 0 else 0 return runs, elapsed, throughput def benchmark_multithreaded(runs: int = 10000, max_workers: int = 16) -> Tuple[int, float, float]: """Multi-threaded benchmark using ThreadPoolExecutor. Args: runs: Number of workload executions max_workers: Number of concurrent worker threads Returns: (runs, elapsed_time, throughput_exec_per_sec) """ def run_one(_): return symbolic_workload() start = time.time() with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: list(executor.map(run_one, range(runs))) elapsed = time.time() - start throughput = runs / elapsed if elapsed > 0 else 0 return runs, elapsed, throughput def main(): """Run benchmark from command line.""" mode = sys.argv[1] if len(sys.argv) > 1 else "single" runs = int(sys.argv[2]) if len(sys.argv) > 2 else 10000 print(f"{'='*60}") print(f"PYTHON SYMBOLIC WORKLOAD BENCHMARK") print(f"{'='*60}") print(f"Mode: {mode}") print(f"Runs: {runs}") print() if mode == "single": exec_runs, elapsed, throughput = benchmark_single_threaded(runs) print(f"Results (Single-threaded):") print(f" Executions: {exec_runs}") print(f" Time: {elapsed:.2f}s") print(f" Throughput: {throughput:.1f} exec/sec") elif mode == "multi": exec_runs, elapsed, throughput = benchmark_multithreaded(runs, max_workers=16) print(f"Results (Multi-threaded, 16 workers):") print(f" Executions: {exec_runs}") print(f" Time: {elapsed:.2f}s") print(f" Throughput: {throughput:.1f} exec/sec") else: print(f"Unknown mode: {mode}") print("Usage: python3 symbolic_workload.py [single|multi] [runs]") sys.exit(1) print(f"{'='*60}") if __name__ == "__main__": main()