Reformat all files with isort and black
This commit is contained in:
@@ -7,12 +7,12 @@ import sys
|
||||
import time
|
||||
from pprint import pprint
|
||||
from random import Random
|
||||
|
||||
from django.core.management import BaseCommand
|
||||
from django.db import connection
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
|
||||
rnd = Random()
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ def show_queries():
|
||||
connection.queries = []
|
||||
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def print_timing(func, message="", iterations=1):
|
||||
def wrapper(*arg):
|
||||
results = []
|
||||
connection.queries_log.clear()
|
||||
@@ -36,13 +36,12 @@ def print_timing(func, message='', iterations=1):
|
||||
res_sum = 0
|
||||
for r in results:
|
||||
res_sum += r
|
||||
print("%s%-19s: %.4f ms, %i queries (%i times)" % (
|
||||
message, func.func_name,
|
||||
res_sum,
|
||||
len(connection.queries),
|
||||
iterations
|
||||
))
|
||||
print(
|
||||
"%s%-19s: %.4f ms, %i queries (%i times)"
|
||||
% (message, func.func_name, res_sum, len(connection.queries), iterations)
|
||||
)
|
||||
sys.stdout.flush()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
@@ -52,9 +51,9 @@ class Command(BaseCommand):
|
||||
def handle_noargs(self, **options):
|
||||
if False:
|
||||
TestModelA.objects.all().delete()
|
||||
a = TestModelA.objects.create(field1='A1')
|
||||
b = TestModelB.objects.create(field1='B1', field2='B2')
|
||||
c = TestModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
a = TestModelA.objects.create(field1="A1")
|
||||
b = TestModelB.objects.create(field1="B1", field2="B2")
|
||||
c = TestModelC.objects.create(field1="C1", field2="C2", field3="C3")
|
||||
connection.queries_log.clear()
|
||||
print(TestModelC.base_objects.all())
|
||||
show_queries()
|
||||
@@ -64,7 +63,9 @@ class Command(BaseCommand):
|
||||
for i in range(1000):
|
||||
a = TestModelA.objects.create(field1=str(i % 100))
|
||||
b = TestModelB.objects.create(field1=str(i % 100), field2=str(i % 200))
|
||||
c = TestModelC.objects.create(field1=str(i % 100), field2=str(i % 200), field3=str(i % 300))
|
||||
c = TestModelC.objects.create(
|
||||
field1=str(i % 100), field2=str(i % 200), field3=str(i % 300)
|
||||
)
|
||||
if i % 100 == 0:
|
||||
print(i)
|
||||
|
||||
@@ -77,9 +78,9 @@ class Command(BaseCommand):
|
||||
return
|
||||
|
||||
NormalModelA.objects.all().delete()
|
||||
a = NormalModelA.objects.create(field1='A1')
|
||||
b = NormalModelB.objects.create(field1='B1', field2='B2')
|
||||
c = NormalModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
a = NormalModelA.objects.create(field1="A1")
|
||||
b = NormalModelB.objects.create(field1="B1", field2="B2")
|
||||
c = NormalModelC.objects.create(field1="C1", field2="C2", field3="C3")
|
||||
qs = TestModelA.objects.raw("SELECT * from pexp_testmodela")
|
||||
for o in list(qs):
|
||||
print(o)
|
||||
@@ -87,7 +88,8 @@ class Command(BaseCommand):
|
||||
|
||||
def poly_sql_query():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT id, pexp_testmodela.field1, pexp_testmodelb.field2, pexp_testmodelc.field3
|
||||
FROM pexp_testmodela
|
||||
LEFT OUTER JOIN pexp_testmodelb
|
||||
@@ -96,18 +98,23 @@ def poly_sql_query():
|
||||
ON pexp_testmodelb.testmodela_ptr_id = pexp_testmodelc.testmodelb_ptr_id
|
||||
WHERE pexp_testmodela.field1=%i
|
||||
ORDER BY pexp_testmodela.id
|
||||
""" % rnd.randint(0, 100))
|
||||
"""
|
||||
% rnd.randint(0, 100)
|
||||
)
|
||||
# row=cursor.fetchone()
|
||||
return
|
||||
|
||||
|
||||
def poly_sql_query2():
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("""
|
||||
cursor.execute(
|
||||
"""
|
||||
SELECT id, pexp_testmodela.field1
|
||||
FROM pexp_testmodela
|
||||
WHERE pexp_testmodela.field1=%i
|
||||
ORDER BY pexp_testmodela.id
|
||||
""" % rnd.randint(0, 100))
|
||||
"""
|
||||
% rnd.randint(0, 100)
|
||||
)
|
||||
# row=cursor.fetchone()
|
||||
return
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
This module is a scratchpad for general development, testing & debugging.
|
||||
"""
|
||||
@@ -20,13 +19,15 @@ class Command(NoArgsCommand):
|
||||
Project.objects.all().delete()
|
||||
a = Project.objects.create(topic="John's gathering")
|
||||
b = ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
c = ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
print Project.objects.all()
|
||||
print
|
||||
c = ResearchProject.objects.create(
|
||||
topic="Swallow Aerodynamics", supervisor="Dr. Winter"
|
||||
)
|
||||
print(Project.objects.all())
|
||||
print("")
|
||||
|
||||
TestModelA.objects.all().delete()
|
||||
a = TestModelA.objects.create(field1='A1')
|
||||
b = TestModelB.objects.create(field1='B1', field2='B2')
|
||||
c = TestModelC.objects.create(field1='C1', field2='C2', field3='C3')
|
||||
print TestModelA.objects.all()
|
||||
print
|
||||
a = TestModelA.objects.create(field1="A1")
|
||||
b = TestModelB.objects.create(field1="B1", field2="B2")
|
||||
c = TestModelC.objects.create(field1="C1", field2="C2", field3="C3")
|
||||
print(TestModelA.objects.all())
|
||||
print("")
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
This module is a scratchpad for general development, testing & debugging
|
||||
"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
import time
|
||||
from pprint import pprint
|
||||
|
||||
from django.core.management import BaseCommand
|
||||
from django.db import connection
|
||||
from pprint import pprint
|
||||
from pexp.models import *
|
||||
|
||||
from pexp.models import *
|
||||
|
||||
num_objects = 1000
|
||||
|
||||
@@ -27,7 +27,7 @@ def show_queries():
|
||||
# benchmark wrappers
|
||||
|
||||
|
||||
def print_timing(func, message='', iterations=1):
|
||||
def print_timing(func, message="", iterations=1):
|
||||
def wrapper(*arg):
|
||||
results = []
|
||||
connection.queries_log.clear()
|
||||
@@ -40,28 +40,31 @@ def print_timing(func, message='', iterations=1):
|
||||
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)
|
||||
))
|
||||
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 = print_timing(func, " ", iterations)
|
||||
f(NormalModelC)
|
||||
f = print_timing(func, 'poly ', iterations)
|
||||
f = print_timing(func, "poly ", iterations)
|
||||
f(TestModelC)
|
||||
|
||||
|
||||
###################################################################################
|
||||
# benchmarks
|
||||
|
||||
|
||||
def bench_create(model):
|
||||
for i in range(num_objects):
|
||||
model.objects.create(field1='abc' + str(i), field2='abcd' + str(i), field3='abcde' + str(i))
|
||||
model.objects.create(
|
||||
field1="abc" + str(i), field2="abcd" + str(i), field3="abcde" + str(i)
|
||||
)
|
||||
# print 'count:',model.objects.count()
|
||||
|
||||
|
||||
@@ -94,6 +97,7 @@ def bench_load2_short(model):
|
||||
def bench_delete(model):
|
||||
model.objects.all().delete()
|
||||
|
||||
|
||||
###################################################################################
|
||||
# Command
|
||||
|
||||
@@ -105,10 +109,10 @@ class Command(BaseCommand):
|
||||
func_list = [
|
||||
(bench_delete, 1),
|
||||
(bench_create, 1),
|
||||
(bench_load1, 5),
|
||||
(bench_load1, 5),
|
||||
(bench_load1_short, 5),
|
||||
(bench_load2, 5),
|
||||
(bench_load2_short, 5)
|
||||
(bench_load2_short, 5),
|
||||
]
|
||||
for f, iterations in func_list:
|
||||
run_vanilla_any_poly(f, iterations=iterations)
|
||||
|
||||
@@ -15,5 +15,7 @@ class Command(BaseCommand):
|
||||
Project.objects.all().delete()
|
||||
o = Project.objects.create(topic="John's gathering")
|
||||
o = ArtProject.objects.create(topic="Sculpting with Tim", artist="T. Turner")
|
||||
o = ResearchProject.objects.create(topic="Swallow Aerodynamics", supervisor="Dr. Winter")
|
||||
o = ResearchProject.objects.create(
|
||||
topic="Swallow Aerodynamics", supervisor="Dr. Winter"
|
||||
)
|
||||
print(Project.objects.all())
|
||||
|
||||
Reference in New Issue
Block a user