diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-06-03 16:25:06 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-06-03 16:34:13 -0500 |
commit | 0d9f81b104f7e15ef5cb33f794ce746738fcaf24 (patch) | |
tree | a6492f23a0f72d390bd6904915ac7cf2f30107c4 /examples | |
parent | 86ee92effd3b078f2df0b6bfd3b9ddca60a7801e (diff) | |
download | requests-cache-0d9f81b104f7e15ef5cb33f794ce746738fcaf24.tar.gz |
Move performance test to examples so it doesn't get picked up by pytest
Diffstat (limited to 'examples')
-rw-r--r-- | examples/performance_test.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/examples/performance_test.py b/examples/performance_test.py new file mode 100644 index 0000000..b3dc1ef --- /dev/null +++ b/examples/performance_test.py @@ -0,0 +1,85 @@ +"""A manual test to compare performance of different serializers""" +# flake8: noqa: F401 +""" +CPU Results: +jsonpickle.encode x10000: 5.673 +jsonpickle.decode x10000: 5.448 +pickle.dumps x10000: 0.256 +pickle.loads x10000: 0.260 +cattrs.unstructure x10000: 0.002 +cattrs.structure x10000: 0.002 +cattrs + pickle.dumps x10000: 0.251 +cattrs + pickle.loads x10000: 0.253 +""" +import pickle +from time import perf_counter as time + +import jsonpickle +from memory_profiler import profile +from rich import print + +from requests_cache import CachedSession +from requests_cache.serializers import PickleSerializer + +ITERATIONS = 10000 + +session = CachedSession() +session.cache.clear() +r = session.get('https://httpbin.org/get?x=y') + + +def test_jsonpickle(): + start = time() + serialized = [jsonpickle.encode(r, use_base85=True) for i in range(ITERATIONS)] + print(f'jsonpickle.encode x{ITERATIONS}: {time() - start:.3f}') + + start = time() + deserialized = [jsonpickle.decode(obj) for obj in serialized] + print(f'jsonpickle.decode x{ITERATIONS}: {time() - start:.3f}') + + +def test_pickle(): + start = time() + serialized = [pickle.dumps(r) for i in range(ITERATIONS)] + print(f'pickle.dumps x{ITERATIONS}: {time() - start:.3f}') + + start = time() + serialized = [pickle.dumps(r) for i in range(ITERATIONS)] + print(f'pickle.loads x{ITERATIONS}: {time() - start:.3f}') + + +def test_cattrs(): + s = PickleSerializer() + start = time() + serialized = [s.unstructure(r) for i in range(ITERATIONS)] + print(f'cattrs.unstructure x{ITERATIONS}: {time() - start:.3f}') + + start = time() + deserialized = [s.structure(obj) for obj in serialized] + print(f'cattrs.structure x{ITERATIONS}: {time() - start:.3f}') + + +def test_cattrs_pickle(): + s = PickleSerializer() + start = time() + serialized = [s.dumps(r) for i in range(ITERATIONS)] + print(f'cattrs + pickle.dumps x{ITERATIONS}: {time() - start:.3f}') + + start = time() + deserialized = [s.loads(obj) for obj in serialized] + print(f'cattrs + pickle.loads x{ITERATIONS}: {time() - start:.3f}') + + +if __name__ == '__main__': + print('CPU:') + test_jsonpickle() + test_pickle() + test_cattrs() + test_cattrs_pickle() + + # Memory + # print('\nMemory:') + # profile(test_jsonpickle)() + # profile(test_pickle)() + # profile(test_cattrs)() + # profile(test_cattrs_pickle)() |