|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import io |
| 5 | +import random |
| 6 | +import tempfile |
| 7 | +import time |
| 8 | +from datetime import datetime |
| 9 | +from ipaddress import IPv4Address |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import aiofiles |
| 13 | +from chia_rs.sized_ints import uint16, uint64 |
| 14 | + |
| 15 | +from chia.server.address_manager import ( |
| 16 | + NEW_BUCKETS_PER_ADDRESS, |
| 17 | + AddressManager, |
| 18 | + ExtendedPeerInfo, |
| 19 | +) |
| 20 | +from chia.types.peer_info import TimestampedPeerInfo |
| 21 | +from chia.util.files import write_file_async |
| 22 | + |
| 23 | + |
| 24 | +def generate_random_ip(rand: random.Random) -> str: |
| 25 | + return str(IPv4Address(rand.getrandbits(32))) |
| 26 | + |
| 27 | + |
| 28 | +def populate_address_manager(num_new: int = 500000, num_tried: int = 200000) -> AddressManager: |
| 29 | + rand = random.Random() |
| 30 | + rand.seed(1337) |
| 31 | + am = AddressManager() |
| 32 | + current_time = int(datetime.now().timestamp()) |
| 33 | + total = num_new + num_tried |
| 34 | + |
| 35 | + for i in range(total): |
| 36 | + host = generate_random_ip(rand) |
| 37 | + port = rand.randint(1024, 65535) |
| 38 | + timestamp = current_time - rand.randint(0, 100000) |
| 39 | + |
| 40 | + # Construct TimestampedPeerInfo |
| 41 | + tpi = TimestampedPeerInfo(host=host, port=uint16(port), timestamp=uint64(timestamp)) |
| 42 | + |
| 43 | + # Create the ExtendedPeerInfo |
| 44 | + epi = ExtendedPeerInfo( |
| 45 | + addr=tpi, |
| 46 | + src_peer=None, # will default to itself inside constructor |
| 47 | + ) |
| 48 | + |
| 49 | + am.tried_count += 1 # why do we even have `assert tried_ids != tried_count`? |
| 50 | + node_id = am.id_count |
| 51 | + am.id_count += 1 |
| 52 | + epi.random_pos = len(am.random_pos) |
| 53 | + am.map_info[node_id] = epi |
| 54 | + am.map_addr[epi.peer_info.host] = node_id |
| 55 | + am.random_pos.append(node_id) |
| 56 | + |
| 57 | + if i >= num_new: |
| 58 | + # make a tried_table entry |
| 59 | + epi.is_tried = True |
| 60 | + epi.last_success = timestamp |
| 61 | + epi.last_try = timestamp - rand.randint(0, 1000) |
| 62 | + bucket = epi.get_tried_bucket(am.key) |
| 63 | + pos = epi.get_bucket_position(am.key, False, bucket) |
| 64 | + if am.tried_matrix[bucket][pos] == -1: |
| 65 | + am.tried_matrix[bucket][pos] = node_id |
| 66 | + am.tried_count += 1 |
| 67 | + else: |
| 68 | + # make a new_table entry |
| 69 | + ref_count = rand.randint(1, NEW_BUCKETS_PER_ADDRESS) |
| 70 | + epi.ref_count = ref_count |
| 71 | + assigned = False |
| 72 | + for _ in range(ref_count): |
| 73 | + bucket = epi.get_new_bucket(am.key) |
| 74 | + pos = epi.get_bucket_position(am.key, True, bucket) |
| 75 | + if am.new_matrix[bucket][pos] == -1: |
| 76 | + am.new_matrix[bucket][pos] = node_id |
| 77 | + am.new_count += 1 |
| 78 | + assigned = True |
| 79 | + break |
| 80 | + if not assigned: |
| 81 | + # fallback if no bucket available |
| 82 | + epi.ref_count = 0 |
| 83 | + |
| 84 | + return am |
| 85 | + |
| 86 | + |
| 87 | +async def benchmark_serialize_deserialize(iterations: int = 5) -> None: |
| 88 | + """ |
| 89 | + Benchmarks the serialization and deserialization of peer data. |
| 90 | + """ |
| 91 | + |
| 92 | + total_serialize_time = 0.0 |
| 93 | + total_deserialize_time = 0.0 |
| 94 | + |
| 95 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 96 | + peers_file_path = Path(tmpdir) / "peers.dat" |
| 97 | + |
| 98 | + for i in range(iterations): |
| 99 | + address_manager: AddressManager = populate_address_manager() |
| 100 | + print(f"--- Benchmark Run {i + 1} ---") |
| 101 | + |
| 102 | + # Benchmark serialize |
| 103 | + start_serialize = time.perf_counter() |
| 104 | + |
| 105 | + serialised_bytes = address_manager.serialize_bytes() |
| 106 | + await write_file_async(peers_file_path, serialised_bytes, file_mode=0o644) |
| 107 | + end_serialize = time.perf_counter() |
| 108 | + serialize_duration = end_serialize - start_serialize |
| 109 | + total_serialize_time += serialize_duration |
| 110 | + print(f"Serialize time: {serialize_duration:.6f} seconds") |
| 111 | + |
| 112 | + # Benchmark deserialize |
| 113 | + async with aiofiles.open(peers_file_path, "rb") as f: |
| 114 | + data = io.BytesIO(await f.read()) |
| 115 | + start_deserialize = time.perf_counter() |
| 116 | + _ = AddressManager.deserialize_bytes(data) |
| 117 | + end_deserialize = time.perf_counter() |
| 118 | + deserialize_duration = end_deserialize - start_deserialize |
| 119 | + total_deserialize_time += deserialize_duration |
| 120 | + print(f"Deserialize time: {deserialize_duration:.6f} seconds") |
| 121 | + |
| 122 | + print(f"\n=== Benchmark Summary ({iterations} iterations) ===") |
| 123 | + print(f"Average serialize time: {total_serialize_time / iterations:.6f} seconds") |
| 124 | + print(f"Average deserialize time: {total_deserialize_time / iterations:.6f} seconds") |
| 125 | + |
| 126 | + |
| 127 | +async def main() -> None: |
| 128 | + await benchmark_serialize_deserialize(iterations=10) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + asyncio.run(main()) |
0 commit comments