Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /dchrnv_neurograph /tests /performance /stress_benchmark.py
| #!/usr/bin/env python3 | |
| """ | |
| NeuroGraph - Comprehensive Stress Benchmark | |
| Version: v0.67.4 | |
| Date: 2026-01-10 | |
| Safe stress testing on multiple scales: 1M, 10M, 100M tokens | |
| Tests Token operations with real Rust Core module. | |
| Usage: | |
| python tests/performance/stress_benchmark.py 1m # 1 million tokens | |
| python tests/performance/stress_benchmark.py 10m # 10 million tokens | |
| python tests/performance/stress_benchmark.py 100m # 100 million tokens | |
| python tests/performance/stress_benchmark.py all # All scales | |
| """ | |
| import sys | |
| import time | |
| import psutil | |
| import json | |
| from datetime import datetime | |
| from pathlib import Path | |
| # Add python path | |
| sys.path.insert(0, 'python') | |
| try: | |
| import neurograph | |
| print(f"โ neurograph v{neurograph.__version__} loaded") | |
| except Exception as e: | |
| print(f"โ Failed to import neurograph: {e}") | |
| sys.exit(1) | |
| # Benchmark configurations | |
| SCALES = { | |
| "1m": { | |
| "name": "1M Tokens", | |
| "tokens": 1_000_000, | |
| "batch_size": 100_000, | |
| "access_sample": 10_000, | |
| "expected_time": "~1 minute", | |
| "emoji": "๐ฅ" | |
| }, | |
| "10m": { | |
| "name": "10M Tokens", | |
| "tokens": 10_000_000, | |
| "batch_size": 500_000, | |
| "access_sample": 100_000, | |
| "expected_time": "~10 minutes", | |
| "emoji": "๐ฅ" | |
| }, | |
| "100m": { | |
| "name": "100M Tokens", | |
| "tokens": 100_000_000, | |
| "batch_size": 1_000_000, | |
| "access_sample": 1_000_000, | |
| "expected_time": "~2 hours", | |
| "emoji": "๐" | |
| } | |
| } | |
| class StressBenchmark: | |
| """Safe stress benchmark runner.""" | |
| def __init__(self, scale: str): | |
| self.scale_name = scale | |
| self.config = SCALES[scale] | |
| self.process = psutil.Process() | |
| self.results = { | |
| "scale": scale, | |
| "config": self.config, | |
| "timestamp": datetime.now().isoformat(), | |
| "system": self._get_system_info(), | |
| "benchmarks": {} | |
| } | |
| def _get_system_info(self): | |
| """Collect system info.""" | |
| return { | |
| "cpu_cores": psutil.cpu_count(logical=False), | |
| "cpu_threads": psutil.cpu_count(logical=True), | |
| "memory_gb": round(psutil.virtual_memory().total / (1024**3), 2), | |
| "python_version": sys.version.split()[0], | |
| "neurograph_version": neurograph.__version__ | |
| } | |
| def log(self, msg: str, emoji: str = "โน๏ธ"): | |
| """Log with timestamp.""" | |
| ts = datetime.now().strftime("%H:%M:%S") | |
| print(f"[{ts}] {emoji} {msg}") | |
| def get_memory_mb(self): | |
| """Current memory in MB.""" | |
| return self.process.memory_info().rss / (1024**2) | |
| def benchmark_single_creation(self): | |
| """Benchmark single token creation.""" | |
| self.log(f"Testing single token creation ({self.config['tokens']:,} tokens)...") | |
| # Use smaller sample for very large scales | |
| test_count = min(100_000, self.config['tokens']) | |
| start_mem = self.get_memory_mb() | |
| start_time = time.perf_counter() | |
| tokens = [] | |
| for i in range(test_count): | |
| token = neurograph.Token(i) | |
| tokens.append(token) | |
| if (i + 1) % 10_000 == 0: | |
| elapsed = time.perf_counter() - start_time | |
| rate = (i + 1) / elapsed | |
| self.log(f" Progress: {i+1:,}/{test_count:,} ({rate:,.0f} tokens/s)", "๐") | |
| elapsed = time.perf_counter() - start_time | |
| end_mem = self.get_memory_mb() | |
| result = { | |
| "test_count": test_count, | |
| "time_sec": round(elapsed, 3), | |
| "rate_per_sec": round(test_count / elapsed, 2), | |
| "latency_us": round((elapsed / test_count) * 1_000_000, 3), | |
| "memory_start_mb": round(start_mem, 2), | |
| "memory_end_mb": round(end_mem, 2), | |
| "memory_delta_mb": round(end_mem - start_mem, 2) | |
| } | |
| self.log(f"โ Single creation: {result['rate_per_sec']:,.0f} tokens/s, " | |
| f"{result['latency_us']:.3f}ยตs/token", "โ ") | |
| return result | |
| def benchmark_batch_creation(self): | |
| """Benchmark batch token creation in chunks.""" | |
| self.log(f"Testing batch creation ({self.config['tokens']:,} tokens in batches)...") | |
| batch_size = self.config['batch_size'] | |
| total_tokens = self.config['tokens'] | |
| num_batches = total_tokens // batch_size | |
| start_mem = self.get_memory_mb() | |
| start_time = time.perf_counter() | |
| total_created = 0 | |
| batch_times = [] | |
| for batch_num in range(num_batches): | |
| batch_start = time.perf_counter() | |
| # Create batch | |
| batch = neurograph.Token.create_batch(batch_size) | |
| total_created += batch_size | |
| batch_elapsed = time.perf_counter() - batch_start | |
| batch_times.append(batch_elapsed) | |
| # Clear batch to free memory | |
| del batch | |
| # Progress | |
| elapsed = time.perf_counter() - start_time | |
| rate = total_created / elapsed | |
| mem = self.get_memory_mb() | |
| progress = (batch_num + 1) / num_batches * 100 | |
| self.log(f" Batch {batch_num+1}/{num_batches} ({progress:.1f}%): " | |
| f"{total_created:,} tokens, {rate:,.0f} tokens/s, {mem:.1f}MB", "๐") | |
| total_elapsed = time.perf_counter() - start_time | |
| end_mem = self.get_memory_mb() | |
| result = { | |
| "total_tokens": total_created, | |
| "num_batches": num_batches, | |
| "batch_size": batch_size, | |
| "time_sec": round(total_elapsed, 2), | |
| "rate_per_sec": round(total_created / total_elapsed, 2), | |
| "latency_us": round((total_elapsed / total_created) * 1_000_000, 3), | |
| "avg_batch_time_sec": round(sum(batch_times) / len(batch_times), 3), | |
| "memory_start_mb": round(start_mem, 2), | |
| "memory_end_mb": round(end_mem, 2), | |
| "memory_delta_mb": round(end_mem - start_mem, 2) | |
| } | |
| self.log(f"โ Batch creation: {result['rate_per_sec']:,.0f} tokens/s, " | |
| f"{result['latency_us']:.3f}ยตs/token, {num_batches} batches", "โ ") | |
| return result | |
| def benchmark_token_access(self): | |
| """Benchmark token attribute access.""" | |
| self.log(f"Testing token access ({self.config['access_sample']:,} accesses)...") | |
| # Create tokens for access test | |
| sample_size = min(10_000, self.config['access_sample']) | |
| tokens = [neurograph.Token(i) for i in range(sample_size)] | |
| access_count = self.config['access_sample'] | |
| start_time = time.perf_counter() | |
| for i in range(access_count): | |
| token = tokens[i % sample_size] | |
| _ = token.id | |
| _ = token.coordinates | |
| if (i + 1) % 100_000 == 0: | |
| elapsed = time.perf_counter() - start_time | |
| rate = (i + 1) / elapsed | |
| self.log(f" Progress: {i+1:,}/{access_count:,} ({rate:,.0f} accesses/s)", "๐") | |
| elapsed = time.perf_counter() - start_time | |
| result = { | |
| "access_count": access_count, | |
| "sample_size": sample_size, | |
| "time_sec": round(elapsed, 3), | |
| "rate_per_sec": round(access_count / elapsed, 2), | |
| "latency_ns": round((elapsed / access_count) * 1_000_000_000, 1) | |
| } | |
| self.log(f"โ Token access: {result['rate_per_sec']:,.0f} accesses/s, " | |
| f"{result['latency_ns']:.1f}ns/access", "โ ") | |
| return result | |
| def run(self): | |
| """Execute all benchmarks.""" | |
| self.log("=" * 80) | |
| self.log(f"{self.config['emoji']} {self.config['name']} STRESS BENCHMARK", "๐") | |
| self.log("=" * 80) | |
| self.log(f"Total tokens: {self.config['tokens']:,}") | |
| self.log(f"Batch size: {self.config['batch_size']:,}") | |
| self.log(f"Expected time: {self.config['expected_time']}") | |
| self.log(f"System: {self.results['system']['cpu_cores']} cores, " | |
| f"{self.results['system']['memory_gb']} GB RAM") | |
| self.log("=" * 80) | |
| overall_start = time.time() | |
| try: | |
| # 1. Single token creation (sample) | |
| self.results['benchmarks']['single_creation'] = self.benchmark_single_creation() | |
| # 2. Batch creation (full scale) | |
| self.results['benchmarks']['batch_creation'] = self.benchmark_batch_creation() | |
| # 3. Token access | |
| self.results['benchmarks']['token_access'] = self.benchmark_token_access() | |
| overall_elapsed = time.time() - overall_start | |
| self.results['total_time_sec'] = round(overall_elapsed, 2) | |
| self.results['status'] = 'SUCCESS' | |
| self.log("=" * 80) | |
| self.log(f"โ BENCHMARK COMPLETE in {overall_elapsed:.1f}s", "โ ") | |
| self.log("=" * 80) | |
| except Exception as e: | |
| self.log(f"โ BENCHMARK FAILED: {e}", "โ") | |
| import traceback | |
| traceback.print_exc() | |
| self.results['status'] = 'FAILED' | |
| self.results['error'] = str(e) | |
| return self.results | |
| def print_summary(self): | |
| """Print benchmark summary.""" | |
| print("\n" + "=" * 80) | |
| print("๐ SUMMARY") | |
| print("=" * 80) | |
| print(f"\nScale: {self.config['name']} ({self.config['tokens']:,} tokens)") | |
| print(f"Status: {'โ SUCCESS' if self.results['status'] == 'SUCCESS' else 'โ FAILED'}") | |
| print(f"Total Time: {self.results.get('total_time_sec', 0):.1f}s") | |
| if 'benchmarks' in self.results: | |
| benchmarks = self.results['benchmarks'] | |
| if 'batch_creation' in benchmarks: | |
| bc = benchmarks['batch_creation'] | |
| print(f"\n๐ฅ Batch Creation ({bc['total_tokens']:,} tokens):") | |
| print(f" โข Throughput: {bc['rate_per_sec']:,.0f} tokens/s") | |
| print(f" โข Latency: {bc['latency_us']:.3f}ยตs/token") | |
| print(f" โข Batches: {bc['num_batches']} x {bc['batch_size']:,}") | |
| print(f" โข Memory: {bc['memory_delta_mb']:.1f}MB delta") | |
| if 'single_creation' in benchmarks: | |
| sc = benchmarks['single_creation'] | |
| print(f"\n๐ Single Creation (sample {sc['test_count']:,}):") | |
| print(f" โข Throughput: {sc['rate_per_sec']:,.0f} tokens/s") | |
| print(f" โข Latency: {sc['latency_us']:.3f}ยตs/token") | |
| if 'token_access' in benchmarks: | |
| ta = benchmarks['token_access'] | |
| print(f"\nโก Token Access ({ta['access_count']:,} accesses):") | |
| print(f" โข Throughput: {ta['rate_per_sec']:,.0f} accesses/s") | |
| print(f" โข Latency: {ta['latency_ns']:.1f}ns/access") | |
| print("\n" + "=" * 80) | |
| def save_results(self): | |
| """Save results to JSON.""" | |
| filename = f"stress_benchmark_{self.scale_name}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" | |
| filepath = Path("tests/performance") / filename | |
| with open(filepath, 'w') as f: | |
| json.dump(self.results, f, indent=2) | |
| self.log(f"๐พ Results saved: {filepath}", "๐พ") | |
| return filepath | |
| def main(): | |
| """Main entry point.""" | |
| if len(sys.argv) < 2: | |
| print(__doc__) | |
| print("\nAvailable scales:") | |
| for scale, config in SCALES.items(): | |
| print(f" {config['emoji']} {scale:5} - {config['name']} ({config['expected_time']})") | |
| sys.exit(1) | |
| mode = sys.argv[1].lower() | |
| if mode == "all": | |
| # Run all scales | |
| all_results = [] | |
| for scale in ["1m", "10m", "100m"]: | |
| print(f"\n{'='*80}") | |
| print(f"Starting {SCALES[scale]['name']} benchmark...") | |
| print(f"{'='*80}\n") | |
| benchmark = StressBenchmark(scale) | |
| results = benchmark.run() | |
| benchmark.print_summary() | |
| filepath = benchmark.save_results() | |
| all_results.append({ | |
| "scale": scale, | |
| "filepath": str(filepath), | |
| "status": results['status'] | |
| }) | |
| # Final summary | |
| print("\n" + "="*80) | |
| print("๐ ALL BENCHMARKS SUMMARY") | |
| print("="*80) | |
| for r in all_results: | |
| status = "โ " if r['status'] == 'SUCCESS' else "โ" | |
| print(f"{status} {r['scale']:5} - {r['filepath']}") | |
| print("="*80) | |
| elif mode in SCALES: | |
| # Run single scale | |
| benchmark = StressBenchmark(mode) | |
| results = benchmark.run() | |
| benchmark.print_summary() | |
| benchmark.save_results() | |
| else: | |
| print(f"โ Unknown scale: {mode}") | |
| print(__doc__) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 12.8 kB
- Xet hash:
- 8d60d5b541ac8a464b7c0f95ed4c3867c449d2cb0464e936ec8165895136c2eb
ยท
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.