Skip to content

Instantly share code, notes, and snippets.

@coleifer coleifer/run_bench.py Secret
Last active Jan 17, 2018

Embed
What would you like to do?
Simple benchmarking code
import os
import time
# The databases!
from unqlite import UnQLite
from vedis import Vedis
import bsddb3
import gdbm
import kyotocabinet as kc
import plyvel
import rocksdb
import sqlite3
# For fun.
from redis import Redis
def timed(fn):
def inner(*args):
s = time.time()
fn(*args)
return time.time() - s
return inner
class Test(object):
def __init__(self, n):
self.n = n
if os.path.exists(self.filename):
if os.path.isdir(self.filename):
os.removedirs(self.filename)
else:
os.unlink(self.filename)
self._db = self.get_db()
def close(self):
try:
self._db.close()
except AttributeError:
pass
@timed
def test_writes(self):
for i in range(self.n):
self.write(str(i))
@timed
def test_reads(self):
for i in range(self.n):
self.read(str(i))
def write(self, i):
self._db[i] = i
def read(self, i):
return self._db[i]
class UnQLiteTest(Test):
filename = '/tmp/unq.db'
def get_db(self):
return UnQLite(self.filename)
class UnQLiteJSONTest(Test):
filename = '/tmp/unq-json.db'
def get_db(self):
db = UnQLite(self.filename)
self.coll = db.collection('values')
self.coll.create()
@timed
def test_writes(self):
data = [{str(i): i} for i in range(self.n)]
self.coll.store(data)
@timed
def test_reads(self):
return self.coll.all()
class VedisTest(Test):
filename = '/tmp/vedis.db'
def get_db(self):
return Vedis(self.filename)
class VedisKVTest(Test):
filename = '/tmp/vediskv.db'
def write(self, i):
self._db.kv_store(i, i)
def read(self, i):
return self._db.kv_fetch(i)
def get_db(self):
return Vedis(self.filename)
class GDBMTest(Test):
filename = '/tmp/gdbm.db'
def get_db(self):
return gdbm.open(self.filename, 'c')
class BDBBTreeTest(Test):
filename = '/tmp/bdb_btree.db'
def get_db(self):
return bsddb3.btopen(self.filename)
class BDBHashTableTest(Test):
filename = '/tmp/bdb_hashtable.db'
def get_db(self):
return bsddb3.hashopen(self.filename)
class KyotoBTreeTest(Test):
filename = '/tmp/kyoto_btree.kct'
def get_db(self):
db = kc.DB()
db.open(self.filename, kc.DB.OWRITER | kc.DB.OCREATE)
return db
def write(self, i):
self._db.set(i, i)
def read(self, i):
return self._db.get(i)
class KyotoHashTableTest(Test):
filename = '/tmp/kyoto_hashtable.kch'
def get_db(self):
db = kc.DB()
db.open(self.filename, kc.DB.OWRITER | kc.DB.OCREATE)
return db
def write(self, i):
self._db.set(i, i)
def read(self, i):
return self._db.get(i)
class LevelDBTest(Test):
filename = '/tmp/leveldb.db'
def get_db(self):
return plyvel.DB(self.filename, create_if_missing=True)
def write(self, i):
self._db.put(i, i)
def read(self, i):
return self._db.get(i)
class RocksDBTest(Test):
filename = '/tmp/rocksdb.db'
def get_db(self):
return rocksdb.DB(self.filename,
rocksdb.Options(create_if_missing=True))
def write(self, i):
self._db.put(i, i)
def read(self, i):
self._db.get(i)
class SqliteTest(Test):
filename = '/tmp/sqlite.db'
def get_db(self):
db = sqlite3.connect(self.filename)
self._cursor = db.cursor()
self._cursor.execute("""
create table data (
key varchar(255) not null primary key,
value varchar(255) not null
);
""")
return db
def write(self, i):
self._cursor.execute("INSERT INTO data (key, value) VALUES (?, ?)",
(i, i))
self._db.commit()
def read(self, i):
self._cursor.execute("SELECT * FROM data WHERE key=?", (i,))
self._cursor.fetchall()
class RedisTest(Test):
filename = '/tmp/na'
def get_db(self):
db = Redis(db=15)
db.flushdb()
return db
tests = [
UnQLiteTest,
UnQLiteJSONTest,
VedisTest,
VedisKVTest,
GDBMTest,
BDBBTreeTest,
BDBHashTableTest,
KyotoBTreeTest,
KyotoHashTableTest,
LevelDBTest,
RocksDBTest,
SqliteTest,
RedisTest,
]
N = 100000
print 'Testing with N =', N
print '------------------------------------'
print
for TestClass in tests:
test_name = TestClass.__name__.replace('Test', '')
print test_name
print '~' * len(test_name)
test = TestClass(N)
print 'Writes: ', test.test_writes()
print 'Reads: ', test.test_reads()
print
test.close()
Testing with N = 100000
------------------------------------
UnQLite
~~~~~~~
Writes: 1.05434393883
Reads: 1.35305213928
UnQLiteJSON
~~~~~~~~~~~
Writes: 2.88663101196
Reads: 3.07466506958
Vedis
~~~~~
Writes: 3.16984295845
Reads: 3.26613688469
VedisKV
~~~~~~~
Writes: 1.04473304749
Reads: 1.21517896652
GDBM
~~~~
Writes: 0.285205125809
Reads: 0.167083978653
BDBBTree
~~~~~~~~
Writes: 0.949194908142
Reads: 0.685673952103
BDBHashTable
~~~~~~~~~~~~
Writes: 1.51037788391
Reads: 0.975378036499
KyotoBTree
~~~~~~~~~~
Writes: 0.270751953125
Reads: 0.28399515152
KyotoHashTable
~~~~~~~~~~~~~~
Writes: 0.247199058533
Reads: 0.247550964355
LevelDB
~~~~~~~
Writes: 0.375289916992
Reads: 0.30633687973
RocksDB
~~~~~~~
Writes: 0.364037036896
Reads: 0.329524040222
Sqlite
~~~~~~
Writes: 6.97696003914
Reads: 1.89023999214
Redis
~~~~~
Writes: 6.24779605865
Reads: 6.45467996597
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.