sumerc / yappi

Yet Another Python Profiler, but this time multithreading, asyncio and gevent aware.
MIT License
1.41k stars 73 forks source link

Question: does yappi add "considerable runtime overhead"? Can it be used in production? #132

Closed m-roberts closed 7 months ago

m-roberts commented 1 year ago

According to dmontagu/fastapi-utils,

If you are look for more fine-grained performance profiling data, consider yappi, a python profiling library that was recently updated with coroutine support to enable better coroutine-aware profiling.

Note however that yappi adds considerable runtime overhead, and should typically be used during development rather than production.

So, my question is, is this a fair assessment of yappi, or is it sufficiently performant for production use?

nijave commented 1 year ago

I've been profiling a Python application with 50 threads that makes a bunch of HTTP requests, does some minimal parsing, and stores the results in a SQLite database. I notice going from 30-80% CPU to 100% CPU and dropping from 200-250 requests/sec (the remote HTTP API rate limit is 200 req/sec) to about 120 req/sec. I'd say this is pretty significant but ymmv

sumerc commented 1 year ago

Yappi is a deterministic profiler, meaning it measures every function call/exit. In that sense, it differs from sampling profilers where application is profiled on some intervals. Both approaches have pros/cons but a deterministic profiler can tell you EXACTLY how your code behaves up to a single function call.

@nijave has given some numbers and the overhead might change from application to application. Usually, it performs poorly on very CPU intensive workloads but shines on I/O bound ones. So, you might want to measure it before using it on production.

Having said all above, maybe instead of turning the profiler always ON, maybe you can turn it ON only when you need it? Like, you can write a simple middleware that enable/disable the profiler based on a specific HTTP header when the user is admin? Or only enable profiling 1 out of 10 requests, or specific endpoint, user...etc.

Hope this helps!