Skip to content

Commit 1570cad

Browse files
committed
Balance encode and decodes, remove validate=True, cover str input.
per review from Serhiy.
1 parent 3b95408 commit 1570cad

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

pyperformance/data-files/benchmarks/bm_base64/run_benchmark.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,68 @@
22
33
Tests encoding and decoding performance across various variants
44
and data sizes, split into _small (balanced small data) and _large variants.
5+
6+
Small weighs towards measuring overhead, large measures the core algorithm
7+
loop implementation.
58
"""
69

710
import base64
811
import random
912
import pyperf
1013

1114

12-
# Generate test data with fixed seed for reproducibility
15+
# Generate test data with a fixed seed for reproducibility
1316
random.seed(12345)
14-
DATA_TINY = bytes(random.randrange(256) for _ in range(20))
15-
DATA_SMALL = bytes(random.randrange(256) for _ in range(127)) # odd on purpose
16-
DATA_MEDIUM = bytes(random.randrange(256) for _ in range(3072))
17-
DATA_9K = bytes(random.randrange(256) for _ in range(9000))
18-
DATA_LARGE = bytes(random.randrange(256) for _ in range(102400))
19-
DATA_HUGE = bytes(random.randrange(256) for _ in range(1048576))
17+
DATA_TINY = random.randbytes(20)
18+
DATA_SMALL = random.randbytes(127) # odd on purpose
19+
DATA_MEDIUM = random.randbytes(3072)
20+
DATA_9K = random.randbytes(9000)
21+
DATA_LARGE = random.randbytes(102400)
22+
DATA_HUGE = random.randbytes(1048574) # 1M-2
2023

2124
# Pre-encoded data for decode benchmarks
2225
B64_TINY = base64.b64encode(DATA_TINY)
2326
B64_SMALL = base64.b64encode(DATA_SMALL)
2427
B64_MEDIUM = base64.b64encode(DATA_MEDIUM)
25-
B64_9K = base64.b64encode(DATA_9K)
28+
B64_9K_STR = base64.b64encode(DATA_9K).decode('ascii')
2629
B64_LARGE = base64.b64encode(DATA_LARGE)
2730
B64_HUGE = base64.b64encode(DATA_HUGE)
2831

2932
B64_URLSAFE_TINY = base64.urlsafe_b64encode(DATA_TINY)
3033
B64_URLSAFE_SMALL = base64.urlsafe_b64encode(DATA_SMALL)
3134
B64_URLSAFE_MEDIUM = base64.urlsafe_b64encode(DATA_MEDIUM)
32-
B64_URLSAFE_9K = base64.urlsafe_b64encode(DATA_9K)
35+
B64_URLSAFE_9K_STR = base64.urlsafe_b64encode(DATA_9K).decode('ascii')
3336

3437
B32_TINY = base64.b32encode(DATA_TINY)
3538
B32_SMALL = base64.b32encode(DATA_SMALL)
3639
B32_MEDIUM = base64.b32encode(DATA_MEDIUM)
37-
B32_9K = base64.b32encode(DATA_9K)
40+
B32_9K_STR = base64.b32encode(DATA_9K).decode('ascii')
3841
B32_LARGE = base64.b32encode(DATA_LARGE)
3942
B32_HUGE = base64.b32encode(DATA_HUGE)
4043

4144
B16_TINY = base64.b16encode(DATA_TINY)
4245
B16_SMALL = base64.b16encode(DATA_SMALL)
4346
B16_MEDIUM = base64.b16encode(DATA_MEDIUM)
44-
B16_9K = base64.b16encode(DATA_9K)
47+
B16_9K_STR = base64.b16encode(DATA_9K).decode('ascii')
4548
B16_LARGE = base64.b16encode(DATA_LARGE)
4649
B16_HUGE = base64.b16encode(DATA_HUGE)
4750

4851
A85_TINY = base64.a85encode(DATA_TINY)
4952
A85_SMALL = base64.a85encode(DATA_SMALL)
5053
A85_MEDIUM = base64.a85encode(DATA_MEDIUM)
51-
A85_9K = base64.a85encode(DATA_9K)
54+
A85_9K_STR = base64.a85encode(DATA_9K).decode('ascii')
5255
A85_LARGE = base64.a85encode(DATA_LARGE)
5356
A85_HUGE = base64.a85encode(DATA_HUGE)
5457

5558
B85_TINY = base64.b85encode(DATA_TINY)
5659
B85_SMALL = base64.b85encode(DATA_SMALL)
5760
B85_MEDIUM = base64.b85encode(DATA_MEDIUM)
58-
B85_9K = base64.b85encode(DATA_9K)
61+
B85_9K_STR = base64.b85encode(DATA_9K).decode('ascii')
5962
B85_LARGE = base64.b85encode(DATA_LARGE)
6063
B85_HUGE = base64.b85encode(DATA_HUGE)
6164

6265

63-
# --- Base64 (includes validate=True) ---
66+
# --- Base64 ---
6467

6568
def bench_b64_small(loops):
6669
range_it = range(loops)
@@ -69,18 +72,14 @@ def bench_b64_small(loops):
6972
for _ in range(450):
7073
base64.b64encode(DATA_TINY)
7174
base64.b64decode(B64_TINY)
72-
base64.b64decode(B64_TINY, validate=True)
7375
for _ in range(71):
7476
base64.b64encode(DATA_SMALL)
7577
base64.b64decode(B64_SMALL)
76-
base64.b64decode(B64_SMALL, validate=True)
7778
for _ in range(3):
7879
base64.b64encode(DATA_MEDIUM)
7980
base64.b64decode(B64_MEDIUM)
80-
base64.b64decode(B64_MEDIUM, validate=True)
8181
base64.b64encode(DATA_9K)
82-
base64.b64decode(B64_9K)
83-
base64.b64decode(B64_9K, validate=True)
82+
base64.b64decode(B64_9K_STR)
8483
return pyperf.perf_counter() - t0
8584

8685

@@ -91,10 +90,8 @@ def bench_b64_large(loops):
9190
for _ in range(10):
9291
base64.b64encode(DATA_LARGE)
9392
base64.b64decode(B64_LARGE)
94-
base64.b64decode(B64_LARGE, validate=True)
9593
base64.b64encode(DATA_HUGE)
9694
base64.b64decode(B64_HUGE)
97-
base64.b64decode(B64_HUGE, validate=True)
9895
return pyperf.perf_counter() - t0
9996

10097

@@ -114,7 +111,7 @@ def bench_urlsafe_b64_small(loops):
114111
base64.urlsafe_b64encode(DATA_MEDIUM)
115112
base64.urlsafe_b64decode(B64_URLSAFE_MEDIUM)
116113
base64.urlsafe_b64encode(DATA_9K)
117-
base64.urlsafe_b64decode(B64_URLSAFE_9K)
114+
base64.urlsafe_b64decode(B64_URLSAFE_9K_STR)
118115
return pyperf.perf_counter() - t0
119116

120117

@@ -134,7 +131,7 @@ def bench_b32_small(loops):
134131
base64.b32encode(DATA_MEDIUM)
135132
base64.b32decode(B32_MEDIUM)
136133
base64.b32encode(DATA_9K)
137-
base64.b32decode(B32_9K)
134+
base64.b32decode(B32_9K_STR)
138135
return pyperf.perf_counter() - t0
139136

140137

@@ -166,7 +163,7 @@ def bench_b16_small(loops):
166163
base64.b16encode(DATA_MEDIUM)
167164
base64.b16decode(B16_MEDIUM)
168165
base64.b16encode(DATA_9K)
169-
base64.b16decode(B16_9K)
166+
base64.b16decode(B16_9K_STR)
170167
return pyperf.perf_counter() - t0
171168

172169

@@ -192,17 +189,21 @@ def bench_a85_small(loops):
192189
base64.a85encode(DATA_TINY)
193190
base64.a85encode(DATA_TINY, wrapcol=76)
194191
base64.a85decode(A85_TINY)
192+
base64.a85decode(A85_TINY) # balance enc+dec weight
195193
for _ in range(71):
196194
base64.a85encode(DATA_SMALL)
197195
base64.a85encode(DATA_SMALL, wrapcol=76)
198196
base64.a85decode(A85_SMALL)
197+
base64.a85decode(A85_SMALL) # balance enc+dec weight
199198
for _ in range(3):
200199
base64.a85encode(DATA_MEDIUM)
201200
base64.a85encode(DATA_MEDIUM, wrapcol=76)
202201
base64.a85decode(A85_MEDIUM)
202+
base64.a85decode(A85_MEDIUM) # balance enc+dec weight
203203
base64.a85encode(DATA_9K)
204204
base64.a85encode(DATA_9K, wrapcol=76)
205-
base64.a85decode(A85_9K)
205+
base64.a85decode(A85_9K_STR)
206+
base64.a85decode(A85_9K_STR) # balance enc+dec weight
206207
return pyperf.perf_counter() - t0
207208

208209

@@ -214,9 +215,11 @@ def bench_a85_large(loops):
214215
base64.a85encode(DATA_LARGE)
215216
base64.a85encode(DATA_LARGE, wrapcol=76)
216217
base64.a85decode(A85_LARGE)
218+
base64.a85decode(A85_LARGE) # balance enc+dec weight
217219
base64.a85encode(DATA_HUGE)
218220
base64.a85encode(DATA_HUGE, wrapcol=76)
219221
base64.a85decode(A85_HUGE)
222+
base64.a85decode(A85_HUGE) # balance enc+dec weight
220223
return pyperf.perf_counter() - t0
221224

222225

@@ -236,7 +239,7 @@ def bench_b85_small(loops):
236239
base64.b85encode(DATA_MEDIUM)
237240
base64.b85decode(B85_MEDIUM)
238241
base64.b85encode(DATA_9K)
239-
base64.b85decode(B85_9K)
242+
base64.b85decode(B85_9K_STR)
240243
return pyperf.perf_counter() - t0
241244

242245

0 commit comments

Comments
 (0)