# -*- coding: utf-8 -*- | |
import csv | |
import pprint | |
import memcache | |
import pylibmc | |
import random | |
import string | |
import time | |
CYCLES = 10000 | |
def sampledata(x): | |
def create(): | |
return ''.join( | |
random.choice( | |
string.ascii_uppercase + string.digits + string.ascii_lowercase | |
) for _ in range(100) | |
) | |
return create | |
memcacheds = {} | |
memcacheds['tcp'] = '127.0.0.1:11211' | |
memcacheds['socket'] = '/var/run/memcached/memcached.sock' | |
datagens = {} | |
datagens['20'] = sampledata(200) | |
datagens['200'] = sampledata(200) | |
datagens['2000'] = sampledata(2000) | |
datagens['20000'] = sampledata(2000) | |
libs = {} | |
libs['pylibmc'] = pylibmc, { | |
'binary': True, | |
'behaviors': { | |
"tcp_nodelay": True, | |
"ketama": True, | |
} | |
} | |
libs['python-memcached'] = memcache, {'debug': 0} | |
def test(mc, datagen): | |
key = sampledata(5)() | |
nonkey = sampledata(5)() | |
mc.set(key, datagen()) | |
mc.get(nonkey) | |
mc.get(key) | |
def bench(memcached, lib, datagen): | |
start = time.time() | |
mc = lib[0].Client([memcached], **lib[1]) | |
for x in range(CYCLES): | |
test(mc, datagen) | |
return (time.time() - start) * 1000 | |
result_all = [] | |
fieldnames = set() | |
for datagen in datagens: | |
record = {} | |
result_all.append(record) | |
record['Bytes'] = datagen + ' Bytes' | |
for lib in libs: | |
for memcached in memcacheds: | |
key = '{0} + {1}'.format(lib, memcached) | |
fieldnames.update([key]) | |
record[key] = bench( | |
memcacheds[memcached], | |
libs[lib], | |
datagens[datagen] | |
) | |
pprint.pprint(result_all) | |
fieldnames = ['Bytes'] + sorted(fieldnames) | |
with open('result.csv', 'w') as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for record in result_all: | |
writer.writerow(record) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment