Last active
January 7, 2021 13:02
-
-
Save gdamjan/b8f283881b77f383ef06 to your computer and use it in GitHub Desktop.
a script to read db tables in memory, truncate tables, and insert back the data - for stupid performance tests (used sqlalchemy, psycopg2 and python 3.5)
This file contains hidden or 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
# http://dabeaz.blogspot.mk/2010/02/context-manager-for-timing-benchmarks.html | |
import time | |
class benchmark(object): | |
def __init__(self,name): | |
self.name = name | |
def __enter__(self): | |
self.start = time.time() | |
def __exit__(self,ty,val,tb): | |
end = time.time() | |
print("%s : %0.3f seconds" % (self.name, end-self.start)) | |
return False |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from benchmark import benchmark | |
from sqlalchemy.engine import reflection | |
from sqlalchemy import select, func, MetaData, create_engine | |
dburl = 'postgres://localhost/' | |
engine = create_engine(dburl, client_encoding='utf8') | |
meta = MetaData() | |
meta.reflect(bind=engine) | |
def list_tables(): | |
for table in meta.sorted_tables: | |
q = select([func.count()]).select_from(table) | |
r = engine.execute(q) | |
print(table.name, r.scalar(), end=' | ', sep=': ') | |
print() | |
with benchmark('--- list database tables'): | |
list_tables() | |
data = {} | |
with benchmark('--- read from all tables'): | |
for table in meta.sorted_tables: | |
r = engine.execute(table.select()) | |
rows = [ dict(row.items()) for row in r ] | |
data[table.name] = rows | |
with benchmark('--- delete from all tables'): | |
for table in reversed(meta.sorted_tables): | |
engine.execute(table.delete()) | |
with benchmark("--- insert into all tables"): | |
for table in meta.sorted_tables: | |
with benchmark("--- insert into: %s" % table.name): | |
rows = data[table.name] | |
if rows: | |
q = table.insert().values(rows) | |
r = engine.execute(q) | |
with benchmark('--- list database tables again'): | |
list_tables() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment