diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-06-10 21:18:24 +0000 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2009-06-10 21:18:24 +0000 |
| commit | 45cec095b4904ba71425d2fe18c143982dd08f43 (patch) | |
| tree | af5e540fdcbf1cb2a3337157d69d4b40be010fa8 /test/profiling | |
| parent | 698a3c1ac665e7cd2ef8d5ad3ebf51b7fe6661f4 (diff) | |
| download | sqlalchemy-45cec095b4904ba71425d2fe18c143982dd08f43.tar.gz | |
- unit tests have been migrated from unittest to nose.
See README.unittests for information on how to run
the tests. [ticket:970]
Diffstat (limited to 'test/profiling')
| -rw-r--r-- | test/profiling/__init__.py | 0 | ||||
| -rw-r--r-- | test/profiling/alltests.py | 26 | ||||
| -rw-r--r-- | test/profiling/compiler.py | 32 | ||||
| -rw-r--r-- | test/profiling/memusage.py | 402 | ||||
| -rw-r--r-- | test/profiling/pool.py | 43 | ||||
| -rw-r--r-- | test/profiling/zoomark.py | 360 | ||||
| -rw-r--r-- | test/profiling/zoomark_orm.py | 322 |
7 files changed, 0 insertions, 1185 deletions
diff --git a/test/profiling/__init__.py b/test/profiling/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/test/profiling/__init__.py +++ /dev/null diff --git a/test/profiling/alltests.py b/test/profiling/alltests.py deleted file mode 100644 index 19401098c..000000000 --- a/test/profiling/alltests.py +++ /dev/null @@ -1,26 +0,0 @@ -import testenv; testenv.configure_for_tests() -from testlib import sa_unittest as unittest - - -def suite(): - modules_to_test = ( - 'profiling.memusage', - 'profiling.compiler', - 'profiling.pool', - 'profiling.zoomark', - 'profiling.zoomark_orm', - ) - alltests = unittest.TestSuite() - if testenv.testlib.config.coverage_enabled: - return alltests - - for name in modules_to_test: - mod = __import__(name) - for token in name.split('.')[1:]: - mod = getattr(mod, token) - alltests.addTest(unittest.findTestCases(mod, suiteClass=None)) - return alltests - - -if __name__ == '__main__': - testenv.main(suite()) diff --git a/test/profiling/compiler.py b/test/profiling/compiler.py deleted file mode 100644 index 26260068a..000000000 --- a/test/profiling/compiler.py +++ /dev/null @@ -1,32 +0,0 @@ -import testenv; testenv.configure_for_tests() -from sqlalchemy import * -from testlib import * - - -class CompileTest(TestBase, AssertsExecutionResults): - def setUpAll(self): - global t1, t2, metadata - metadata = MetaData() - t1 = Table('t1', metadata, - Column('c1', Integer, primary_key=True), - Column('c2', String(30))) - - t2 = Table('t2', metadata, - Column('c1', Integer, primary_key=True), - Column('c2', String(30))) - - @profiling.function_call_count(68, {'2.4': 42}) - def test_insert(self): - t1.insert().compile() - - @profiling.function_call_count(68, {'2.4': 45}) - def test_update(self): - t1.update().compile() - - @profiling.function_call_count(185, versions={'2.4':118}) - def test_select(self): - s = select([t1], t1.c.c2==t2.c.c1) - s.compile() - -if __name__ == '__main__': - testenv.main() diff --git a/test/profiling/memusage.py b/test/profiling/memusage.py deleted file mode 100644 index ccafc7bd7..000000000 --- a/test/profiling/memusage.py +++ /dev/null @@ -1,402 +0,0 @@ -import testenv; testenv.configure_for_tests() -import gc -from sqlalchemy.orm import mapper, relation, create_session, clear_mappers, sessionmaker -from sqlalchemy.orm.mapper import _mapper_registry -from sqlalchemy.orm.session import _sessions -import operator -from testlib import testing -from testlib.sa import MetaData, Table, Column, Integer, String, ForeignKey, PickleType -import sqlalchemy as sa -from sqlalchemy.sql import column -from orm import _base - - -class A(_base.ComparableEntity): - pass -class B(_base.ComparableEntity): - pass - -def profile_memory(func): - # run the test 50 times. if length of gc.get_objects() - # keeps growing, assert false - def profile(*args): - gc.collect() - samples = [0 for x in range(0, 50)] - for x in range(0, 50): - func(*args) - gc.collect() - samples[x] = len(gc.get_objects()) - print "sample gc sizes:", samples - - assert len(_sessions) == 0 - - for x in samples[-4:]: - if x != samples[-5]: - flatline = False - break - else: - flatline = True - - if not flatline and samples[-1] > samples[0]: # object count is bigger than when it started - for x in samples[1:-2]: - if x > samples[-1]: # see if a spike bigger than the endpoint exists - break - else: - assert False, repr(samples) + " " + repr(flatline) - - return profile - -def assert_no_mappers(): - clear_mappers() - gc.collect() - assert len(_mapper_registry) == 0 - -class EnsureZeroed(_base.ORMTest): - def setUp(self): - _sessions.clear() - _mapper_registry.clear() - -class MemUsageTest(EnsureZeroed): - - # ensure a pure growing test trips the assertion - @testing.fails_if(lambda:True) - def test_fixture(self): - class Foo(object): - pass - - x = [] - @profile_memory - def go(): - x[-1:] = [Foo(), Foo(), Foo(), Foo(), Foo(), Foo()] - go() - - def test_session(self): - metadata = MetaData(testing.db) - - table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30))) - - table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30)), - Column('col3', Integer, ForeignKey("mytable.col1"))) - - metadata.create_all() - - m1 = mapper(A, table1, properties={ - "bs":relation(B, cascade="all, delete", order_by=table2.c.col1)}, - order_by=table1.c.col1) - m2 = mapper(B, table2) - - m3 = mapper(A, table1, non_primary=True) - - @profile_memory - def go(): - sess = create_session() - a1 = A(col2="a1") - a2 = A(col2="a2") - a3 = A(col2="a3") - a1.bs.append(B(col2="b1")) - a1.bs.append(B(col2="b2")) - a3.bs.append(B(col2="b3")) - for x in [a1,a2,a3]: - sess.add(x) - sess.flush() - sess.expunge_all() - - alist = sess.query(A).all() - self.assertEquals( - [ - A(col2="a1", bs=[B(col2="b1"), B(col2="b2")]), - A(col2="a2", bs=[]), - A(col2="a3", bs=[B(col2="b3")]) - ], - alist) - - for a in alist: - sess.delete(a) - sess.flush() - go() - - metadata.drop_all() - del m1, m2, m3 - assert_no_mappers() - - def test_mapper_reset(self): - metadata = MetaData(testing.db) - - table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30))) - - table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30)), - Column('col3', Integer, ForeignKey("mytable.col1"))) - - @profile_memory - def go(): - m1 = mapper(A, table1, properties={ - "bs":relation(B, order_by=table2.c.col1) - }) - m2 = mapper(B, table2) - - m3 = mapper(A, table1, non_primary=True) - - sess = create_session() - a1 = A(col2="a1") - a2 = A(col2="a2") - a3 = A(col2="a3") - a1.bs.append(B(col2="b1")) - a1.bs.append(B(col2="b2")) - a3.bs.append(B(col2="b3")) - for x in [a1,a2,a3]: - sess.add(x) - sess.flush() - sess.expunge_all() - - alist = sess.query(A).order_by(A.col1).all() - self.assertEquals( - [ - A(col2="a1", bs=[B(col2="b1"), B(col2="b2")]), - A(col2="a2", bs=[]), - A(col2="a3", bs=[B(col2="b3")]) - ], - alist) - - for a in alist: - sess.delete(a) - sess.flush() - sess.close() - clear_mappers() - - metadata.create_all() - try: - go() - finally: - metadata.drop_all() - assert_no_mappers() - - def test_with_inheritance(self): - metadata = MetaData(testing.db) - - table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30)) - ) - - table2 = Table("mytable2", metadata, - Column('col1', Integer, ForeignKey('mytable.col1'), - primary_key=True), - Column('col3', String(30)), - ) - - @profile_memory - def go(): - class A(_base.ComparableEntity): - pass - class B(A): - pass - - mapper(A, table1, - polymorphic_on=table1.c.col2, - polymorphic_identity='a') - mapper(B, table2, - inherits=A, - polymorphic_identity='b') - - sess = create_session() - a1 = A() - a2 = A() - b1 = B(col3='b1') - b2 = B(col3='b2') - for x in [a1,a2,b1, b2]: - sess.add(x) - sess.flush() - sess.expunge_all() - - alist = sess.query(A).order_by(A.col1).all() - self.assertEquals( - [ - A(), A(), B(col3='b1'), B(col3='b2') - ], - alist) - - for a in alist: - sess.delete(a) - sess.flush() - - # dont need to clear_mappers() - del B - del A - - metadata.create_all() - try: - go() - finally: - metadata.drop_all() - assert_no_mappers() - - def test_with_manytomany(self): - metadata = MetaData(testing.db) - - table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30)) - ) - - table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', String(30)), - ) - - table3 = Table('t1tot2', metadata, - Column('t1', Integer, ForeignKey('mytable.col1')), - Column('t2', Integer, ForeignKey('mytable2.col1')), - ) - - @profile_memory - def go(): - class A(_base.ComparableEntity): - pass - class B(_base.ComparableEntity): - pass - - mapper(A, table1, properties={ - 'bs':relation(B, secondary=table3, backref='as', order_by=table3.c.t1) - }) - mapper(B, table2) - - sess = create_session() - a1 = A(col2='a1') - a2 = A(col2='a2') - b1 = B(col2='b1') - b2 = B(col2='b2') - a1.bs.append(b1) - a2.bs.append(b2) - for x in [a1,a2]: - sess.add(x) - sess.flush() - sess.expunge_all() - - alist = sess.query(A).order_by(A.col1).all() - self.assertEquals( - [ - A(bs=[B(col2='b1')]), A(bs=[B(col2='b2')]) - ], - alist) - - for a in alist: - sess.delete(a) - sess.flush() - - # dont need to clear_mappers() - del B - del A - - metadata.create_all() - try: - go() - finally: - metadata.drop_all() - assert_no_mappers() - - def test_join_cache(self): - metadata = MetaData(testing.db) - - table1 = Table("table1", metadata, - Column('id', Integer, primary_key=True), - Column('data', String(30)) - ) - - table2 = Table("table2", metadata, - Column('id', Integer, primary_key=True), - Column('data', String(30)), - Column('t1id', Integer, ForeignKey('table1.id')) - ) - - class Foo(object): - pass - - class Bar(object): - pass - - mapper(Foo, table1, properties={ - 'bars':relation(mapper(Bar, table2)) - }) - metadata.create_all() - - session = sessionmaker() - - @profile_memory - def go(): - s = table2.select() - sess = session() - sess.query(Foo).join((s, Foo.bars)).all() - sess.rollback() - try: - go() - finally: - metadata.drop_all() - - - def test_mutable_identity(self): - metadata = MetaData(testing.db) - - table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True), - Column('col2', PickleType(comparator=operator.eq)) - ) - - class Foo(object): - def __init__(self, col2): - self.col2 = col2 - - mapper(Foo, table1) - metadata.create_all() - - session = sessionmaker()() - - def go(): - obj = [ - Foo({'a':1}), - Foo({'b':1}), - Foo({'c':1}), - Foo({'d':1}), - Foo({'e':1}), - Foo({'f':1}), - Foo({'g':1}), - Foo({'h':1}), - Foo({'i':1}), - Foo({'j':1}), - Foo({'k':1}), - Foo({'l':1}), - ] - - session.add_all(obj) - session.commit() - - testing.eq_(len(session.identity_map._mutable_attrs), 12) - testing.eq_(len(session.identity_map), 12) - obj = None - gc.collect() - testing.eq_(len(session.identity_map._mutable_attrs), 0) - testing.eq_(len(session.identity_map), 0) - - try: - go() - finally: - metadata.drop_all() - - def test_type_compile(self): - from sqlalchemy.databases.sqlite import SQLiteDialect - cast = sa.cast(column('x'), sa.Integer) - @profile_memory - def go(): - dialect = SQLiteDialect() - cast.compile(dialect=dialect) - go() - -if __name__ == '__main__': - testenv.main() diff --git a/test/profiling/pool.py b/test/profiling/pool.py deleted file mode 100644 index f3f69222c..000000000 --- a/test/profiling/pool.py +++ /dev/null @@ -1,43 +0,0 @@ -import testenv; testenv.configure_for_tests() -from sqlalchemy import * -from testlib import * -from sqlalchemy.pool import QueuePool - - -class QueuePoolTest(TestBase, AssertsExecutionResults): - class Connection(object): - def close(self): - pass - - def setUp(self): - global pool - pool = QueuePool(creator=self.Connection, - pool_size=3, max_overflow=-1, - use_threadlocal=True) - - - @profiling.function_call_count(54, {'2.4': 38}) - def test_first_connect(self): - conn = pool.connect() - - def test_second_connect(self): - conn = pool.connect() - conn.close() - - @profiling.function_call_count(31, {'2.4': 21}) - def go(): - conn2 = pool.connect() - return conn2 - c2 = go() - - def test_second_samethread_connect(self): - conn = pool.connect() - - @profiling.function_call_count(5, {'2.4': 3}) - def go(): - return pool.connect() - c2 = go() - - -if __name__ == '__main__': - testenv.main() diff --git a/test/profiling/zoomark.py b/test/profiling/zoomark.py deleted file mode 100644 index c9f3d9df8..000000000 --- a/test/profiling/zoomark.py +++ /dev/null @@ -1,360 +0,0 @@ -"""Benchmark for SQLAlchemy. - -An adaptation of Robert Brewers' ZooMark speed tests. -""" - -import datetime -import sys -import time -import testenv; testenv.configure_for_tests() -from sqlalchemy import * -from testlib import * - -ITERATIONS = 1 - -dbapi_session = engines.ReplayableSession() -metadata = None - -class ZooMarkTest(TestBase): - """Runs the ZooMark and squawks if method counts vary from the norm. - - Each test has an associated `call_range`, the total number of accepted - function calls made during the test. The count can vary between Python - 2.4 and 2.5. - - Unlike a unit test, this is a ordered collection of steps. Running - components individually will fail. - - """ - - __only_on__ = 'postgres' - __skip_if__ = ((lambda: sys.version_info < (2, 4)), ) - - def test_baseline_0_setup(self): - global metadata - - creator = testing.db.pool._creator - recorder = lambda: dbapi_session.recorder(creator()) - engine = engines.testing_engine(options={'creator':recorder}) - metadata = MetaData(engine) - - def test_baseline_1_create_tables(self): - Zoo = Table('Zoo', metadata, - Column('ID', Integer, Sequence('zoo_id_seq'), - primary_key=True, index=True), - Column('Name', Unicode(255)), - Column('Founded', Date), - Column('Opens', Time), - Column('LastEscape', DateTime), - Column('Admission', Float), - ) - - Animal = Table('Animal', metadata, - Column('ID', Integer, Sequence('animal_id_seq'), - primary_key=True), - Column('ZooID', Integer, ForeignKey('Zoo.ID'), - index=True), - Column('Name', Unicode(100)), - Column('Species', Unicode(100)), - Column('Legs', Integer, default=4), - Column('LastEscape', DateTime), - Column('Lifespan', Float(4)), - Column('MotherID', Integer, ForeignKey('Animal.ID')), - Column('PreferredFoodID', Integer), - Column('AlternateFoodID', Integer), - ) - metadata.create_all() - - def test_baseline_1a_populate(self): - Zoo = metadata.tables['Zoo'] - Animal = metadata.tables['Animal'] - - wap = Zoo.insert().execute(Name=u'Wild Animal Park', - Founded=datetime.date(2000, 1, 1), - # 59 can give rounding errors with divmod, which - # AdapterFromADO needs to correct. - Opens=datetime.time(8, 15, 59), - LastEscape=datetime.datetime(2004, 7, 29, 5, 6, 7), - Admission=4.95, - ).last_inserted_ids()[0] - - sdz = Zoo.insert().execute(Name =u'San Diego Zoo', - Founded = datetime.date(1935, 9, 13), - Opens = datetime.time(9, 0, 0), - Admission = 0, - ).last_inserted_ids()[0] - - Zoo.insert().execute( - Name = u'Montr\xe9al Biod\xf4me', - Founded = datetime.date(1992, 6, 19), - Opens = datetime.time(9, 0, 0), - Admission = 11.75, - ) - - seaworld = Zoo.insert().execute( - Name =u'Sea_World', Admission = 60).last_inserted_ids()[0] - - # Let's add a crazy futuristic Zoo to test large date values. - lp = Zoo.insert().execute(Name =u'Luna Park', - Founded = datetime.date(2072, 7, 17), - Opens = datetime.time(0, 0, 0), - Admission = 134.95, - ).last_inserted_ids()[0] - - # Animals - leopardid = Animal.insert().execute(Species=u'Leopard', Lifespan=73.5, - ).last_inserted_ids()[0] - Animal.update(Animal.c.ID==leopardid).execute(ZooID=wap, - LastEscape=datetime.datetime(2004, 12, 21, 8, 15, 0, 999907)) - - lion = Animal.insert().execute(Species=u'Lion', ZooID=wap).last_inserted_ids()[0] - Animal.insert().execute(Species=u'Slug', Legs=1, Lifespan=.75) - - tiger = Animal.insert().execute(Species=u'Tiger', ZooID=sdz - ).last_inserted_ids()[0] - - # Override Legs.default with itself just to make sure it works. - Animal.insert().execute(Species=u'Bear', Legs=4) - Animal.insert().execute(Species=u'Ostrich', Legs=2, Lifespan=103.2) - Animal.insert().execute(Species=u'Centipede', Legs=100) - - emp = Animal.insert().execute(Species=u'Emperor Penguin', Legs=2, - ZooID=seaworld).last_inserted_ids()[0] - adelie = Animal.insert().execute(Species=u'Adelie Penguin', Legs=2, - ZooID=seaworld).last_inserted_ids()[0] - - Animal.insert().execute(Species=u'Millipede', Legs=1000000, ZooID=sdz) - - # Add a mother and child to test relationships - bai_yun = Animal.insert().execute(Species=u'Ape', Name=u'Bai Yun', - Legs=2).last_inserted_ids()[0] - Animal.insert().execute(Species=u'Ape', Name=u'Hua Mei', Legs=2, - MotherID=bai_yun) - - def test_baseline_2_insert(self): - Animal = metadata.tables['Animal'] - i = Animal.insert() - for x in xrange(ITERATIONS): - tick = i.execute(Species=u'Tick', Name=u'Tick %d' % x, Legs=8) - - def test_baseline_3_properties(self): - Zoo = metadata.tables['Zoo'] - Animal = metadata.tables['Animal'] - - def fullobject(select): - """Iterate over the full result row.""" - return list(select.execute().fetchone()) - - for x in xrange(ITERATIONS): - # Zoos - WAP = fullobject(Zoo.select(Zoo.c.Name==u'Wild Animal Park')) - SDZ = fullobject(Zoo.select(Zoo.c.Founded==datetime.date(1935, 9, 13))) - Biodome = fullobject(Zoo.select(Zoo.c.Name==u'Montr\xe9al Biod\xf4me')) - seaworld = fullobject(Zoo.select(Zoo.c.Admission == float(60))) - - # Animals - leopard = fullobject(Animal.select(Animal.c.Species ==u'Leopard')) - ostrich = fullobject(Animal.select(Animal.c.Species==u'Ostrich')) - millipede = fullobject(Animal.select(Animal.c.Legs==1000000)) - ticks = fullobject(Animal.select(Animal.c.Species==u'Tick')) - - def test_baseline_4_expressions(self): - Zoo = metadata.tables['Zoo'] - Animal = metadata.tables['Animal'] - - def fulltable(select): - """Iterate over the full result table.""" - return [list(row) for row in select.execute().fetchall()] - - for x in xrange(ITERATIONS): - assert len(fulltable(Zoo.select())) == 5 - assert len(fulltable(Animal.select())) == ITERATIONS + 12 - assert len(fulltable(Animal.select(Animal.c.Legs==4))) == 4 - assert len(fulltable(Animal.select(Animal.c.Legs == 2))) == 5 - assert len(fulltable(Animal.select(and_(Animal.c.Legs >= 2, Animal.c.Legs < 20) - ))) == ITERATIONS + 9 - assert len(fulltable(Animal.select(Animal.c.Legs > 10))) == 2 - assert len(fulltable(Animal.select(Animal.c.Lifespan > 70))) == 2 - assert len(fulltable(Animal.select(Animal.c.Species.startswith(u'L')))) == 2 - assert len(fulltable(Animal.select(Animal.c.Species.endswith(u'pede')))) == 2 - - assert len(fulltable(Animal.select(Animal.c.LastEscape != None))) == 1 - assert len(fulltable(Animal.select(None == Animal.c.LastEscape - ))) == ITERATIONS + 11 - - # In operator (containedby) - assert len(fulltable(Animal.select(Animal.c.Species.like(u'%pede%')))) == 2 - assert len(fulltable(Animal.select(Animal.c.Species.in_([u'Lion', u'Tiger', u'Bear'])))) == 3 - - # Try In with cell references - class thing(object): pass - pet, pet2 = thing(), thing() - pet.Name, pet2.Name =u'Slug', u'Ostrich' - assert len(fulltable(Animal.select(Animal.c.Species.in_([pet.Name, pet2.Name])))) == 2 - - # logic and other functions - assert len(fulltable(Animal.select(Animal.c.Species.like(u'Slug')))) == 1 - assert len(fulltable(Animal.select(Animal.c.Species.like(u'%pede%')))) == 2 - name =u'Lion' - assert len(fulltable(Animal.select(func.length(Animal.c.Species) == len(name) - ))) == ITERATIONS + 3 - - assert len(fulltable(Animal.select(Animal.c.Species.like(u'%i%') - ))) == ITERATIONS + 7 - - # Test now(), today(), year(), month(), day() - assert len(fulltable(Zoo.select(Zoo.c.Founded != None - and Zoo.c.Founded < func.current_timestamp(_type=Date)))) == 3 - assert len(fulltable(Animal.select(Animal.c.LastEscape == func.current_timestamp(_type=Date)))) == 0 - assert len(fulltable(Animal.select(func.date_part('year', Animal.c.LastEscape) == 2004))) == 1 - assert len(fulltable(Animal.select(func.date_part('month', Animal.c.LastEscape) == 12))) == 1 - assert len(fulltable(Animal.select(func.date_part('day', Animal.c.LastEscape) == 21))) == 1 - - def test_baseline_5_aggregates(self): - Animal = metadata.tables['Animal'] - Zoo = metadata.tables['Zoo'] - - for x in xrange(ITERATIONS): - # views - view = select([Animal.c.Legs]).execute().fetchall() - legs = [x[0] for x in view] - legs.sort() - - expected = {'Leopard': 73.5, - 'Slug': .75, - 'Tiger': None, - 'Lion': None, - 'Bear': None, - 'Ostrich': 103.2, - 'Centipede': None, - 'Emperor Penguin': None, - 'Adelie Penguin': None, - 'Millipede': None, - 'Ape': None, - 'Tick': None, - } - for species, lifespan in select([Animal.c.Species, Animal.c.Lifespan] - ).execute().fetchall(): - assert lifespan == expected[species] - - expected = [u'Montr\xe9al Biod\xf4me', 'Wild Animal Park'] - e = select([Zoo.c.Name], - and_(Zoo.c.Founded != None, - Zoo.c.Founded <= func.current_timestamp(), - Zoo.c.Founded >= datetime.date(1990, 1, 1))) - values = [val[0] for val in e.execute().fetchall()] - assert set(values) == set(expected) - - # distinct - legs = [x[0] for x in - select([Animal.c.Legs], distinct=True).execute().fetchall()] - legs.sort() - - def test_baseline_6_editing(self): - Zoo = metadata.tables['Zoo'] - - for x in xrange(ITERATIONS): - # Edit - SDZ = Zoo.select(Zoo.c.Name==u'San Diego Zoo').execute().fetchone() - Zoo.update(Zoo.c.ID==SDZ['ID']).execute( - Name=u'The San Diego Zoo', - Founded = datetime.date(1900, 1, 1), - Opens = datetime.time(7, 30, 0), - Admission = "35.00") - - # Test edits - SDZ = Zoo.select(Zoo.c.Name==u'The San Diego Zoo').execute().fetchone() - assert SDZ['Founded'] == datetime.date(1900, 1, 1), SDZ['Founded'] - - # Change it back - Zoo.update(Zoo.c.ID==SDZ['ID']).execute( - Name =u'San Diego Zoo', - Founded = datetime.date(1935, 9, 13), - Opens = datetime.time(9, 0, 0), - Admission = "0") - - # Test re-edits - SDZ = Zoo.select(Zoo.c.Name==u'San Diego Zoo').execute().fetchone() - assert SDZ['Founded'] == datetime.date(1935, 9, 13) - - def test_baseline_7_multiview(self): - Zoo = metadata.tables['Zoo'] - Animal = metadata.tables['Animal'] - - def fulltable(select): - """Iterate over the full result table.""" - return [list(row) for row in select.execute().fetchall()] - - for x in xrange(ITERATIONS): - za = fulltable(select([Zoo.c.ID] + list(Animal.c), - Zoo.c.Name ==u'San Diego Zoo', - from_obj = [join(Zoo, Animal)])) - - SDZ = Zoo.select(Zoo.c.Name==u'San Diego Zoo') - - e = fulltable(select([Zoo.c.ID, Animal.c.ID], - and_(Zoo.c.Name==u'San Diego Zoo', - Animal.c.Species==u'Leopard'), - from_obj = [join(Zoo, Animal)])) - - # Now try the same query with INNER, LEFT, and RIGHT JOINs. - e = fulltable(select([Zoo.c.Name, Animal.c.Species], - from_obj=[join(Zoo, Animal)])) - e = fulltable(select([Zoo.c.Name, Animal.c.Species], - from_obj=[outerjoin(Zoo, Animal)])) - e = fulltable(select([Zoo.c.Name, Animal.c.Species], - from_obj=[outerjoin(Animal, Zoo)])) - - def test_baseline_8_drop(self): - metadata.drop_all() - - # Now, run all of these tests again with the DB-API driver factored out: - # the ReplayableSession playback stands in for the database. - - # How awkward is this in a unittest framework? Very. - - def test_profile_0(self): - global metadata - - player = lambda: dbapi_session.player() - engine = create_engine('postgres:///', creator=player) - metadata = MetaData(engine) - - @profiling.function_call_count(3230, {'2.4': 1796}) - def test_profile_1_create_tables(self): - self.test_baseline_1_create_tables() - - @profiling.function_call_count(5726, {'2.4': 3650}) - def test_profile_1a_populate(self): - self.test_baseline_1a_populate() - - @profiling.function_call_count(322, {'2.4': 202}) - def test_profile_2_insert(self): - self.test_baseline_2_insert() - - @profiling.function_call_count(3834, {'2.4': 2347}) - def test_profile_3_properties(self): - self.test_baseline_3_properties() - - @profiling.function_call_count(14752, {'2.4': 9950}) - def test_profile_4_expressions(self): - self.test_baseline_4_expressions() - - @profiling.function_call_count(1347, {'2.4': 1001}) - def test_profile_5_aggregates(self): - self.test_baseline_5_aggregates() - - @profiling.function_call_count(1904, {'2.4': 1193}) - def test_profile_6_editing(self): - self.test_baseline_6_editing() - - @profiling.function_call_count(2994, {'2.4': 1998}) - def test_profile_7_multiview(self): - self.test_baseline_7_multiview() - - def test_profile_8_drop(self): - self.test_baseline_8_drop() - - -if __name__ == '__main__': - testenv.main() diff --git a/test/profiling/zoomark_orm.py b/test/profiling/zoomark_orm.py deleted file mode 100644 index 5d7192261..000000000 --- a/test/profiling/zoomark_orm.py +++ /dev/null @@ -1,322 +0,0 @@ -"""Benchmark for SQLAlchemy. - -An adaptation of Robert Brewers' ZooMark speed tests. -""" - -import datetime -import sys -import time -import testenv; testenv.configure_for_tests() -from sqlalchemy import * -from sqlalchemy.orm import * -from testlib import * - -ITERATIONS = 1 - -dbapi_session = engines.ReplayableSession() -metadata = None - -class ZooMarkTest(TestBase): - """Runs the ZooMark and squawks if method counts vary from the norm. - - Each test has an associated `call_range`, the total number of accepted - function calls made during the test. The count can vary between Python - 2.4 and 2.5. - - Unlike a unit test, this is a ordered collection of steps. Running - components individually will fail. - - """ - - __only_on__ = 'postgres' - __skip_if__ = ((lambda: sys.version_info < (2, 5)), ) # TODO: get 2.4 support - - def test_baseline_0_setup(self): - global metadata, session - - creator = testing.db.pool._creator - recorder = lambda: dbapi_session.recorder(creator()) - engine = engines.testing_engine(options={'creator':recorder}) - metadata = MetaData(engine) - session = sessionmaker()() - - def test_baseline_1_create_tables(self): - zoo = Table('Zoo', metadata, - Column('ID', Integer, Sequence('zoo_id_seq'), - primary_key=True, index=True), - Column('Name', Unicode(255)), - Column('Founded', Date), - Column('Opens', Time), - Column('LastEscape', DateTime), - Column('Admission', Float), - ) - - animal = Table('Animal', metadata, - Column('ID', Integer, Sequence('animal_id_seq'), - primary_key=True), - Column('ZooID', Integer, ForeignKey('Zoo.ID'), - index=True), - Column('Name', Unicode(100)), - Column('Species', Unicode(100)), - Column('Legs', Integer, default=4), - Column('LastEscape', DateTime), - Column('Lifespan', Float(4)), - Column('MotherID', Integer, ForeignKey('Animal.ID')), - Column('PreferredFoodID', Integer), - Column('AlternateFoodID', Integer), - ) - metadata.create_all() - - global Zoo, Animal - class Zoo(object): - def __init__(self, **kwargs): - for k, v in kwargs.iteritems(): - setattr(self, k, v) - class Animal(object): - def __init__(self, **kwargs): - for k, v in kwargs.iteritems(): - setattr(self, k, v) - mapper(Zoo, zoo) - mapper(Animal, animal) - - def test_baseline_1a_populate(self): - wap = Zoo(Name=u'Wild Animal Park', - Founded=datetime.date(2000, 1, 1), - # 59 can give rounding errors with divmod, which - # AdapterFromADO needs to correct. - Opens=datetime.time(8, 15, 59), - LastEscape=datetime.datetime(2004, 7, 29, 5, 6, 7), - Admission=4.95, - ) - session.add(wap) - sdz = Zoo(Name =u'San Diego Zoo', - # This early date should play havoc with a number - # of implementations. - Founded = datetime.date(1835, 9, 13), - Opens = datetime.time(9, 0, 0), - Admission = 0, - ) - session.add(sdz) - - bio = Zoo( - Name = u'Montr\xe9al Biod\xf4me', - Founded = datetime.date(1992, 6, 19), - Opens = datetime.time(9, 0, 0), - Admission = 11.75, - ) - session.add(bio) - - seaworld = Zoo( - Name =u'Sea_World', Admission = 60) - session.add(seaworld) - - # Let's add a crazy futuristic Zoo to test large date values. - lp = Zoo(Name =u'Luna Park', - Founded = datetime.date(2072, 7, 17), - Opens = datetime.time(0, 0, 0), - Admission = 134.95, - ) - session.add(lp) - session.flush() - - # Animals - leopard = Animal(Species=u'Leopard', Lifespan=73.5,) - session.add(leopard) - leopard.ZooID = wap.ID - leopard.LastEscape = datetime.datetime(2004, 12, 21, 8, 15, 0, 999907) - - session.add(Animal(Species=u'Lion', ZooID=wap.ID)) - session.add(Animal(Species=u'Slug', Legs=1, Lifespan=.75)) - session.add(Animal(Species=u'Tiger', ZooID=sdz.ID)) - - # Override Legs.default with itself just to make sure it works. - session.add(Animal(Species=u'Bear', Legs=4)) - session.add(Animal(Species=u'Ostrich', Legs=2, Lifespan=103.2)) - session.add(Animal(Species=u'Centipede', Legs=100)) - - session.add(Animal(Species=u'Emperor Penguin', Legs=2, ZooID=seaworld.ID)) - session.add(Animal(Species=u'Adelie Penguin', Legs=2, ZooID=seaworld.ID)) - - session.add(Animal(Species=u'Millipede', Legs=1000000, ZooID=sdz.ID)) - - # Add a mother and child to test relationships - bai_yun = Animal(Species=u'Ape', Nameu=u'Bai Yun', Legs=2) - session.add(bai_yun) - session.add(Animal(Species=u'Ape', Name=u'Hua Mei', Legs=2, - MotherID=bai_yun.ID)) - session.flush() - session.commit() - - def test_baseline_2_insert(self): - for x in xrange(ITERATIONS): - session.add(Animal(Species=u'Tick', Name=u'Tick %d' % x, Legs=8)) - session.flush() - - def test_baseline_3_properties(self): - for x in xrange(ITERATIONS): - # Zoos - WAP = list(session.query(Zoo).filter(Zoo.Name==u'Wild Animal Park')) - SDZ = list(session.query(Zoo).filter(Zoo.Founded==datetime.date(1835, 9, 13))) - Biodome = list(session.query(Zoo).filter(Zoo.Name==u'Montr\xe9al Biod\xf4me')) - seaworld = list(session.query(Zoo).filter(Zoo.Admission == float(60))) - - # Animals - leopard = list(session.query(Animal).filter(Animal.Species == u'Leopard')) - ostrich = list(session.query(Animal).filter(Animal.Species==u'Ostrich')) - millipede = list(session.query(Animal).filter(Animal.Legs==1000000)) - ticks = list(session.query(Animal).filter(Animal.Species==u'Tick')) - - def test_baseline_4_expressions(self): - for x in xrange(ITERATIONS): - assert len(list(session.query(Zoo))) == 5 - assert len(list(session.query(Animal))) == ITERATIONS + 12 - assert len(list(session.query(Animal).filter(Animal.Legs==4))) == 4 - assert len(list(session.query(Animal).filter(Animal.Legs == 2))) == 5 - assert len(list(session.query(Animal).filter(and_(Animal.Legs >= 2, Animal.Legs < 20)))) == ITERATIONS + 9 - assert len(list(session.query(Animal).filter(Animal.Legs > 10))) == 2 - assert len(list(session.query(Animal).filter(Animal.Lifespan > 70))) == 2 - assert len(list(session.query(Animal).filter(Animal.Species.like(u'L%')))) == 2 - assert len(list(session.query(Animal).filter(Animal.Species.like(u'%pede')))) == 2 - - assert len(list(session.query(Animal).filter(Animal.LastEscape != None))) == 1 - assert len(list(session.query(Animal).filter(Animal.LastEscape == None))) == ITERATIONS + 11 - - # In operator (containedby) - assert len(list(session.query(Animal).filter(Animal.Species.like(u'%pede%')))) == 2 - assert len(list(session.query(Animal).filter( - Animal.Species.in_((u'Lion', u'Tiger', u'Bear'))))) == 3 - - # Try In with cell references - class thing(object): pass - pet, pet2 = thing(), thing() - pet.Name, pet2.Name = u'Slug', u'Ostrich' - assert len(list(session.query(Animal).filter(Animal.Species.in_((pet.Name, pet2.Name))))) == 2 - - # logic and other functions - name =u'Lion' - assert len(list(session.query(Animal).filter(func.length(Animal.Species) == len(name)))) == ITERATIONS + 3 - - assert len(list(session.query(Animal).filter(Animal.Species.like(u'%i%')))) == ITERATIONS + 7 - - # Test now(), today(), year(), month(), day() - assert len(list(session.query(Zoo).filter(and_(Zoo.Founded != None, Zoo.Founded < func.now())))) == 3 - assert len(list(session.query(Animal).filter(Animal.LastEscape == func.now()))) == 0 - assert len(list(session.query(Animal).filter(func.date_part('year', Animal.LastEscape) == 2004))) == 1 - assert len(list(session.query(Animal).filter(func.date_part('month', Animal.LastEscape) == 12))) == 1 - assert len(list(session.query(Animal).filter(func.date_part('day', Animal.LastEscape) == 21))) == 1 - - def test_baseline_5_aggregates(self): - Animal = metadata.tables['Animal'] - Zoo = metadata.tables['Zoo'] - - # TODO: convert to ORM - for x in xrange(ITERATIONS): - # views - view = select([Animal.c.Legs]).execute().fetchall() - legs = [x[0] for x in view] - legs.sort() - - expected = {'Leopard': 73.5, - 'Slug': .75, - 'Tiger': None, - 'Lion': None, - 'Bear': None, - 'Ostrich': 103.2, - 'Centipede': None, - 'Emperor Penguin': None, - 'Adelie Penguin': None, - 'Millipede': None, - 'Ape': None, - 'Tick': None, - } - for species, lifespan in select([Animal.c.Species, Animal.c.Lifespan] - ).execute().fetchall(): - assert lifespan == expected[species] - - expected = [u'Montr\xe9al Biod\xf4me', 'Wild Animal Park'] - e = select([Zoo.c.Name], - and_(Zoo.c.Founded != None, - Zoo.c.Founded <= func.current_timestamp(), - Zoo.c.Founded >= datetime.date(1990, 1, 1))) - values = [val[0] for val in e.execute().fetchall()] - assert set(values) == set(expected) - - # distinct - legs = [x[0] for x in - select([Animal.c.Legs], distinct=True).execute().fetchall()] - legs.sort() - - def test_baseline_6_editing(self): - for x in xrange(ITERATIONS): - # Edit - SDZ = session.query(Zoo).filter(Zoo.Name==u'San Diego Zoo').one() - SDZ.Name = u'The San Diego Zoo' - SDZ.Founded = datetime.date(1900, 1, 1) - SDZ.Opens = datetime.time(7, 30, 0) - SDZ.Admission = 35.00 - - # Test edits - SDZ = session.query(Zoo).filter(Zoo.Name==u'The San Diego Zoo').one() - assert SDZ.Founded == datetime.date(1900, 1, 1), SDZ.Founded - - # Change it back - SDZ.Name = u'San Diego Zoo' - SDZ.Founded = datetime.date(1835, 9, 13) - SDZ.Opens = datetime.time(9, 0, 0) - SDZ.Admission = 0 - - # Test re-edits - SDZ = session.query(Zoo).filter(Zoo.Name==u'San Diego Zoo').one() - assert SDZ.Founded == datetime.date(1835, 9, 13), SDZ.Founded - - def test_baseline_7_drop(self): - session.rollback() - metadata.drop_all() - - # Now, run all of these tests again with the DB-API driver factored out: - # the ReplayableSession playback stands in for the database. - - # How awkward is this in a unittest framework? Very. - - def test_profile_0(self): - global metadata, session - - player = lambda: dbapi_session.player() - engine = create_engine('postgres:///', creator=player) - metadata = MetaData(engine) - session = sessionmaker()() - - @profiling.function_call_count(4898) - def test_profile_1_create_tables(self): - self.test_baseline_1_create_tables() - - @profiling.function_call_count(12178, {'2.4':12178}) - def test_profile_1a_populate(self): - self.test_baseline_1a_populate() - - @profiling.function_call_count(903, {'2.4':903}) - def test_profile_2_insert(self): - self.test_baseline_2_insert() - - @profiling.function_call_count(6783) - def test_profile_3_properties(self): - self.test_baseline_3_properties() - - @profiling.function_call_count(23861) - def test_profile_4_expressions(self): - self.test_baseline_4_expressions() - - @profiling.function_call_count(1331) - def test_profile_5_aggregates(self): - self.test_baseline_5_aggregates() - - @profiling.function_call_count(3343) - def test_profile_6_editing(self): - self.test_baseline_6_editing() - - def test_profile_7_drop(self): - self.test_baseline_7_drop() - - -if __name__ == '__main__': - testenv.main() |
