From: Kevin Benton Date: Wed, 17 Sep 2014 00:29:51 +0000 (-0700) Subject: Delete DB records instead of tables to speedup UT X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=34cf04a0ea84f4e5b32aa8d45108bd2a38bcff58;p=openstack-build%2Fneutron-build.git Delete DB records instead of tables to speedup UT Now that the schema is fixed for all of the plugins, there isn't a need to delete and recreate the entire schema for every unit test. This patch clears the tables at the end of each test instead of deleting them. This eliminated overhead seems to save 10%+ execution time of the entire set of unit tests. Example of performance gain from tox -epy27 tests.unit.ml2: Before: Ran 2495 tests in 284.186s After: Ran 2495 tests in 223.299s Change-Id: Ic5bcbb0cf941e0745890abc776d719e58bb42e35 --- diff --git a/neutron/tests/unit/testlib_api.py b/neutron/tests/unit/testlib_api.py index a8c7a7e6e..564e0b501 100644 --- a/neutron/tests/unit/testlib_api.py +++ b/neutron/tests/unit/testlib_api.py @@ -55,17 +55,24 @@ def create_request(path, body, content_type, method='GET', class SqlTestCase(base.BaseTestCase): + # flag to indicate that the models have been loaded + _TABLES_ESTABLISHED = False + def setUp(self): super(SqlTestCase, self).setUp() # Register all data models engine = db_api.get_engine() - model_base.BASEV2.metadata.create_all(engine) + if not SqlTestCase._TABLES_ESTABLISHED: + model_base.BASEV2.metadata.create_all(engine) + SqlTestCase._TABLES_ESTABLISHED = True - def unregister_models(): - """Unregister all data models.""" - model_base.BASEV2.metadata.drop_all(engine) + def clear_tables(): + with engine.begin() as conn: + for table in reversed( + model_base.BASEV2.metadata.sorted_tables): + conn.execute(table.delete()) - self.addCleanup(unregister_models) + self.addCleanup(clear_tables) class WebTestCase(SqlTestCase):