Benchmarking a streamlit app

Is there any recommended way or tool to benchmark a streamlit app and find the bottlenecks in the code? Is something like pyinstrument usable with streamlit?

1 Like

Hi @thunderbug1, and welcome to the Streamlit community! :balloon::raised_hands:

Using Python profilers (pyinstrument or else) usually works - the output should be displayed in the console - isn’t it the case for you?

Would it be possible to get our hands on the code for a review?

Thanks,
Charly

Thank you for the fast reply,

I’m rather asking myself how I would run it.
Normally streamlit must be run in the form: streamlit run streamlit_server.py
but also pyinstrument, for example, must be run as pyinstrument streamlit_server.py

how can I combine the two (ideally in a way that doesn’t require me to change my code)?

Thanks for the clarification.

To be sure, do you have to specifically do a profiling with PyInstrument?

Best,
Charly

Not necessarily, it’s just a tool that I used in another project where it worked well. If there is another way to benchmark the code easily, then I am happy as well.

The perfect solution of course would be a
streamlit benchmark streamlit_server.py
But I don’t think this exists yet :joy:

ok I figured it out for my usecase. Here what I did in case anybody finds it usefull.

I inserted this into my code:

from pyinstrument import Profiler

profiler = Profiler()
profiler.start()

# code you want to profile

profiler.stop()

profiler.print()

which then prints the usage to the console. This requires code changes but works fine.

However I did find out that the python code was not the bottleneck.
I profiled the performance of the browser by hitting F12 and then making a recording in the performance tab.
There I saw that the function “A.parse” was the culprit (it seemed JSON parsing related) .
I found out that the download link of the datafile caused the file to be sent to the browser every time the page was reloaded. Moving the link to it’s own page solved the issue for me.

1 Like

Nice one @thunderbug1!

Although your code is (expectedly?) spitting out that error in my code:

Am I missing something? :thinking:

Best,
Charly

oh ok, I got this from their documentation here: https://pyinstrument.readthedocs.io/en/latest/guide.html#profile-a-specific-chunk-of-code

possibly you have an old version installed?

1 Like

I had! I’ve reinstalled and it works now! :slight_smile:

Thanks @thunderbug1! :raised_hands:

Charly

1 Like