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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
"""
This module is a scratchpad for general development, testing & debugging
"""
import sys
import time
from pprint import pprint
from django.core.management import BaseCommand
from django.db import connection
from pexp.models import *
num_objects = 1000
def show_queries():
print()
print("QUERIES:", len(connection.queries))
pprint(connection.queries)
print()
connection.queries_log.clear()
###################################################################################
# benchmark wrappers
def print_timing(func, message="", iterations=1):
def wrapper(*arg):
results = []
connection.queries_log.clear()
for i in range(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(
f"{message}{func.func_name:<19}: {median:.0f} ms, "
f"{len(connection.queries) / len(results):d} queries"
)
sys.stdout.flush()
return wrapper
def run_vanilla_any_poly(func, iterations=1):
f = print_timing(func, " ", iterations)
f(NormalModelC)
f = print_timing(func, "poly ", iterations)
f(TestModelC)
###################################################################################
# benchmarks
def bench_create(model):
for i in range(num_objects):
model.objects.create(
field1=f"abc{i}",
field2=f"abcd{i}",
field3=f"abcde{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 range(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 range(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(BaseCommand):
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)
|