forked from Suor/django-cacheops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.py
executable file
·72 lines (51 loc) · 1.81 KB
/
bench.py
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
#!/usr/bin/env python
import os, time, gc
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
verbosity = 1
interactive = False
fixtures = ['basic']
from operator import itemgetter
def run_benchmarks(tests):
for name, test in tests:
time, clock = bench_test(test)
print '%s\ttime: %.2fms\tclock: %.2fms' % (name, time * 1000, clock * 1000)
def bench_test(test):
prepared = None
if 'prepare_once' in test:
prepared = test['prepare_once']()
total = 0
n = 1
while total < 2:
gc.disable()
l = [bench_once(test, prepared) for i in range(n)]
gc.enable()
total = sum(r[0] for r in l)
# print [int(x * 1000000) for x in [total / n, min(l), max(l),
# sum(norm) / len(norm), min(norm), max(norm)]]
n *= 2
# print len(l)
s = sorted(l)
norm = s[n/8:-n/8] if n / 8 else s
norm_time = [r[0] for r in l]
norm_clock = [r[1] for r in l]
# return total * 2 / n # Or use normalized?
return sum(norm_time) / len(norm_time), sum(norm_clock) / len(norm_clock)
def bench_once(test, prepared=None):
if 'prepare' in test:
prepared = test['prepare']()
start = time.time()
clock = time.clock()
if prepared is None:
test['run']()
else:
test['run'](prepared)
return (time.time() - start, time.clock() - clock)
from django.db import connection
from django.core.management import call_command
# Create a test database.
db_name = connection.creation.create_test_db(verbosity=verbosity, autoclobber=not interactive)
# Import the fixture data into the test database.
call_command('loaddata', *fixtures, **{'verbosity': verbosity})
from tests.bench import TESTS
run_benchmarks(TESTS)
connection.creation.destroy_test_db(db_name, verbosity=verbosity)