diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/aaa_profiling/test_memusage.py | 115 | ||||
-rw-r--r-- | test/base/test_utils.py | 46 |
2 files changed, 138 insertions, 23 deletions
diff --git a/test/aaa_profiling/test_memusage.py b/test/aaa_profiling/test_memusage.py index 2d64cd804..2e1810eca 100644 --- a/test/aaa_profiling/test_memusage.py +++ b/test/aaa_profiling/test_memusage.py @@ -1,12 +1,13 @@ from sqlalchemy.test.testing import eq_ -from sqlalchemy.orm import mapper, relationship, create_session, clear_mappers, \ - sessionmaker, class_mapper +from sqlalchemy.orm import mapper, relationship, create_session,\ + clear_mappers, sessionmaker, class_mapper from sqlalchemy.orm.mapper import _mapper_registry from sqlalchemy.orm.session import _sessions from sqlalchemy.util import jython import operator from sqlalchemy.test import testing, engines -from sqlalchemy import MetaData, Integer, String, ForeignKey, PickleType, create_engine +from sqlalchemy import MetaData, Integer, String, ForeignKey, \ + PickleType, create_engine from sqlalchemy.test.schema import Table, Column import sqlalchemy as sa from sqlalchemy.sql import column @@ -48,9 +49,11 @@ def profile_memory(func): else: flatline = True - if not flatline and samples[-1] > samples[0]: # object count is bigger than when it started + # object count is bigger than when it started + if not flatline and samples[-1] > samples[0]: for x in samples[1:-2]: - if x > samples[-1]: # see if a spike bigger than the endpoint exists + # see if a spike bigger than the endpoint exists + if x > samples[-1]: break else: assert False, repr(samples) + " " + repr(flatline) @@ -85,18 +88,21 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30))) table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)), Column('col3', Integer, ForeignKey("mytable.col1"))) metadata.create_all() m1 = mapper(A, table1, properties={ - "bs":relationship(B, cascade="all, delete", order_by=table2.c.col1)}, + "bs":relationship(B, cascade="all, delete", + order_by=table2.c.col1)}, order_by=table1.c.col1) m2 = mapper(B, table2) @@ -139,26 +145,36 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30))) table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)), Column('col3', Integer, ForeignKey("mytable.col1"))) metadata.create_all() m1 = mapper(A, table1, properties={ - "bs":relationship(B, cascade="all, delete", order_by=table2.c.col1)}, - order_by=table1.c.col1) - m2 = mapper(B, table2) + "bs":relationship(B, cascade="all, delete", + order_by=table2.c.col1)}, + order_by=table1.c.col1, + _compiled_cache_size=10 + ) + m2 = mapper(B, table2, + _compiled_cache_size=10 + ) m3 = mapper(A, table1, non_primary=True) @profile_memory def go(): - engine = engines.testing_engine(options={'logging_name':'FOO', 'pool_logging_name':'BAR'}) + engine = engines.testing_engine( + options={'logging_name':'FOO', + 'pool_logging_name':'BAR'} + ) sess = create_session(bind=engine) a1 = A(col2="a1") @@ -192,15 +208,61 @@ class MemUsageTest(EnsureZeroed): del m1, m2, m3 assert_no_mappers() + def test_many_updates(self): + metadata = MetaData(testing.db) + + wide_table = Table('t', metadata, + Column('id', Integer, primary_key=True), + *[Column('col%d' % i, Integer) for i in range(10)] + ) + + class Wide(object): + pass + + mapper(Wide, wide_table, _compiled_cache_size=10) + + metadata.create_all() + session = create_session() + w1 = Wide() + session.add(w1) + session.flush() + session.close() + del session + counter = [1] + + @profile_memory + def go(): + session = create_session() + w1 = session.query(Wide).first() + x = counter[0] + dec = 10 + while dec > 0: + # trying to count in binary here, + # works enough to trip the test case + if pow(2, dec) < x: + setattr(w1, 'col%d' % dec, counter[0]) + x -= pow(2, dec) + dec -= 1 + session.flush() + session.close() + counter[0] += 1 + + try: + go() + finally: + metadata.drop_all() + def test_mapper_reset(self): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30))) table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)), Column('col3', Integer, ForeignKey("mytable.col1"))) @@ -251,7 +313,8 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)) ) @@ -311,12 +374,14 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)) ) table2 = Table("mytable2", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', String(30)), ) @@ -333,7 +398,8 @@ class MemUsageTest(EnsureZeroed): pass mapper(A, table1, properties={ - 'bs':relationship(B, secondary=table3, backref='as', order_by=table3.c.t1) + 'bs':relationship(B, secondary=table3, + backref='as', order_by=table3.c.t1) }) mapper(B, table2) @@ -381,12 +447,14 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("table1", metadata, - Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), Column('data', String(30)) ) table2 = Table("table2", metadata, - Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), Column('data', String(30)), Column('t1id', Integer, ForeignKey('table1.id')) ) @@ -420,7 +488,8 @@ class MemUsageTest(EnsureZeroed): metadata = MetaData(testing.db) table1 = Table("mytable", metadata, - Column('col1', Integer, primary_key=True, test_needs_autoincrement=True), + Column('col1', Integer, primary_key=True, + test_needs_autoincrement=True), Column('col2', PickleType(comparator=operator.eq)) ) diff --git a/test/base/test_utils.py b/test/base/test_utils.py index 035e4f268..f9888ef0c 100644 --- a/test/base/test_utils.py +++ b/test/base/test_utils.py @@ -112,6 +112,52 @@ class ColumnCollectionTest(TestBase): assert (cc1==cc2).compare(c1 == c2) assert not (cc1==cc3).compare(c2 == c3) + + +class LRUTest(TestBase): + + def test_lru(self): + class item(object): + def __init__(self, id): + self.id = id + + def __str__(self): + return "item id %d" % self.id + + l = util.LRUCache(10, threshold=.2) + + for id in range(1,20): + l[id] = item(id) + + # first couple of items should be gone + assert 1 not in l + assert 2 not in l + + # next batch over the threshold of 10 should be present + for id_ in range(11,20): + assert id_ in l + + l[12] + l[15] + l[23] = item(23) + l[24] = item(24) + l[25] = item(25) + l[26] = item(26) + l[27] = item(27) + + assert 11 not in l + assert 13 not in l + + for id_ in (25, 24, 23, 14, 12, 19, 18, 17, 16, 15): + assert id_ in l + + i1 = l[25] + i2 = item(25) + l[25] = i2 + assert 25 in l + assert l[25] is i2 + + class ImmutableSubclass(str): pass |