TheRarestWords | RarestNews | Suggestan | TheCraziestIdeas| SemanticKernelBot | Flim.me | My dev.blog | Йои Хаджи


20th Jul 2008

Testing SQL engines/queries with Django (avg.query time)

I love Django for many reasons and here’s one of them. Testing average time for queries I’ve done today to compare engines (mySQL vs postgreSQL) is easily done with django.

models.py

class Logga(models.Model):
    sum1 = models.FloatField(default=0)
    cnt1 = models.FloatField(default=0)

settings.py

DEBUG = True

somewhere in end of your view or a loop when a few queries are done:

if len(db.connection.queries):
    Logga(sum1=sum(float(q['time']) for q in db.connection.queries), cnt1=len(db.connection.queries)).save()
    db.reset_queries()

All done!

Now, you can issue SQL:

SELECT sum(sum1)/sum(cnt1) FROM yourappname_logga

and you have average time for queries. Try increasing the number of threads, or other queries and see how it affects your average times. Stress testing is good too.

P.S. In case you’ve come here looking for “avg” function (or any other aggregate functions for django - there aren’t any yet - nowhere - django doesn’t support aggregation). And actually you should avoid using aggregate functions if you’re going to build a big site.

This entry was posted on Sunday, July 20th, 2008 at 7:37 pm and is filed under python, site.

Subscribe via RSS: or e-mail (the form in right sidebar).