| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import collections |
| 4 |
import pathlib |
| 5 |
import sys |
| 6 |
import time |
| 7 |
import zlib |
| 8 |
|
| 9 |
|
| 10 |
REPEAT = 10 |
| 11 |
|
| 12 |
WB, ML = 12, 5 # defaults used as a reference |
| 13 |
WBITS = range(9, 16) |
| 14 |
MEMLEVELS = range(1, 10) |
| 15 |
|
| 16 |
|
| 17 |
def benchmark(data): |
| 18 |
size = collections.defaultdict(dict) |
| 19 |
duration = collections.defaultdict(dict) |
| 20 |
|
| 21 |
for wbits in WBITS: |
| 22 |
for memLevel in MEMLEVELS: |
| 23 |
encoder = zlib.compressobj(wbits=-wbits, memLevel=memLevel) |
| 24 |
encoded = [] |
| 25 |
|
| 26 |
print(f"Compressing {REPEAT} times with {wbits=} and {memLevel=}") |
| 27 |
|
| 28 |
t0 = time.perf_counter() |
| 29 |
|
| 30 |
for _ in range(REPEAT): |
| 31 |
for item in data: |
| 32 |
# Taken from PerMessageDeflate.encode |
| 33 |
item = encoder.compress(item) + encoder.flush(zlib.Z_SYNC_FLUSH) |
| 34 |
if item.endswith(b"\x00\x00\xff\xff"): |
| 35 |
item = item[:-4] |
| 36 |
encoded.append(item) |
| 37 |
|
| 38 |
t1 = time.perf_counter() |
| 39 |
|
| 40 |
size[wbits][memLevel] = sum(len(item) for item in encoded) / REPEAT |
| 41 |
duration[wbits][memLevel] = (t1 - t0) / REPEAT |
| 42 |
|
| 43 |
raw_size = sum(len(item) for item in data) |
| 44 |
|
| 45 |
print("=" * 79) |
| 46 |
print("Compression ratio") |
| 47 |
print("=" * 79) |
| 48 |
print("\t".join(["wb \\ ml"] + [str(memLevel) for memLevel in MEMLEVELS])) |
| 49 |
for wbits in WBITS: |
| 50 |
print( |
| 51 |
"\t".join( |
| 52 |
[str(wbits)] |
| 53 |
+ [ |
| 54 |
f"{100 * (1 - size[wbits][memLevel] / raw_size):.1f}%" |
| 55 |
for memLevel in MEMLEVELS |
| 56 |
] |
| 57 |
) |
| 58 |
) |
| 59 |
print("=" * 79) |
| 60 |
print() |
| 61 |
|
| 62 |
print("=" * 79) |
| 63 |
print("CPU time") |
| 64 |
print("=" * 79) |
| 65 |
print("\t".join(["wb \\ ml"] + [str(memLevel) for memLevel in MEMLEVELS])) |
| 66 |
for wbits in WBITS: |
| 67 |
print( |
| 68 |
"\t".join( |
| 69 |
[str(wbits)] |
| 70 |
+ [ |
| 71 |
f"{1000 * duration[wbits][memLevel]:.1f}ms" |
| 72 |
for memLevel in MEMLEVELS |
| 73 |
] |
| 74 |
) |
| 75 |
) |
| 76 |
print("=" * 79) |
| 77 |
print() |
| 78 |
|
| 79 |
print("=" * 79) |
| 80 |
print(f"Size vs. {WB} \\ {ML}") |
| 81 |
print("=" * 79) |
| 82 |
print("\t".join(["wb \\ ml"] + [str(memLevel) for memLevel in MEMLEVELS])) |
| 83 |
for wbits in WBITS: |
| 84 |
print( |
| 85 |
"\t".join( |
| 86 |
[str(wbits)] |
| 87 |
+ [ |
| 88 |
f"{100 * (size[wbits][memLevel] / size[WB][ML] - 1):.1f}%" |
| 89 |
for memLevel in MEMLEVELS |
| 90 |
] |
| 91 |
) |
| 92 |
) |
| 93 |
print("=" * 79) |
| 94 |
print() |
| 95 |
|
| 96 |
print("=" * 79) |
| 97 |
print(f"Time vs. {WB} \\ {ML}") |
| 98 |
print("=" * 79) |
| 99 |
print("\t".join(["wb \\ ml"] + [str(memLevel) for memLevel in MEMLEVELS])) |
| 100 |
for wbits in WBITS: |
| 101 |
print( |
| 102 |
"\t".join( |
| 103 |
[str(wbits)] |
| 104 |
+ [ |
| 105 |
f"{100 * (duration[wbits][memLevel] / duration[WB][ML] - 1):.1f}%" |
| 106 |
for memLevel in MEMLEVELS |
| 107 |
] |
| 108 |
) |
| 109 |
) |
| 110 |
print("=" * 79) |
| 111 |
print() |
| 112 |
|
| 113 |
|
| 114 |
def main(corpus): |
| 115 |
data = [file.read_bytes() for file in corpus.iterdir()] |
| 116 |
benchmark(data) |
| 117 |
|
| 118 |
|
| 119 |
if __name__ == "__main__": |
| 120 |
if len(sys.argv) < 2: |
| 121 |
print(f"Usage: {sys.argv[0]} [directory]") |
| 122 |
sys.exit(2) |
| 123 |
main(pathlib.Path(sys.argv[1])) |