adamchainz / django-mysql

:dolphin: :horse: Extensions to Django for use with MySQL/MariaDB
https://django-mysql.readthedocs.io/
MIT License
568 stars 110 forks source link

Lazily import django.test to reduce import time #1084

Closed angusholder closed 4 months ago

angusholder commented 4 months ago

I was running a flamegraph on our manage.py, and noticed this import of django.test using 20-40ms. Seems like an easy win for startup time to make it a lazy import, because most production code wouldn't normally ever need the django.test module.

adamchainz commented 4 months ago

Thanks, that is an easy win. I added a changelog note and improved the comment.

This function could be improved further to not use django.test and instead capture the query with database instrumentation, if you’re up for the challenge.

angusholder commented 4 months ago

After sending this, I realised it was your blog posts I read recently about improving Django start time, so thanks for those!

Might have a go at that database instrumentation change later, I haven't seen it before, looks interesting.

adamchainz commented 4 months ago

Glad you enjoyed them!

I also have some blog posts on database instrumentation that may inform: https://duckduckgo.com/?t=ffab&q=site%3Aadamj.eu+%22database+instrumentation%22&ia=web

The suggestion is just an “if you’re interested”, I don't use pt_visual_explain myself, at the moment, and I guess you don’t either :)