r/Python • • 17m ago

News Benchmarking python-json-logger: logging exceptions is slowwwww (and v4.3.0rc1 release)

Hi r/python 👋 - maintainer of python-json-logger here.

In 2024, shortly after taking over maintenance of python-json-logger, I introduced support for using the high performance JSON encoders: Orjson and MsgSpec (v3.1.0 - ignore the wrong date will be updated on the next final release). Although there are many benchmarks available for these encoders, it wasn't until recently that I got the time (and tokens) to introduce some proper benchmarking. The full report is available here and includes pretty graphs.

The TLDR is that depending on workload you can expect a 2-5x speedup by using the high performance JSON encoders with CPython. This still significantly beats PyPy which at best expects a 2x speedup (note: neither Orjson or MsgSpec support PyPy).

The most surpsing result was for the formatting of exc_info. Although serialisation could enjoy an aproximate 7-8x speedup, serialisation is actually only a small part of the total time spent formatting log records with exc_info.

Using the results of Python 3.15 as an example: it turns out generating the exception info is really expensive - like ~20x more expensive than a simple log record. The total time spent formatting a simple record is ~2.2 microseconds comapred with a record with a exc_info which is ~44 microseconds and of which only ~1.6 microseconds is spent in serialisation.

So Amdahl's Law kicks in and we get almost no overall speedup. I'm also pretty sure this is the first time I've thought about Amdahls Law since university 🤔.

The exc_info case is also the only time that PyPy actually beats the high performance JSON encoders, not because it's encoding faster but (presumably) because it can produce the information faster.

As I'm writing this it also looks like the creation of exc_info has also aproximately doubled b CPython 3.10 (presumably due to all the extra info that is now included in tracebacks, but I've done no analysis on that, nor do I intend to).

-------------------

I've also just uploaded v4.3.0rc1 (change log) which includes a 5-10% performance improvement amongst many other changes and fixes.

This will also be the first release to use Trusted Publishing 🎉

2 Upvotes

0 comments sorted by