Created
November 8, 2018 11:07
-
-
Save halfdan/3462897d03dab055e9cbe63fda12a6f8 to your computer and use it in GitHub Desktop.
TinyDB Storage Proxy performance benchmark
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
from tinydb import TinyDB | |
from tinydb.database import Table, StorageProxy, Document | |
from tinydb.storages import MemoryStorage | |
from uuid import uuid4 | |
class UUIDTable(Table): | |
def _init_last_id(self, data): | |
pass | |
def _get_next_id(self): | |
return str(uuid4()) | |
class UUIDStorageProxy(StorageProxy): | |
def _new_document(self, key, val): | |
doc_id = key | |
return Document(val, doc_id) | |
# TinyDB.storage_proxy_class = UUIDStorageProxy | |
# TinyDB.table_class = UUIDTable | |
## DB | |
db = TinyDB(storage=MemoryStorage) | |
table = db.table("cubes") | |
""" | |
Single insert is extremely slow because of repeated reads/writes: | |
for x in range(100000): | |
db.insert({"key": x, "value": x ** 2}) | |
table.insert({"key": x, "value": x ** 2}) | |
""" | |
size = 1_000_000 | |
squares = [{"key": x, "value": x ** 2} for x in range(size)] | |
cubes = [{"key": x, "value": x ** 3} for x in range(size)] | |
db.insert_multiple(squares) | |
table.insert_multiple(cubes) | |
""" | |
Benchmarking | |
Repeated reads from: | |
table | |
db | |
dict | |
""" | |
def benchmark(): | |
from timeit import timeit | |
from random import randint | |
repeats = 50 | |
def db_bench(): | |
x = randint(1, size) | |
return db.get(doc_id=x) | |
def table_bench(): | |
x = randint(1, size) | |
return table.get(doc_id=x) | |
def dict_bench(): | |
x = randint(1, size) | |
return db._storage.memory["cubes"][x] | |
print(f"Reads on db: {timeit(db_bench, number=repeats)}") | |
print(f"Reads on table: {timeit(table_bench, number=repeats)}") | |
print(f"Reads on dict: {timeit(dict_bench, number=repeats)}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Turns out the db/table comparison isn't really worth doing since
db
is just defaulting to_default
which in turn is another table object.