Anubhav
Anubhav

Reputation: 585

Django slow execution of python code (no ORM)

I am seeing massively degraded DRF performance for simple python code when running under an API vs. standalone.

For e.g., here are the benchmarks when running the python code standalone (i.e. outside of Django. Just a plain old script in python). I am loading a user file, converting it into a custom object and running some validations on it. All done in regular python code with no Django dependencies nor any ORM.

xlsname = 'user_file.xlsx'
dash = Dashboard(xlsname,logging.ERROR,create_copy=True)  #loading user file
Load time: 1.828125

dash.validate() #validate user file
Validation time: 0.203125

Now when running the same code under a simple DRF function view:

#views.py

@api_view(['GET'])
def load_and_validate(request):
    xlsname = 'user_file.xlsx'
    dash = Dashboard(xlsname, logging.DEBUG, create_copy=True)
    dash.validate()
    return Response({"message": "{} loaded".format(xlsname)})

My response times are:

Load time: 1.96875 #this is in line with standalone execution
**Validation time: 5.21875 # this is 2500x slower than standalone!**

The validation time for my file is now 2500x slower than standalone!

I am not even using any ORM yet so there are no queries. Everything is loaded into the memory and executed from there.

What may be causing this and how can I diagnose it?

Upvotes: 0

Views: 43

Answers (1)

Anubhav
Anubhav

Reputation: 585

User error. I was running the DRF case under logging.DEBUG instead of logging.ERROR.

Upvotes: 1

Related Questions