Skip to content

Instantly share code, notes, and snippets.

@nvllsvm
Last active April 9, 2024 17:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nvllsvm/70ccd9ce5170571ad16e715a79244429 to your computer and use it in GitHub Desktop.
Save nvllsvm/70ccd9ce5170571ad16e715a79244429 to your computer and use it in GitHub Desktop.
Benchmark JSON and MessagePack encoding/decoding times and result sizes
#!/usr/bin/env python3
import json
import gzip
import time
import brotli
import msgpack
import tabulate
import umsgpack
import zstd
DATA = {
'a': 'aardvark',
'map': {str(i): i for i in range(1000)},
'nums': list(range(1000)),
}
ITERATIONS = 500
def benchmark(to_func, from_func):
start = time.time()
for _ in range(ITERATIONS):
serialized = to_func(DATA)
to_seconds = time.time() - start
start = time.time()
for _ in range(ITERATIONS):
from_func(serialized)
from_seconds = time.time() - start
return to_seconds, from_seconds, len(serialized)
def main():
results = []
funcs = {
'json': [
lambda x: json.dumps(x).encode(),
json.loads,
],
'msgpack': [
msgpack.packb,
msgpack.unpackb,
],
'umsgpack': [
umsgpack.packb,
umsgpack.unpackb,
]
}
for name, (to_func, from_func) in funcs.items():
results.append([name, *benchmark(to_func, from_func)])
for c in [brotli, gzip, zstd]:
results.append([
f'{name}-{c.__name__}', *benchmark(
lambda d: c.compress(to_func(d)),
lambda d: from_func(c.decompress(d)))
])
results = [
(r[0], f'{r[1]:.2f}', f'{r[2]:.2f}', r[3])
for r in results
]
results = sorted(results, key=lambda x: x[0])
print(
tabulate.tabulate(
results,
headers=['format', 'to_seconds', 'from_seconds', 'byte size']))
if __name__ == '__main__':
main()
@nvllsvm
Copy link
Author

nvllsvm commented Mar 11, 2020

Results:

format             to_seconds    from_seconds    byte size
---------------  ------------  --------------  -----------
json                     0.17            0.22        16706
json-brotli             18.92            0.3          4294
json-gzip                0.65            0.28         6225
json-zstd                0.25            0.23         4229
msgpack                  0.05            0.13         9149
msgpack-brotli          12.96            0.19         3280
msgpack-gzip             0.2             0.19         5143
msgpack-zstd             0.09            0.15         5517
umsgpack                 1.97            3.22         9149
umsgpack-brotli         14.83            3.32         3280
umsgpack-gzip            2.09            3.55         5143
umsgpack-zstd            2.09            3.26         5517

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment