PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/pexp/management/commands/polybench.py

https://bitbucket.org/bconstantin/django_polymorphic/
Python | 106 lines | 93 code | 7 blank | 6 comment | 0 complexity | e2b7f81a5d83858897061130db1a5b1f MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3This module is a scratchpad for general development, testing & debugging
  4"""
  5
  6from django.core.management.base import NoArgsCommand
  7from django.db.models import connection
  8from pprint import pprint
  9import settings
 10import sys
 11from pexp.models import *
 12
 13num_objects=1000
 14
 15def reset_queries():
 16    connection.queries=[]
 17
 18def show_queries():
 19    print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; reset_queries()
 20
 21import time
 22
 23###################################################################################
 24### benchmark wrappers
 25
 26def print_timing(func, message='', iterations=1):
 27    def wrapper(*arg):
 28        results=[]
 29        reset_queries()
 30        for i in xrange(iterations):
 31            t1 = time.time()
 32            x = func(*arg)
 33            t2 = time.time()
 34            results.append((t2-t1)*1000.0)
 35        res_sum=0
 36        for r in results: res_sum +=r
 37        median = res_sum / len(results)
 38        print '%s%-19s: %.0f ms, %i queries' % (
 39            message,func.func_name,
 40            median,
 41            len(connection.queries)/len(results)
 42            )
 43        sys.stdout.flush()
 44    return wrapper
 45
 46def run_vanilla_any_poly(func, iterations=1):
 47    f=print_timing(func,'     ', iterations)
 48    f(nModelC)
 49    f=print_timing(func,'poly ', iterations)
 50    f(ModelC)
 51
 52
 53###################################################################################
 54### benchmarks
 55
 56def bench_create(model):
 57    for i in xrange(num_objects):
 58        model.objects.create(field1='abc'+str(i), field2='abcd'+str(i), field3='abcde'+str(i))
 59    #print 'count:',model.objects.count()
 60
 61def bench_load1(model):
 62    for o in model.objects.all():
 63        pass
 64
 65def bench_load1_short(model):
 66    for i in xrange(num_objects/100):
 67        for o in model.objects.all()[:100]:
 68            pass
 69
 70def bench_load2(model):
 71    for o in model.objects.all():
 72        f1=o.field1
 73        f2=o.field2
 74        f3=o.field3
 75
 76def bench_load2_short(model):
 77    for i in xrange(num_objects/100):
 78        for o in model.objects.all()[:100]:
 79            f1=o.field1
 80            f2=o.field2
 81            f3=o.field3
 82
 83def bench_delete(model):
 84    model.objects.all().delete()
 85
 86###################################################################################
 87### Command
 88
 89class Command(NoArgsCommand):
 90    help = ""
 91
 92    def handle_noargs(self, **options):
 93        print 'polybench - sqlite test db is stored in:',settings.SQLITE_DB_PATH
 94
 95        func_list = [
 96            ( bench_delete, 1 ),
 97            ( bench_create, 1 ),
 98            ( bench_load1,  5 ),
 99            ( bench_load1_short, 5 ),
100            ( bench_load2, 5 ),
101            ( bench_load2_short, 5 )
102            ]
103        for f,iterations in func_list:
104            run_vanilla_any_poly(f,iterations=iterations)
105
106        print