summaryrefslogtreecommitdiff
path: root/examples/performance/short_selects.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/performance/short_selects.py')
-rw-r--r--examples/performance/short_selects.py60
1 files changed, 36 insertions, 24 deletions
diff --git a/examples/performance/short_selects.py b/examples/performance/short_selects.py
index 6f64aa63e..4a8d401ad 100644
--- a/examples/performance/short_selects.py
+++ b/examples/performance/short_selects.py
@@ -6,8 +6,14 @@ record by primary key
from . import Profiler
from sqlalchemy.ext.declarative import declarative_base
-from sqlalchemy import Column, Integer, String, create_engine, \
- bindparam, select
+from sqlalchemy import (
+ Column,
+ Integer,
+ String,
+ create_engine,
+ bindparam,
+ select,
+)
from sqlalchemy.orm import Session, deferred
from sqlalchemy.ext import baked
import random
@@ -29,6 +35,7 @@ class Customer(Base):
y = deferred(Column(Integer))
z = deferred(Column(Integer))
+
Profiler.init("short_selects", num=10000)
@@ -39,16 +46,20 @@ def setup_database(dburl, echo, num):
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
sess = Session(engine)
- sess.add_all([
- Customer(
- id=i, name='c%d' % i, description="c%d" % i,
- q=i * 10,
- p=i * 20,
- x=i * 30,
- y=i * 40,
- )
- for i in ids
- ])
+ sess.add_all(
+ [
+ Customer(
+ id=i,
+ name="c%d" % i,
+ description="c%d" % i,
+ q=i * 10,
+ p=i * 20,
+ x=i * 30,
+ y=i * 40,
+ )
+ for i in ids
+ ]
+ )
sess.commit()
@@ -65,9 +76,9 @@ def test_orm_query_cols_only(n):
"""test an ORM query of only the entity columns."""
session = Session(bind=engine)
for id_ in random.sample(ids, n):
- session.query(
- Customer.id, Customer.name, Customer.description
- ).filter(Customer.id == id_).one()
+ session.query(Customer.id, Customer.name, Customer.description).filter(
+ Customer.id == id_
+ ).one()
@Profiler.profile
@@ -77,7 +88,7 @@ def test_baked_query(n):
s = Session(bind=engine)
for id_ in random.sample(ids, n):
q = bakery(lambda s: s.query(Customer))
- q += lambda q: q.filter(Customer.id == bindparam('id'))
+ q += lambda q: q.filter(Customer.id == bindparam("id"))
q(s).params(id=id_).one()
@@ -88,9 +99,9 @@ def test_baked_query_cols_only(n):
s = Session(bind=engine)
for id_ in random.sample(ids, n):
q = bakery(
- lambda s: s.query(
- Customer.id, Customer.name, Customer.description))
- q += lambda q: q.filter(Customer.id == bindparam('id'))
+ lambda s: s.query(Customer.id, Customer.name, Customer.description)
+ )
+ q += lambda q: q.filter(Customer.id == bindparam("id"))
q(s).params(id=id_).one()
@@ -109,7 +120,7 @@ def test_core_new_stmt_each_time(n):
def test_core_reuse_stmt(n):
"""test core, reusing the same statement (but recompiling each time)."""
- stmt = select([Customer.__table__]).where(Customer.id == bindparam('id'))
+ stmt = select([Customer.__table__]).where(Customer.id == bindparam("id"))
with engine.connect() as conn:
for id_ in random.sample(ids, n):
@@ -122,13 +133,14 @@ def test_core_reuse_stmt_compiled_cache(n):
"""test core, reusing the same statement + compiled cache."""
compiled_cache = {}
- stmt = select([Customer.__table__]).where(Customer.id == bindparam('id'))
- with engine.connect().\
- execution_options(compiled_cache=compiled_cache) as conn:
+ stmt = select([Customer.__table__]).where(Customer.id == bindparam("id"))
+ with engine.connect().execution_options(
+ compiled_cache=compiled_cache
+ ) as conn:
for id_ in random.sample(ids, n):
row = conn.execute(stmt, id=id_).first()
tuple(row)
-if __name__ == '__main__':
+if __name__ == "__main__":
Profiler.main()