mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-10-12 10:28:01 +00:00
Add benchmarks for speedups (#473)
* Add benchmarks for speedups * Update README.md * Update README.md Co-authored-by: Teemu R. <tpr@iki.fi> * relo * Update README.md * document benchmark * Update README.md --------- Co-authored-by: Teemu R. <tpr@iki.fi>
This commit is contained in:
30
devtools/bench/benchmark.py
Normal file
30
devtools/bench/benchmark.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Benchmark the new parser against the old parser."""
|
||||
|
||||
import json
|
||||
import timeit
|
||||
|
||||
import orjson
|
||||
from kasa_crypt import decrypt, encrypt
|
||||
from utils.data import REQUEST, WIRE_RESPONSE
|
||||
from utils.original import OriginalTPLinkSmartHomeProtocol
|
||||
|
||||
|
||||
def original_request_response() -> None:
|
||||
"""Benchmark the original parser."""
|
||||
OriginalTPLinkSmartHomeProtocol.encrypt(json.dumps(REQUEST))
|
||||
json.loads(OriginalTPLinkSmartHomeProtocol.decrypt(WIRE_RESPONSE[4:]))
|
||||
|
||||
|
||||
def new_request_response() -> None:
|
||||
"""Benchmark the new parser."""
|
||||
encrypt(orjson.dumps(REQUEST).decode())
|
||||
orjson.loads(decrypt(WIRE_RESPONSE[4:]))
|
||||
|
||||
|
||||
count = 100000
|
||||
|
||||
time = timeit.Timer(new_request_response).timeit(count)
|
||||
print(f"New parser, parsing {count} messages took {time} seconds")
|
||||
|
||||
time = timeit.Timer(original_request_response).timeit(count)
|
||||
print(f"Old parser, parsing {count} messages took {time} seconds")
|
Reference in New Issue
Block a user