| 1 |
"""Benchark parsing WebSocket frames.""" |
| 2 |
|
| 3 |
import subprocess |
| 4 |
import sys |
| 5 |
import timeit |
| 6 |
|
| 7 |
from websockets.extensions.permessage_deflate import PerMessageDeflate |
| 8 |
from websockets.frames import Frame, Opcode |
| 9 |
from websockets.streams import StreamReader |
| 10 |
|
| 11 |
|
| 12 |
# 256kB of text, compressible by about 70%. |
| 13 |
text = subprocess.check_output(["git", "log", "8dd8e410"], text=True) |
| 14 |
|
| 15 |
|
| 16 |
def get_frame(size): |
| 17 |
repeat, remainder = divmod(size, 256 * 1024) |
| 18 |
payload = repeat * text + text[:remainder] |
| 19 |
return Frame(Opcode.TEXT, payload.encode(), True) |
| 20 |
|
| 21 |
|
| 22 |
def parse_frame(data, count, mask, extensions): |
| 23 |
reader = StreamReader() |
| 24 |
for _ in range(count): |
| 25 |
reader.feed_data(data) |
| 26 |
parser = Frame.parse( |
| 27 |
reader.read_exact, |
| 28 |
mask=mask, |
| 29 |
extensions=extensions, |
| 30 |
) |
| 31 |
try: |
| 32 |
next(parser) |
| 33 |
except StopIteration: |
| 34 |
pass |
| 35 |
else: |
| 36 |
assert False, "parser should return frame" |
| 37 |
reader.feed_eof() |
| 38 |
assert reader.at_eof(), "parser should consume all data" |
| 39 |
|
| 40 |
|
| 41 |
def run_benchmark(size, count, compression=False, number=100): |
| 42 |
if compression: |
| 43 |
extensions = [PerMessageDeflate(True, True, 12, 12, {"memLevel": 5})] |
| 44 |
else: |
| 45 |
extensions = [] |
| 46 |
globals = { |
| 47 |
"get_frame": get_frame, |
| 48 |
"parse_frame": parse_frame, |
| 49 |
"extensions": extensions, |
| 50 |
} |
| 51 |
sppf = ( |
| 52 |
min( |
| 53 |
timeit.repeat( |
| 54 |
f"parse_frame(data, {count}, mask=True, extensions=extensions)", |
| 55 |
f"data = get_frame({size})" |
| 56 |
f".serialize(mask=True, extensions=extensions)", |
| 57 |
number=number, |
| 58 |
globals=globals, |
| 59 |
) |
| 60 |
) |
| 61 |
/ number |
| 62 |
/ count |
| 63 |
* 1_000_000 |
| 64 |
) |
| 65 |
cppf = ( |
| 66 |
min( |
| 67 |
timeit.repeat( |
| 68 |
f"parse_frame(data, {count}, mask=False, extensions=extensions)", |
| 69 |
f"data = get_frame({size})" |
| 70 |
f".serialize(mask=False, extensions=extensions)", |
| 71 |
number=number, |
| 72 |
globals=globals, |
| 73 |
) |
| 74 |
) |
| 75 |
/ number |
| 76 |
/ count |
| 77 |
* 1_000_000 |
| 78 |
) |
| 79 |
print(f"{size}\t{compression}\t{sppf:.2f}\t{cppf:.2f}") |
| 80 |
|
| 81 |
|
| 82 |
if __name__ == "__main__": |
| 83 |
print("Sizes are in bytes. Times are in µs per frame.", file=sys.stderr) |
| 84 |
print("Run `tabs -16` for clean output. Pipe stdout to TSV for saving.") |
| 85 |
print(file=sys.stderr) |
| 86 |
|
| 87 |
print("size\tcompression\tserver\tclient") |
| 88 |
run_benchmark(size=8, count=1000, compression=False) |
| 89 |
run_benchmark(size=60, count=1000, compression=False) |
| 90 |
run_benchmark(size=500, count=1000, compression=False) |
| 91 |
run_benchmark(size=4_000, count=1000, compression=False) |
| 92 |
run_benchmark(size=30_000, count=200, compression=False) |
| 93 |
run_benchmark(size=250_000, count=100, compression=False) |
| 94 |
run_benchmark(size=2_000_000, count=20, compression=False) |
| 95 |
|
| 96 |
run_benchmark(size=8, count=1000, compression=True) |
| 97 |
run_benchmark(size=60, count=1000, compression=True) |
| 98 |
run_benchmark(size=500, count=200, compression=True) |
| 99 |
run_benchmark(size=4_000, count=100, compression=True) |
| 100 |
run_benchmark(size=30_000, count=20, compression=True) |
| 101 |
run_benchmark(size=250_000, count=10, compression=True) |