File: polybench.py

package info (click to toggle)
django-polymorphic 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 436 kB
  • ctags: 586
  • sloc: python: 2,208; makefile: 142
file content (103 lines) | stat: -rw-r--r-- 2,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
"""
This module is a scratchpad for general development, testing & debugging
"""

from django.core.management.base import NoArgsCommand
from django.db.models import connection
from pprint import pprint
import sys
from pexp.models import *

num_objects=1000

def reset_queries():
    connection.queries=[]

def show_queries():
    print; print 'QUERIES:',len(connection.queries); pprint(connection.queries); print; reset_queries()

import time

###################################################################################
### benchmark wrappers

def print_timing(func, message='', iterations=1):
    def wrapper(*arg):
        results=[]
        reset_queries()
        for i in xrange(iterations):
            t1 = time.time()
            x = func(*arg)
            t2 = time.time()
            results.append((t2-t1)*1000.0)
        res_sum=0
        for r in results: res_sum +=r
        median = res_sum / len(results)
        print '%s%-19s: %.0f ms, %i queries' % (
            message,func.func_name,
            median,
            len(connection.queries)/len(results)
            )
        sys.stdout.flush()
    return wrapper

def run_vanilla_any_poly(func, iterations=1):
    f=print_timing(func,'     ', iterations)
    f(nModelC)
    f=print_timing(func,'poly ', iterations)
    f(ModelC)


###################################################################################
### benchmarks

def bench_create(model):
    for i in xrange(num_objects):
        model.objects.create(field1='abc'+str(i), field2='abcd'+str(i), field3='abcde'+str(i))
    #print 'count:',model.objects.count()

def bench_load1(model):
    for o in model.objects.all():
        pass

def bench_load1_short(model):
    for i in xrange(num_objects/100):
        for o in model.objects.all()[:100]:
            pass

def bench_load2(model):
    for o in model.objects.all():
        f1=o.field1
        f2=o.field2
        f3=o.field3

def bench_load2_short(model):
    for i in xrange(num_objects/100):
        for o in model.objects.all()[:100]:
            f1=o.field1
            f2=o.field2
            f3=o.field3

def bench_delete(model):
    model.objects.all().delete()

###################################################################################
### Command

class Command(NoArgsCommand):
    help = ""

    def handle_noargs(self, **options):
        func_list = [
            ( bench_delete, 1 ),
            ( bench_create, 1 ),
            ( bench_load1,  5 ),
            ( bench_load1_short, 5 ),
            ( bench_load2, 5 ),
            ( bench_load2_short, 5 )
            ]
        for f,iterations in func_list:
            run_vanilla_any_poly(f,iterations=iterations)

        print