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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
"""Benchmark diskcache.Cache
$ export PYTHONPATH=/Users/grantj/repos/python-diskcache
$ python tests/benchmark_core.py -p 1 > tests/timings_core_p1.txt
$ python tests/benchmark_core.py -p 8 > tests/timings_core_p8.txt
"""
import collections as co
import multiprocessing as mp
import os
import pickle
import random
import shutil
import time
import warnings
from utils import display
PROCS = 8
OPS = int(1e5)
RANGE = 100
WARMUP = int(1e3)
caches = []
###############################################################################
# Disk Cache Benchmarks
###############################################################################
import diskcache # noqa
caches.append(
(
'diskcache.Cache',
diskcache.Cache,
('tmp',),
{},
)
)
caches.append(
(
'diskcache.FanoutCache(shards=4, timeout=1.0)',
diskcache.FanoutCache,
('tmp',),
{'shards': 4, 'timeout': 1.0},
)
)
caches.append(
(
'diskcache.FanoutCache(shards=8, timeout=0.010)',
diskcache.FanoutCache,
('tmp',),
{'shards': 8, 'timeout': 0.010},
)
)
###############################################################################
# PyLibMC Benchmarks
###############################################################################
try:
import pylibmc
caches.append(
(
'pylibmc.Client',
pylibmc.Client,
(['127.0.0.1'],),
{
'binary': True,
'behaviors': {'tcp_nodelay': True, 'ketama': True},
},
)
)
except ImportError:
warnings.warn('skipping pylibmc')
###############################################################################
# Redis Benchmarks
###############################################################################
try:
import redis
caches.append(
(
'redis.StrictRedis',
redis.StrictRedis,
(),
{'host': 'localhost', 'port': 6379, 'db': 0},
)
)
except ImportError:
warnings.warn('skipping redis')
def worker(num, kind, args, kwargs):
random.seed(num)
time.sleep(0.01) # Let other processes start.
obj = kind(*args, **kwargs)
timings = co.defaultdict(list)
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 dispatch():
for name, kind, args, kwargs in caches:
shutil.rmtree('tmp', ignore_errors=True)
obj = kind(*args, **kwargs)
for key in range(RANGE):
key = str(key).encode('utf-8')
obj.set(key, key)
try:
obj.close()
except Exception:
pass
processes = [
mp.Process(target=worker, args=(value, kind, args, kwargs))
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()
|