Is there a library to benchmark my Django App for

2019-06-06 16:49发布

问题:

I have a large complex Django App that we use that does lots of things. From looking at the Django Debug Toolbar some of our views do a lot of SQL requests. I want to increase the performance of it by reducing the number of SQL requests (e.g. by adding more select_related, more clever queries, etc.). I would like to be able to measure the improvement as I go along, to encourage me and to be able to say how much fat has been trimmed. I have a large set of django unit tests for this app that touch on lots of parts of the code.

Is there some library/programme/script can run the unittests and then print out how many SQL requests in total were executed? This way I can iterativly improve our app.

回答1:

This information is only available in debug mode. I'm not aware of any library to do exactly what you are trying to do, however you could probably rig something up pretty easily.

If you are in running in debug mode, then you can view all the queries that have been run with connection.queries. This is effectively how django debug toolbar works as well.

So alter your unittests to look at the connection.queries dict and that should get you started pretty well.