Performance is 10x worse on Anvil as compared to the same code executed on uplink
I recently came across the interesting fact that the same code executed on a connected uplink (e.g. my Laptop) runs 10 times faster than on Anvil’s full server.
Result of comparison
The following screenshot shows the execution times for generating the same plot on Anvil’s full server (~ 5s) and on the connected uplink (~0.5s).
Benchmarking against local creation of the plot
Creating the plot locally on my Laptop in a Jupyter Notebook also requires only 0.5s.
Would be interested to hear whether Anvil is that slow for you, too, or whether this applies only to me.
As I am on a professional plan I’d expect a performance that is at least on par with my Laptop, but not 10x worse. But maybe I am expecting too much?
App for performance comparison (using my uplink)
Clone link - if you want to run the performance comparison with your own uplink
To run your own uplink for the copied app
- Clone your copy of the app to a local directory on the machine your uplink will run on
- Copy the uplink key of your app and substitute it in the below steps for <your uplink key> (I assume bash on Linux here)
# pip install pandas plotly # uncomment if not installed yet
export ANVIL_UPLINK_KEY=<your uplink key>
Do you have the persistent server enabled?
Can you try to print timestamps after each line and see which line is the slowest?
No, this is only available on the Business Plan
It’s worth doing as @stefano.menci suggests but my bet would be that most of the time is spent in your server starting up.
I think @owen.campbell is on the money - The first run using Persistent Server required 8 seconds (its a long way to the UK from here) but each subsequent run only took 1 - 2 seconds
Thanks @rickhurlbatt, do I understand it correctly that you ran a clone of my app with Persistent Server set to True?
Yep I sure did
Bear in mind that I have a fair over head for the round trip to the UK infrastructure (~approx 120 - 200ms each way)
ok, this indeed means that it is mostly startup time. But I thought the server/container is usually available for some time after it is started so that it would still be available for subsequent requests .
No. Without the persistent server, it starts for each and every call.
ok, then it seems I need a persistent server or I need to run the entire backend on my own instance.
That’s a long startup time. It might be worth exploring why that’s the case.
Do you have some big imports or code running at module level somewhere?
I only import pandas and plotly in the server module. My impression is that the compute resources of the server that is started are not very high.
Actually, I misread your first post. That’s not an overly excessive time at all. Sorry!