| 1 |
""" |
| 2 |
This module is a scratchpad for general development, testing & debugging |
| 3 |
""" |
| 4 |
|
| 5 |
import sys |
| 6 |
import time |
| 7 |
from pprint import pprint |
| 8 |
|
| 9 |
from django.core.management import BaseCommand |
| 10 |
from django.db import connection |
| 11 |
from pexp.models import * |
| 12 |
|
| 13 |
num_objects = 1000 |
| 14 |
|
| 15 |
|
| 16 |
def show_queries(): |
| 17 |
print() |
| 18 |
print("QUERIES:", len(connection.queries)) |
| 19 |
pprint(connection.queries) |
| 20 |
print() |
| 21 |
connection.queries_log.clear() |
| 22 |
|
| 23 |
|
| 24 |
################################################################################### |
| 25 |
# benchmark wrappers |
| 26 |
|
| 27 |
|
| 28 |
def print_timing(func, message="", iterations=1): |
| 29 |
def wrapper(*arg): |
| 30 |
results = [] |
| 31 |
connection.queries_log.clear() |
| 32 |
for i in range(iterations): |
| 33 |
t1 = time.time() |
| 34 |
x = func(*arg) |
| 35 |
t2 = time.time() |
| 36 |
results.append((t2 - t1) * 1000.0) |
| 37 |
res_sum = 0 |
| 38 |
for r in results: |
| 39 |
res_sum += r |
| 40 |
median = res_sum / len(results) |
| 41 |
print( |
| 42 |
f"{message}{func.func_name:<19}: {median:.0f} ms, " |
| 43 |
f"{len(connection.queries) / len(results):d} queries" |
| 44 |
) |
| 45 |
sys.stdout.flush() |
| 46 |
|
| 47 |
return wrapper |
| 48 |
|
| 49 |
|
| 50 |
def run_vanilla_any_poly(func, iterations=1): |
| 51 |
f = print_timing(func, " ", iterations) |
| 52 |
f(NormalModelC) |
| 53 |
f = print_timing(func, "poly ", iterations) |
| 54 |
f(TestModelC) |
| 55 |
|
| 56 |
|
| 57 |
################################################################################### |
| 58 |
# benchmarks |
| 59 |
|
| 60 |
|
| 61 |
def bench_create(model): |
| 62 |
for i in range(num_objects): |
| 63 |
model.objects.create( |
| 64 |
field1=f"abc{i}", |
| 65 |
field2=f"abcd{i}", |
| 66 |
field3=f"abcde{i}", |
| 67 |
) |
| 68 |
# print 'count:',model.objects.count() |
| 69 |
|
| 70 |
|
| 71 |
def bench_load1(model): |
| 72 |
for o in model.objects.all(): |
| 73 |
pass |
| 74 |
|
| 75 |
|
| 76 |
def bench_load1_short(model): |
| 77 |
for i in range(num_objects / 100): |
| 78 |
for o in model.objects.all()[:100]: |
| 79 |
pass |
| 80 |
|
| 81 |
|
| 82 |
def bench_load2(model): |
| 83 |
for o in model.objects.all(): |
| 84 |
f1 = o.field1 |
| 85 |
f2 = o.field2 |
| 86 |
f3 = o.field3 |
| 87 |
|
| 88 |
|
| 89 |
def bench_load2_short(model): |
| 90 |
for i in range(num_objects / 100): |
| 91 |
for o in model.objects.all()[:100]: |
| 92 |
f1 = o.field1 |
| 93 |
f2 = o.field2 |
| 94 |
f3 = o.field3 |
| 95 |
|
| 96 |
|
| 97 |
def bench_delete(model): |
| 98 |
model.objects.all().delete() |
| 99 |
|
| 100 |
|
| 101 |
################################################################################### |
| 102 |
# Command |
| 103 |
|
| 104 |
|
| 105 |
class Command(BaseCommand): |
| 106 |
help = "" |
| 107 |
|
| 108 |
def handle_noargs(self, **options): |
| 109 |
func_list = [ |
| 110 |
(bench_delete, 1), |
| 111 |
(bench_create, 1), |
| 112 |
(bench_load1, 5), |
| 113 |
(bench_load1_short, 5), |
| 114 |
(bench_load2, 5), |
| 115 |
(bench_load2_short, 5), |
| 116 |
] |
| 117 |
for f, iterations in func_list: |
| 118 |
run_vanilla_any_poly(f, iterations=iterations) |