-
-
Save pavel-paulau/1aeaea76519c628702c4 to your computer and use it in GitHub Desktop.
python sdk performance benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
import uuid | |
import json | |
import time | |
import os | |
import cProfile | |
import pstats | |
import memcache | |
from couchbase.couchbaseclient import CouchbaseClient | |
from threading import Thread | |
from multiprocessing import Process | |
from pylibcb import Client | |
# Benchmark parameters | |
HOST = "127.0.0.1" | |
ITEMS = 50000 # Total number of documents to create/read | |
VALUES_PER_DOC = 1 | |
VALUE_LENGTH = 92 # together with previous parameter it defines document size | |
def prepare_data(): | |
# Test data | |
print "Preparing data..." | |
# Random string generator | |
randstr = lambda length: ''.join(random.choice(string.letters + string.digits) | |
for _ in range(length)) | |
data = dict() | |
for item in range(ITEMS): | |
key = uuid.uuid4().hex | |
value = dict((randstr(10), randstr(VALUE_LENGTH)) | |
for _ in range(VALUES_PER_DOC)) | |
data[key] = json.dumps(value) | |
return data | |
def do_sets(cb, data): | |
# Insert all items | |
print "Loading data..." | |
start_time = time.time() | |
for key, value in data.items(): | |
#cb.set(key, 0, 0, value) | |
cb.set(key, value) | |
end_time = time.time() | |
print "Set cmds/sec: {0}".format(ITEMS/(end_time - start_time)) | |
def do_gets(cb, data): | |
# Read all items from database | |
print "Reading data..." | |
start_time = time.time() | |
for key in data: | |
cb.get(key) | |
end_time = time.time() | |
print "Get cmds/sec: {0}".format(ITEMS/(end_time - start_time)) | |
def main(): | |
# Establish connection | |
cb = CouchbaseClient("http://{0}:8091/pools/default".format(HOST), | |
"default", "password", True) | |
#cb = memcache.Client(["{0}:11211".format(HOST)], debug=0) | |
cb = Client(host=HOST, user='Administrator', passwd='password', | |
bucket='default') | |
dataset = [prepare_data() for _ in range(3)] | |
threads = list() | |
for data in dataset: | |
t = Thread(target=do_sets, args=(cb, data)) | |
threads.append(t) | |
t.start() | |
for thread in threads: | |
thread.join() | |
""" | |
data = prepare_data() | |
do_sets(cb, data)""" | |
#cb.__del__() | |
#cProfile.runctx('do_sets(cb, data)', globals(), locals(), 'cb_profile') | |
if __name__ == "__main__": | |
main() | |
if False: | |
p = pstats.Stats('cb_profile') | |
p.sort_stats('cumulative').print_stats(10) | |
p.sort_stats('time').print_stats(10) | |
p.sort_stats('calls').print_stats(10) | |
os._exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment