|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Benchmark script for RavenChain performance testing. |
| 4 | +Tests transaction processing, mining speed, and chain validation. |
| 5 | +""" |
| 6 | +import time |
| 7 | +import statistics |
| 8 | +from typing import List, Tuple, Dict |
| 9 | +from config.logging import setup_logging |
| 10 | +from ravenchain.blockchain import Blockchain |
| 11 | +from ravenchain.wallet import Wallet |
| 12 | + |
| 13 | +logger = setup_logging("ravenchain.benchmark") |
| 14 | + |
| 15 | + |
| 16 | +class BlockchainBenchmark: |
| 17 | + def __init__(self, difficulty: int = 2): |
| 18 | + self.blockchain = Blockchain(difficulty=difficulty) |
| 19 | + self.wallet = Wallet() |
| 20 | + self.wallet_registry: Dict[str, Wallet] = {self.wallet.address: self.wallet} |
| 21 | + |
| 22 | + def benchmark_mining(self, num_blocks: int = 5) -> List[float]: |
| 23 | + """Benchmark mining performance.""" |
| 24 | + mining_times = [] |
| 25 | + |
| 26 | + for _ in range(num_blocks): |
| 27 | + # Add some transactions |
| 28 | + for _ in range(3): |
| 29 | + recipient = Wallet() |
| 30 | + self.wallet_registry[recipient.address] = recipient |
| 31 | + self.blockchain.add_transaction( |
| 32 | + self.wallet.address, recipient.address, 1.0, wallet=self.wallet |
| 33 | + ) |
| 34 | + |
| 35 | + # Time the mining process |
| 36 | + start_time = time.time() |
| 37 | + self.blockchain.mine_pending_transactions(self.wallet.address) |
| 38 | + mining_times.append(time.time() - start_time) |
| 39 | + |
| 40 | + return mining_times |
| 41 | + |
| 42 | + def benchmark_transaction_processing(self, num_transactions: int = 100) -> float: |
| 43 | + """Benchmark transaction processing speed.""" |
| 44 | + start_time = time.time() |
| 45 | + |
| 46 | + for _ in range(num_transactions): |
| 47 | + recipient = Wallet() |
| 48 | + self.wallet_registry[recipient.address] = recipient |
| 49 | + self.blockchain.add_transaction( |
| 50 | + self.wallet.address, recipient.address, 1.0, wallet=self.wallet |
| 51 | + ) |
| 52 | + |
| 53 | + return time.time() - start_time |
| 54 | + |
| 55 | + def benchmark_chain_validation(self) -> float: |
| 56 | + """Benchmark chain validation speed.""" |
| 57 | + # First, create a decent-sized chain |
| 58 | + for _ in range(5): |
| 59 | + recipient = Wallet() |
| 60 | + self.wallet_registry[recipient.address] = recipient |
| 61 | + self.blockchain.add_transaction( |
| 62 | + self.wallet.address, recipient.address, 1.0, wallet=self.wallet |
| 63 | + ) |
| 64 | + self.blockchain.mine_pending_transactions(self.wallet.address) |
| 65 | + |
| 66 | + # Time the validation |
| 67 | + start_time = time.time() |
| 68 | + self.blockchain.is_chain_valid(self.wallet_registry) |
| 69 | + return time.time() - start_time |
| 70 | + |
| 71 | + |
| 72 | +def run_benchmarks() -> Tuple[List[float], float, float]: |
| 73 | + """Run all benchmarks and return results.""" |
| 74 | + try: |
| 75 | + benchmark = BlockchainBenchmark() |
| 76 | + |
| 77 | + # Mining benchmark |
| 78 | + logger.info("Starting mining benchmark") |
| 79 | + mining_times = benchmark.benchmark_mining() |
| 80 | + avg_mining_time = statistics.mean(mining_times) |
| 81 | + logger.info( |
| 82 | + "Mining benchmark complete", avg_time=f"{avg_mining_time:.2f}s", times=mining_times |
| 83 | + ) |
| 84 | + |
| 85 | + # Transaction processing benchmark |
| 86 | + logger.info("Starting transaction processing benchmark") |
| 87 | + tx_time = benchmark.benchmark_transaction_processing() |
| 88 | + logger.info( |
| 89 | + "Transaction benchmark complete", |
| 90 | + total_time=f"{tx_time:.2f}s", |
| 91 | + transactions_per_second=f"{100/tx_time:.2f}", |
| 92 | + ) |
| 93 | + |
| 94 | + # Chain validation benchmark |
| 95 | + logger.info("Starting chain validation benchmark") |
| 96 | + validation_time = benchmark.benchmark_chain_validation() |
| 97 | + logger.info("Validation benchmark complete", validation_time=f"{validation_time:.2f}s") |
| 98 | + |
| 99 | + return mining_times, tx_time, validation_time |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + logger.error("Benchmark failed", error=str(e), exc_info=True) |
| 103 | + raise |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + run_benchmarks() |
0 commit comments