Expensive JSON serialization always run for debug logging
# Description
The `transmission_rpc.client.Client._request()` method [logs the response JSON at the `DEBUG` level](https://github.com/trim21/transmission-rpc/blob/924eb188b7f8e511752dc38a0d5408a947cda29b/transmission_rpc/client.py#L278):
```
self.logger.debug(json.dumps(data, indent=2))
```
Serializing the response JSON is quite expensive, and particularly so for large responses such as the `torrent-get` method for getting all torrents. Unfortunately, done this way, that expensive serialization is run for every request even when that log message isn't emitted.
## Environment
- language version: Python 3.10.14
- app/lib/repo version: 3.4.2
## How to reproduce
Profile code that calls the `transmission_rpc.client.Client.get_torrents()` method and note that the `json.dumps()` call constitutes much of the cumulative call time:
```
2 0.028 0.014 62.821 31.411 client.py:206(_request)
1 0.018 0.018 56.927 56.927 client.py:534(get_torrents)
6 0.144 0.024 34.835 5.806 __init__.py:183(dumps)
```
This example is from a Transmission daemon with thousands of torrents run under `$ python3 -m "cProfile" -s "cumulative"`.
## Expectations
Expensive operations for `DEBUG` logging should not be run when `DEBUG` log messages aren't configured to be printed/emitted.
## Actual result
The expensive JSON serialization is always run regardless of the logger's configuration.
关闭于 2024-08-20 7 条评论