Skip to content

Instantly share code, notes, and snippets.

@halfdan
Created November 8, 2018 11:07
Show Gist options
  • Save halfdan/3462897d03dab055e9cbe63fda12a6f8 to your computer and use it in GitHub Desktop.
Save halfdan/3462897d03dab055e9cbe63fda12a6f8 to your computer and use it in GitHub Desktop.
TinyDB Storage Proxy performance benchmark
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)}")
@halfdan
Copy link
Author

halfdan commented Nov 8, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment