|
| 1 | +"""In this series of tests, we are looking at time to load 1M very small |
| 2 | +and simple rows. |
| 3 | +
|
| 4 | +""" |
| 5 | +from . import Profiler |
| 6 | + |
| 7 | +from sqlalchemy.ext.declarative import declarative_base |
| 8 | +from sqlalchemy import Column, Integer, String, create_engine, literal_column |
| 9 | +from sqlalchemy.orm import Session, Bundle |
| 10 | + |
| 11 | +Base = declarative_base() |
| 12 | +engine = None |
| 13 | + |
| 14 | + |
| 15 | +class Customer(Base): |
| 16 | + __tablename__ = "customer" |
| 17 | + id = Column(Integer, primary_key=True) |
| 18 | + name = Column(String(255)) |
| 19 | + description = Column(String(255)) |
| 20 | + |
| 21 | + |
| 22 | +def setup_database(dburl, echo, num): |
| 23 | + global engine |
| 24 | + engine = create_engine(dburl, echo=echo) |
| 25 | + Base.metadata.drop_all(engine) |
| 26 | + Base.metadata.create_all(engine) |
| 27 | + |
| 28 | + s = Session(engine) |
| 29 | + for chunk in range(0, num, 10000): |
| 30 | + s.bulk_insert_mappings(Customer, [ |
| 31 | + { |
| 32 | + 'name': 'customer name %d' % i, |
| 33 | + 'description': 'customer description %d' % i |
| 34 | + } for i in range(chunk, chunk + 10000) |
| 35 | + ]) |
| 36 | + s.commit() |
| 37 | + |
| 38 | + |
| 39 | +@Profiler.profile |
| 40 | +def test_orm_full_objects(n): |
| 41 | + """Load fully tracked objects using the ORM.""" |
| 42 | + |
| 43 | + sess = Session(engine) |
| 44 | + # avoid using all() so that we don't have the overhead of building |
| 45 | + # a large list of full objects in memory |
| 46 | + for obj in sess.query(Customer).yield_per(1000).limit(n): |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +@Profiler.profile |
| 51 | +def test_orm_bundles(n): |
| 52 | + """Load lightweight "bundle" objects using the ORM.""" |
| 53 | + |
| 54 | + sess = Session(engine) |
| 55 | + bundle = Bundle('customer', |
| 56 | + Customer.id, Customer.name, Customer.description) |
| 57 | + for row in sess.query(bundle).yield_per(10000).limit(n): |
| 58 | + pass |
| 59 | + |
| 60 | + |
| 61 | +@Profiler.profile |
| 62 | +def test_orm_columns(n): |
| 63 | + """Load individual columns into named tuples using the ORM.""" |
| 64 | + |
| 65 | + sess = Session(engine) |
| 66 | + for row in sess.query( |
| 67 | + Customer.id, Customer.name, |
| 68 | + Customer.description).yield_per(10000).limit(n): |
| 69 | + pass |
| 70 | + |
| 71 | + |
| 72 | +@Profiler.profile |
| 73 | +def test_core_fetchall(n): |
| 74 | + """Load Core result rows using Core / fetchall.""" |
| 75 | + |
| 76 | + with engine.connect() as conn: |
| 77 | + result = conn.execute(Customer.__table__.select().limit(n)).fetchall() |
| 78 | + for row in result: |
| 79 | + data = row['id'], row['name'], row['description'] |
| 80 | + |
| 81 | + |
| 82 | +@Profiler.profile |
| 83 | +def test_core_fetchchunks_w_streaming(n): |
| 84 | + """Load Core result rows using Core with fetchmany and |
| 85 | + streaming results.""" |
| 86 | + |
| 87 | + with engine.connect() as conn: |
| 88 | + result = conn.execution_options(stream_results=True).\ |
| 89 | + execute(Customer.__table__.select().limit(n)) |
| 90 | + while True: |
| 91 | + chunk = result.fetchmany(10000) |
| 92 | + if not chunk: |
| 93 | + break |
| 94 | + for row in chunk: |
| 95 | + data = row['id'], row['name'], row['description'] |
| 96 | + |
| 97 | + |
| 98 | +@Profiler.profile |
| 99 | +def test_core_fetchchunks(n): |
| 100 | + """Load Core result rows using Core / fetchmany.""" |
| 101 | + |
| 102 | + with engine.connect() as conn: |
| 103 | + result = conn.execute(Customer.__table__.select().limit(n)) |
| 104 | + while True: |
| 105 | + chunk = result.fetchmany(10000) |
| 106 | + if not chunk: |
| 107 | + break |
| 108 | + for row in chunk: |
| 109 | + data = row['id'], row['name'], row['description'] |
| 110 | + |
| 111 | + |
| 112 | +@Profiler.profile |
| 113 | +def test_dbapi_fetchall(n): |
| 114 | + """Load DBAPI cursor rows using fetchall()""" |
| 115 | + |
| 116 | + _test_dbapi_raw(n, True) |
| 117 | + |
| 118 | + |
| 119 | +@Profiler.profile |
| 120 | +def test_dbapi_fetchchunks(n): |
| 121 | + """Load DBAPI cursor rows using fetchmany() |
| 122 | + (usually doesn't limit memory)""" |
| 123 | + |
| 124 | + _test_dbapi_raw(n, False) |
| 125 | + |
| 126 | + |
| 127 | +def _test_dbapi_raw(n, fetchall): |
| 128 | + compiled = Customer.__table__.select().limit(n).\ |
| 129 | + compile( |
| 130 | + dialect=engine.dialect, |
| 131 | + compile_kwargs={"literal_binds": True}) |
| 132 | + |
| 133 | + sql = str(compiled) |
| 134 | + |
| 135 | + import pdb |
| 136 | + pdb.set_trace() |
| 137 | + conn = engine.raw_connection() |
| 138 | + cursor = conn.cursor() |
| 139 | + cursor.execute(sql) |
| 140 | + |
| 141 | + if fetchall: |
| 142 | + for row in cursor.fetchall(): |
| 143 | + # ensure that we fully fetch! |
| 144 | + data = row[0], row[1], row[2] |
| 145 | + else: |
| 146 | + while True: |
| 147 | + chunk = cursor.fetchmany(10000) |
| 148 | + if not chunk: |
| 149 | + break |
| 150 | + for row in chunk: |
| 151 | + data = row[0], row[1], row[2] |
| 152 | + conn.close() |
| 153 | + |
| 154 | +if __name__ == '__main__': |
| 155 | + Profiler.main(setup_once=setup_database, num=1000000) |
0 commit comments