|
| 1 | +import random |
| 2 | +import time |
| 3 | +from typing import List |
| 4 | + |
| 5 | +import wsproto |
| 6 | + |
| 7 | +random_seed = 0 |
| 8 | +mu = 125 * 1024 |
| 9 | +sigma = 75 * 1024 |
| 10 | +iterations = 5000 |
| 11 | +per_message_deflate = False |
| 12 | + |
| 13 | + |
| 14 | +rand = random.Random(random_seed) |
| 15 | + |
| 16 | + |
| 17 | +client_extensions: List[wsproto.extensions.Extension] = [] |
| 18 | +if per_message_deflate: |
| 19 | + pmd = wsproto.extensions.PerMessageDeflate() |
| 20 | + offer = pmd.offer() |
| 21 | + assert isinstance(offer, str) |
| 22 | + pmd.finalize(offer) |
| 23 | + client_extensions.append(pmd) |
| 24 | +client = wsproto.connection.Connection( |
| 25 | + wsproto.ConnectionType.CLIENT, |
| 26 | + extensions=client_extensions, |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +server_extensions: List[wsproto.extensions.Extension] = [] |
| 31 | +if per_message_deflate: |
| 32 | + pmd = wsproto.extensions.PerMessageDeflate() |
| 33 | + offer = pmd.offer() |
| 34 | + assert isinstance(offer, str) |
| 35 | + pmd.accept(offer) |
| 36 | +server = wsproto.connection.Connection( |
| 37 | + wsproto.ConnectionType.SERVER, |
| 38 | + extensions=server_extensions, |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +start = time.perf_counter() |
| 43 | +for i in range(iterations): |
| 44 | + client_msg = b"0" * max(0, round(rand.gauss(mu, sigma))) |
| 45 | + client_out = client.send(wsproto.events.BytesMessage(client_msg)) |
| 46 | + server.receive_data(client_out) |
| 47 | + for event in server.events(): |
| 48 | + pass |
| 49 | + |
| 50 | + server_msg = "0" * max(0, round(rand.gauss(mu, sigma))) |
| 51 | + server_out = server.send(wsproto.events.TextMessage(server_msg)) |
| 52 | + client.receive_data(server_out) |
| 53 | + for event in client.events(): |
| 54 | + pass |
| 55 | +end = time.perf_counter() |
| 56 | + |
| 57 | +print(f"{end - start:.4f}s") |
0 commit comments