python-kasa/devtools/bench/benchmark.py
Steven B 416d3118bf
Configure mypy to run in virtual environment and fix resulting issues (#989)
For some time I've noticed that my IDE is reporting mypy errors that the
pre-commit hook is not picking up. This is because [mypy
mirror](https://github.com/pre-commit/mirrors-mypy) runs in an isolated
pre-commit environment which does not have dependencies installed and it
enables `--ignore-missing-imports` to avoid errors.

This is [advised against by
mypy](https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker)
for obvious reasons:

> We recommend avoiding --ignore-missing-imports if possible: it’s
equivalent to adding a # type: ignore to all unresolved imports in your
codebase.

This PR configures the mypy pre-commit hook to run in the virtual
environment and addresses the additional errors identified as a result.
It also introduces a minimal mypy config into the `pyproject.toml`

[mypy errors identified without the fixes in this
PR](https://github.com/user-attachments/files/15896693/mypyerrors.txt)
2024-06-19 15:07:59 +02:00

32 lines
930 B
Python

"""Benchmark the new parser against the old parser."""
import json
import timeit
import orjson
from kasa_crypt import decrypt, encrypt
from devtools.bench.utils.data import REQUEST, WIRE_RESPONSE
from devtools.bench.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")