Hamsterdb tesing code (python)

Tesing code for HamsterDB, python bind.

Big file may slow down HamsterDB.(QPS 70 around 5,000,000 kvs)
So test_write_en() / test_read_en() use pre-hash (mmh3).(QPS 300K around 5,000,000 kvs) That is not true, I made a mistake in my code and QPS is still around 70 with 5,000,000 kvs (even after the hash and split).

mmh3 can be installed from pypi

'''
Created on 2011-8-24

@author: liheyuan
'''

import hamsterdb, os, time, random, mmh3

def test_write_en():
    LOOP = 5000000
    M = 10

    dbs = [hamsterdb.db() for i in xrange(M)]
    for i in xrange(M):
        dbs[i].create(str(i) + ".db")

    start = time.time()
    for i in xrange(LOOP):
        key = str(i)
        j = abs(mmh3.hash(key)) % M
        dbs[j].insert(key, os.urandom(512))
    #close all db
    for i in xrange(M):
        dbs[i].close()
    end = time.time()

    print "QPS:", LOOP / (end - start)

def test_read_en():
    LOOP = 5000
    M = 10

    pop = range(5000000)
    keys = random.sample(pop, LOOP)

    dbs = [hamsterdb.db() for i in xrange(M)]
    for i in xrange(M):
        dbs[i].open(str(i) + ".db")

    start = time.time()

    for key in keys:
        key = str(key)
        j = abs(mmh3.hash(key)) % M
        dbs[j].find(str(key))

    end = time.time()

    #close all db
    for i in xrange(M):
        dbs[i].close()

    print "QPS:", LOOP / (end - start)

def test_write():
    LOOP = 1000000

    start = time.time()
    db = hamsterdb.db()
    db.create("test.db")
    for i in xrange(LOOP):
        db.insert(str(i), os.urandom(512))
    db.close()
    end = time.time()

    print "QPS:", LOOP / (end - start)

def test_read():
    LOOP = 5000
    pop = range(1000000)
    keys = random.sample(pop, LOOP)

    start = time.time()
    db = hamsterdb.db()
    db.open("test.db")

    for key in keys:
        db.find(str(key))
    db.close()
    end = time.time()

    print "QPS:", LOOP / (end - start)

if __name__ == "__main__":
    #test_write_en()
    test_read_en()
    #test_write()
    #test_read()

Leave a Reply

Your email address will not be published. Required fields are marked *