-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathbenchmark_djangocache.py
175 lines (131 loc) · 3.82 KB
/
benchmark_djangocache.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""Benchmark diskcache.DjangoCache
$ export PYTHONPATH=/Users/grantj/repos/python-diskcache
$ python tests/benchmark_djangocache.py > tests/timings_djangocache.txt
"""
import collections as co
import multiprocessing as mp
import os
import pickle
import random
import shutil
import time
from utils import display
PROCS = 8
OPS = int(1e5)
RANGE = int(1.1e3)
WARMUP = int(1e3)
def setup():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings_benchmark')
import django
django.setup()
def worker(num, name):
setup()
from django.core.cache import caches
obj = caches[name]
random.seed(num)
timings = co.defaultdict(list)
time.sleep(0.01) # Let other processes start.
for count in range(OPS):
key = str(random.randrange(RANGE)).encode('utf-8')
value = str(count).encode('utf-8') * random.randrange(1, 100)
choice = random.random()
if choice < 0.900:
start = time.time()
result = obj.get(key)
end = time.time()
miss = result is None
action = 'get'
elif choice < 0.990:
start = time.time()
result = obj.set(key, value)
end = time.time()
miss = result is False
action = 'set'
else:
start = time.time()
result = obj.delete(key)
end = time.time()
miss = result is False
action = 'delete'
if count > WARMUP:
delta = end - start
timings[action].append(delta)
if miss:
timings[action + '-miss'].append(delta)
with open('output-%d.pkl' % num, 'wb') as writer:
pickle.dump(timings, writer, protocol=pickle.HIGHEST_PROTOCOL)
def prepare(name):
setup()
from django.core.cache import caches
obj = caches[name]
for key in range(RANGE):
key = str(key).encode('utf-8')
obj.set(key, key)
try:
obj.close()
except Exception:
pass
def dispatch():
setup()
from django.core.cache import caches # noqa
for name in ['locmem', 'memcached', 'redis', 'diskcache', 'filebased']:
shutil.rmtree('tmp', ignore_errors=True)
preparer = mp.Process(target=prepare, args=(name,))
preparer.start()
preparer.join()
processes = [
mp.Process(target=worker, args=(value, name))
for value in range(PROCS)
]
for process in processes:
process.start()
for process in processes:
process.join()
timings = co.defaultdict(list)
for num in range(PROCS):
filename = 'output-%d.pkl' % num
with open(filename, 'rb') as reader:
output = pickle.load(reader)
for key in output:
timings[key].extend(output[key])
os.remove(filename)
display(name, timings)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'-p',
'--processes',
type=int,
default=PROCS,
help='Number of processes to start',
)
parser.add_argument(
'-n',
'--operations',
type=float,
default=OPS,
help='Number of operations to perform',
)
parser.add_argument(
'-r',
'--range',
type=int,
default=RANGE,
help='Range of keys',
)
parser.add_argument(
'-w',
'--warmup',
type=float,
default=WARMUP,
help='Number of warmup operations before timings',
)
args = parser.parse_args()
PROCS = int(args.processes)
OPS = int(args.operations)
RANGE = int(args.range)
WARMUP = int(args.warmup)
dispatch()