PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/pexp/management/commands/p2cmd.py

https://bitbucket.org/bconstantin/django_polymorphic/
Python | 110 lines | 100 code | 5 blank | 5 comment | 0 complexity | de370056c22d03e8060a8c58b76ac0f5 MD5 | raw file
  1# -*- coding: utf-8 -*-
  2"""
  3This module is a scratchpad for general development, testing & debugging
  4Well, even more so than pcmd.py. You best ignore p2cmd.py.
  5"""
  6import uuid
  7
  8from django.core.management.base import NoArgsCommand
  9from django.db.models import connection
 10from pprint import pprint
 11import settings
 12import time,sys
 13
 14from pexp.models import *
 15
 16def reset_queries():
 17    connection.queries=[]
 18
 19def show_queries():
 20    print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; connection.queries=[]
 21
 22def print_timing(func, message='', iterations=1):
 23    def wrapper(*arg):
 24        results=[]
 25        reset_queries()
 26        for i in xrange(iterations):
 27            t1 = time.time()
 28            x = func(*arg)
 29            t2 = time.time()
 30            results.append((t2-t1)*1000.0)
 31        res_sum=0
 32        for r in results: res_sum +=r
 33        median = res_sum / len(results)
 34        print '%s%-19s: %.4f ms, %i queries (%i times)' % (
 35            message,func.func_name,
 36            res_sum,
 37            len(connection.queries),
 38            iterations
 39            )
 40        sys.stdout.flush()
 41    return wrapper
 42
 43class Command(NoArgsCommand):
 44    help = ""
 45
 46    def handle_noargs(self, **options):
 47        print 'polycmd - sqlite test db is stored in:',settings.SQLITE_DB_PATH
 48        print
 49
 50        if False:
 51            ModelA.objects.all().delete()
 52            a=ModelA.objects.create(field1='A1')
 53            b=ModelB.objects.create(field1='B1', field2='B2')
 54            c=ModelC.objects.create(field1='C1', field2='C2', field3='C3')
 55            reset_queries()
 56            print ModelC.base_objects.all();
 57            show_queries()
 58
 59        if False:
 60            ModelA.objects.all().delete()
 61            for i in xrange(1000):
 62                a=ModelA.objects.create(field1=str(i%100))
 63                b=ModelB.objects.create(field1=str(i%100), field2=str(i%200))
 64                c=ModelC.objects.create(field1=str(i%100), field2=str(i%200), field3=str(i%300))
 65                if i%100==0: print i
 66
 67        f=print_timing(poly_sql_query,iterations=1000)
 68        f()
 69        
 70        f=print_timing(poly_sql_query2,iterations=1000)
 71        f()
 72
 73        return
 74
 75        nModelA.objects.all().delete()
 76        a=nModelA.objects.create(field1='A1')
 77        b=nModelB.objects.create(field1='B1', field2='B2')
 78        c=nModelC.objects.create(field1='C1', field2='C2', field3='C3')
 79        qs=ModelA.objects.raw("SELECT * from pexp_modela")
 80        for o in list(qs): print o
 81
 82from django.db import connection, transaction
 83from random import Random
 84rnd=Random()
 85
 86def poly_sql_query():
 87    cursor = connection.cursor()
 88    cursor.execute("""
 89        SELECT id, pexp_modela.field1, pexp_modelb.field2, pexp_modelc.field3
 90        FROM pexp_modela
 91        LEFT OUTER JOIN pexp_modelb
 92        ON pexp_modela.id = pexp_modelb.modela_ptr_id
 93        LEFT OUTER JOIN pexp_modelc
 94        ON pexp_modelb.modela_ptr_id = pexp_modelc.modelb_ptr_id
 95        WHERE pexp_modela.field1=%i
 96        ORDER BY pexp_modela.id
 97        """ % rnd.randint(0,100) )
 98    #row=cursor.fetchone()
 99    return
100
101def poly_sql_query2():
102    cursor = connection.cursor()
103    cursor.execute("""
104        SELECT id, pexp_modela.field1
105        FROM pexp_modela
106        WHERE pexp_modela.field1=%i
107        ORDER BY pexp_modela.id
108        """ % rnd.randint(0,100) )
109    #row=cursor.fetchone()
110    return