-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_huffman_benchmark.py
More file actions
88 lines (71 loc) · 2.92 KB
/
test_huffman_benchmark.py
File metadata and controls
88 lines (71 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from __future__ import annotations
from hpack import HPACKDecodingError
from hpack.huffman import HuffmanEncoder
from hpack.huffman_constants import REQUEST_CODES, REQUEST_CODES_LENGTH
from hpack.huffman_table import decode_huffman
from concurrent.futures import ThreadPoolExecutor
import pytest
class TestHuffmanEncoderBenchmark:
@pytest.mark.benchmark
def test_request_huffman_encode(self):
encoder = HuffmanEncoder(REQUEST_CODES, REQUEST_CODES_LENGTH)
assert (
encoder.encode(b"www.example.com") ==
b'\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff'
)
assert encoder.encode(b"no-cache") == b'\xa8\xeb\x10d\x9c\xbf'
assert encoder.encode(b"custom-key") == b'%\xa8I\xe9[\xa9}\x7f'
assert (
encoder.encode(b"custom-value") == b'%\xa8I\xe9[\xb8\xe8\xb4\xbf'
)
@pytest.mark.benchmark
def test_request_huffman_encoder_under_heavy_threading(self):
INPUTS = [
b"www.example.com",
b"no-cache",
b"custom-key",
b"custom-value"
] * 50 # 200 Entries to simulate heavy traffic
ANSWERS = {
b'\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff',
b'\xa8\xeb\x10d\x9c\xbf',
b'%\xa8I\xe9[\xa9}\x7f',
b'%\xa8I\xe9[\xb8\xe8\xb4\xbf'
}
encoder = HuffmanEncoder(REQUEST_CODES, REQUEST_CODES_LENGTH)
with ThreadPoolExecutor(2) as te:
for answer in te.map(encoder.encode, INPUTS):
assert answer in ANSWERS
class TestHuffmanDecoderBenchmark:
# 3.13t cannot use pytest-codspeed due to cffi-2.0
# so using markers was the best solution
@pytest.mark.benchmark
def test_request_huffman_decoder(self):
assert (
decode_huffman(b'\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff') ==
b"www.example.com"
)
assert decode_huffman(b'\xa8\xeb\x10d\x9c\xbf') == b"no-cache"
assert decode_huffman(b'%\xa8I\xe9[\xa9}\x7f') == b"custom-key"
assert (
decode_huffman(b'%\xa8I\xe9[\xb8\xe8\xb4\xbf') == b"custom-value"
)
@pytest.mark.benchmark
def test_request_huffman_decoder_under_heavy_threading(self):
# Trying to ensure that huffman decoder is threadsafe and can work under heavy traffic
# SEE: https://github.com/python-hyper/hpack/issues/284
INPUTS = [
b'\xf1\xe3\xc2\xe5\xf2:k\xa0\xab\x90\xf4\xff',
b'\xa8\xeb\x10d\x9c\xbf',
b'%\xa8I\xe9[\xa9}\x7f',
b'%\xa8I\xe9[\xb8\xe8\xb4\xbf'
] * 50 # 200 entries should be enough to simulate heavy loads to rip through
ANSWERS = {
b"www.example.com",
b"no-cache",
b"custom-key",
b"custom-value"
}
with ThreadPoolExecutor(2) as te:
for answer in te.map(decode_huffman, INPUTS):
assert answer in ANSWERS