|
| 1 | +import asyncio |
| 2 | +import time |
| 3 | +import struct |
| 4 | +import psutil |
| 5 | +import json |
| 6 | + |
| 7 | +class SimpleMQTTClient: |
| 8 | + def __init__(self, host='127.0.0.1', port=1883, client_id='bench-client'): |
| 9 | + self.host = host |
| 10 | + self.port = port |
| 11 | + self.client_id = client_id |
| 12 | + self.reader = None |
| 13 | + self.writer = None |
| 14 | + |
| 15 | + async def connect(self): |
| 16 | + self.reader, self.writer = await asyncio.open_connection(self.host, self.port) |
| 17 | + |
| 18 | + # Fixed Header: Connect (0x10), Remaining Length |
| 19 | + # Variable Header: Protocol Name (0004 'MQTT'), Protocol Level (04), Connect Flags (02 = Clean Session), Keep Alive (003c = 60s) |
| 20 | + # Payload: Client ID |
| 21 | + |
| 22 | + protocol_name = b'\x00\x04MQTT' |
| 23 | + protocol_level = b'\x04' |
| 24 | + connect_flags = b'\x02' |
| 25 | + keep_alive = b'\x00\x3c' |
| 26 | + |
| 27 | + payload = struct.pack('!H', len(self.client_id)) + self.client_id.encode() |
| 28 | + variable_header = protocol_name + protocol_level + connect_flags + keep_alive |
| 29 | + |
| 30 | + remaining_length = len(variable_header) + len(payload) |
| 31 | + fixed_header = struct.pack('!BB', 0x10, remaining_length) |
| 32 | + |
| 33 | + packet = fixed_header + variable_header + payload |
| 34 | + self.writer.write(packet) |
| 35 | + await self.writer.drain() |
| 36 | + |
| 37 | + # Wait for CONNACK |
| 38 | + connack = await self.reader.readexactly(4) |
| 39 | + if connack[0] != 0x20: |
| 40 | + raise Exception("Failed to connect") |
| 41 | + |
| 42 | + async def subscribe(self, topic): |
| 43 | + # Fixed Header: Subscribe (0x82), Remaining Length |
| 44 | + # Variable Header: Packet Identifier (0001) |
| 45 | + # Payload: Topic Filter, Requested QoS (0) |
| 46 | + |
| 47 | + packet_id = b'\x00\x01' |
| 48 | + payload = struct.pack('!H', len(topic)) + topic.encode() + b'\x00' |
| 49 | + remaining_length = len(packet_id) + len(payload) |
| 50 | + fixed_header = struct.pack('!BB', 0x82, remaining_length) |
| 51 | + |
| 52 | + packet = fixed_header + packet_id + payload |
| 53 | + self.writer.write(packet) |
| 54 | + await self.writer.drain() |
| 55 | + |
| 56 | + # Wait for SUBACK |
| 57 | + suback = await self.reader.readexactly(5) |
| 58 | + if suback[0] != 0x90: |
| 59 | + raise Exception("Failed to subscribe") |
| 60 | + |
| 61 | + async def publish(self, topic, message): |
| 62 | + # Fixed Header: Publish (0x30), Remaining Length |
| 63 | + # Variable Header: Topic Name |
| 64 | + # Payload: Message |
| 65 | + |
| 66 | + var_header = struct.pack('!H', len(topic)) + topic.encode() |
| 67 | + payload = message.encode() |
| 68 | + remaining_length = len(var_header) + len(payload) |
| 69 | + fixed_header = bytes([0x30, remaining_length]) # Simplified for small lengths |
| 70 | + |
| 71 | + packet = fixed_header + var_header + payload |
| 72 | + self.writer.write(packet) |
| 73 | + await self.writer.drain() |
| 74 | + |
| 75 | + async def wait_for_message(self): |
| 76 | + # Very simplified publish packet reading |
| 77 | + header = await self.reader.readexactly(1) |
| 78 | + if (header[0] & 0xF0) == 0x30: |
| 79 | + rem_len = await self.reader.readexactly(1) # Assuming < 128 |
| 80 | + data = await self.reader.readexactly(rem_len[0]) |
| 81 | + topic_len = struct.unpack('!H', data[:2])[0] |
| 82 | + msg = data[2 + topic_len:] |
| 83 | + return msg.decode() |
| 84 | + return None |
| 85 | + |
| 86 | + async def disconnect(self): |
| 87 | + if self.writer: |
| 88 | + self.writer.write(b'\xe0\x00') # Fixed Header: Disconnect (0xe0), 0 length |
| 89 | + await self.writer.drain() |
| 90 | + self.writer.close() |
| 91 | + await self.writer.wait_closed() |
| 92 | + |
| 93 | +async def benchmark_concurrency(target_clients=100): |
| 94 | + print(f"--- Concurrency Test: {target_clients} clients ---") |
| 95 | + clients = [] |
| 96 | + start_time = time.time() |
| 97 | + |
| 98 | + for i in range(target_clients): |
| 99 | + client = SimpleMQTTClient(client_id=f"bench-{i}") |
| 100 | + try: |
| 101 | + await client.connect() |
| 102 | + clients.append(client) |
| 103 | + if (i + 1) % 10 == 0: |
| 104 | + print(f"Connected {i + 1} clients...") |
| 105 | + except Exception as e: |
| 106 | + print(f"Failed to connect client {i}: {e}") |
| 107 | + break |
| 108 | + |
| 109 | + duration = time.time() - start_time |
| 110 | + print(f"Successfully connected {len(clients)} clients in {duration:.2f}s") |
| 111 | + |
| 112 | + server_pid = None |
| 113 | + for proc in psutil.process_iter(['name']): |
| 114 | + if proc.info['name'] == 'mqtt-server': |
| 115 | + server_pid = proc.pid |
| 116 | + break |
| 117 | + |
| 118 | + if server_pid: |
| 119 | + process = psutil.Process(server_pid) |
| 120 | + mem_info = process.memory_info() |
| 121 | + print(f"Server RSS Memory: {mem_info.rss / 1024 / 1024:.2f} MB") |
| 122 | + else: |
| 123 | + print("Could not find mqtt-server process for memory measurement.") |
| 124 | + |
| 125 | + # Keep alive for a bit |
| 126 | + await asyncio.sleep(2) |
| 127 | + |
| 128 | + # Cleanup |
| 129 | + print("Disconnecting clients...") |
| 130 | + for client in clients: |
| 131 | + await client.disconnect() |
| 132 | + |
| 133 | + return len(clients) |
| 134 | + |
| 135 | +async def benchmark_latency(warmup=5, trials=50): |
| 136 | + print(f"--- Latency Test: {trials} trials ---") |
| 137 | + sub = SimpleMQTTClient(client_id="bench-sub") |
| 138 | + pub = SimpleMQTTClient(client_id="bench-pub") |
| 139 | + await sub.connect() |
| 140 | + await pub.connect() |
| 141 | + |
| 142 | + topic = "bench/latency" |
| 143 | + await sub.subscribe(topic) |
| 144 | + |
| 145 | + # Warmup |
| 146 | + for _ in range(warmup): |
| 147 | + await pub.publish(topic, "warmup") |
| 148 | + await sub.wait_for_message() |
| 149 | + |
| 150 | + latencies = [] |
| 151 | + for i in range(trials): |
| 152 | + msg = f"ping-{i}" |
| 153 | + start = time.perf_counter() |
| 154 | + await pub.publish(topic, msg) |
| 155 | + received = await sub.wait_for_message() |
| 156 | + end = time.perf_counter() |
| 157 | + |
| 158 | + if received == msg: |
| 159 | + latencies.append((end - start) * 1000) # Convert to ms |
| 160 | + else: |
| 161 | + print(f"Unexpected message received: {received}") |
| 162 | + |
| 163 | + latencies.sort() |
| 164 | + p50 = latencies[len(latencies)//2] |
| 165 | + p99 = latencies[int(len(latencies)*0.99)] |
| 166 | + avg = sum(latencies) / len(latencies) |
| 167 | + |
| 168 | + print(f"Latency (ms): Avg={avg:.2f}, P50={p50:.2f}, P99={p99:.2f}") |
| 169 | + |
| 170 | + await sub.disconnect() |
| 171 | + await pub.disconnect() |
| 172 | + return p50, p99 |
| 173 | + |
| 174 | +async def main(): |
| 175 | + # Wait for server to be ready (caller should start the server) |
| 176 | + await asyncio.sleep(1) |
| 177 | + |
| 178 | + conn_count = await benchmark_concurrency(100) |
| 179 | + p50, p99 = await benchmark_latency(trials=100) |
| 180 | + |
| 181 | + results = { |
| 182 | + "concurrent_connections": conn_count, |
| 183 | + "latency_p50_ms": p50, |
| 184 | + "latency_p99_ms": p99 |
| 185 | + } |
| 186 | + |
| 187 | + with open("benchmarks/verified_results.json", "w") as f: |
| 188 | + json.dump(results, f, indent=4) |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + asyncio.run(main()) |
0 commit comments