-
-
Save VV0JC13CH/a4b0e92f7660d8cae50d708fa394578e to your computer and use it in GitHub Desktop.
Simple benchmarking code. Description available here: https://bitstudio.dev/embedded-cross-platform-python-databases/
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 os | |
import time | |
import shutil | |
# The databases! | |
from redis import Redis | |
from unqlite import UnQLite | |
from vedis import Vedis | |
import semidbm.db as SemiDBM | |
import sqlitedict | |
import tinydb | |
import pickledb | |
import dbm.dumb | |
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): | |
shutil.rmtree(self.filename, ignore_errors=True) | |
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 SemidbmTest(Test): | |
filename = '/tmp/semidbm.tmp' | |
def get_db(self): | |
return SemiDBM.open(self.filename, 'c') | |
class SQLitedictTest(Test): | |
filename = '/tmp/sqldict.db' | |
def get_db(self): | |
return sqlitedict.open(self.filename) | |
class DBMdumbTest(Test): | |
filename = '/tmp/dbmdumb.db' | |
def get_db(self): | |
return dbm.dumb.open(self.filename) | |
class PickleDBTest(Test): | |
filename = '/tmp/pickle.db' | |
def get_db(self): | |
return pickledb.load(self.filename, True) | |
@timed | |
def test_writes(self): | |
for i in range(self.n): | |
self.get_db().set(str(i), i) | |
@timed | |
def test_reads(self): | |
return self._db.getall() | |
class TinydbTest(Test): | |
filename = '/tmp/tinydbm.json' | |
def get_db(self): | |
return tinydb.TinyDB(self.filename) | |
@timed | |
def test_writes(self): | |
for i in range(self.n): | |
self._db.insert({"key": str(i),"value": i}) | |
@timed | |
def test_reads(self): | |
return self._db.storage.read() | |
class RedisTest(Test): | |
filename = '/tmp/redis' | |
def get_db(self): | |
db = Redis(db=15) | |
db.flushdb() | |
return db | |
tests = [ | |
UnQLiteTest, | |
UnQLiteJSONTest, | |
VedisTest, | |
SemidbmTest, | |
DBMdumbTest, | |
SQLitedictTest, | |
RedisTest, # Slow | |
TinydbTest, # Extra Slow | |
PickleDBTest, # Extra Slow | |
] | |
N = 10000 | |
print('Testing with N =', N) | |
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() |
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
Testing with N = 10000 | |
------------------------------------ | |
UnQLite | |
~~~~~~~ | |
Writes: 0.031588077545166016 | |
Reads: 0.015156984329223633 | |
UnQLiteJSON | |
~~~~~~~~~~~ | |
Writes: 0.071929931640625 | |
Reads: 0.03981304168701172 | |
Vedis | |
~~~~~ | |
Writes: 0.029628992080688477 | |
Reads: 0.017828941345214844 | |
Semidbm | |
~~~~~~~ | |
Writes: 0.06862401962280273 | |
Reads: 0.03330397605895996 | |
DBMdumb | |
~~~~~~~ | |
Writes: 1.1578619480133057 | |
Reads: 0.3975369930267334 | |
SQLitedict | |
~~~~~~~~~~ | |
Writes: 0.9191091060638428 | |
Reads: 1.3142261505126953 | |
Redis | |
~~~~~ | |
Writes: 18.476226091384888 | |
Reads: 20.019309997558594 | |
Tinydb | |
~~~~~~ | |
Writes: 165.7792468070984 | |
Reads: 0.009881734848022461 | |
PickleDB | |
~~~~~~~~ | |
Writes: 222.6582329273224 | |
Reads: 3.814697265625e-06 | |
Process finished with exit code 0 |
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
Testing with N = 100000 | |
------------------------------------ | |
UnQLite | |
~~~~~~~ | |
Writes: 0.4398989677429199 | |
Reads: 0.3231680393218994 | |
UnQLiteJSON | |
~~~~~~~~~~~ | |
Writes: 0.872934103012085 | |
Reads: 0.5446491241455078 | |
Vedis | |
~~~~~ | |
Writes: 0.4412050247192383 | |
Reads: 0.28951573371887207 | |
Semidbm | |
~~~~~~~ | |
Writes: 0.8261370658874512 | |
Reads: 0.33639979362487793 | |
DBMdumb | |
~~~~~~~ | |
Writes: 11.771900177001953 | |
Reads: 3.800724983215332 | |
SQLitedict | |
~~~~~~~~~~ | |
Writes: 10.107897996902466 | |
Reads: 14.796352863311768 | |
Redis | |
~~~~~ | |
Writes: 160.33845686912537 | |
Reads: 159.49768900871277 | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment