重置MongoDB服务器/删除所有数据库的最快方法

时间:2016-04-25 15:31:44

标签: python mongodb pymongo

我正在使用MongoBox开始测试MongoDB服务器,在每个测试方法之前,我正在重置它:

class BaseTest(unittest.TestCase):

    def setUp(self):
        self.purge_database()

    @cached_property
    def mongo_client(self):
        return pymongo.mongo_client.MongoClient(host=settings.DEFAULT_DB)

    def purge_database(self, drop=True):
        for db_name in self.mongo_client.database_names():
            db = getattr(self.mongo_client, db_name)
            db.eval("""
                db.getCollectionNames().forEach( function(collection_name) {
                    if (collection_name.indexOf("system.") == -1)
                        db[collection_name].drop();
                    else
                        db.collection_name.remove({});
                });
            """)

我的测试运行得太慢了:

Ran 311 tests in 685.171s

此变体

def purge_database(self, drop=True):
    for db_name in self.mongo_client.database_names():
        self.mongo_client.drop_database(db_name)

也很慢

Ran 311 tests in 678.837s

这个

    # exclude system databases
    database_names = (
        db_name for db_name in
        self.mongo_client.database_names()
        if db_name not in ['local']
    )

    for db_name in database_names:
        db = self.mongo_client[db_name]

        # exclude system collections
        collections = (
            db[c] for c in db.collection_names()
            if not c.startswith('system.')
        )
        for collection in collections:
            if drop:
                db.drop_collection(collection)
            else:
                try:
                    collection.remove(None)
                except pymongo.errors.OperationFailure:
                    if collection.options().get('capped', False):
                        # cannot remove documents from capped collections
                        # in latest version of Mongo. Dropping instead.
                        db.drop_collection(collection)

太:

Ran 311 tests in 657.224s

您是否知道快速清除每种测试方法的数据库的其他方法?

1 个答案:

答案 0 :(得分:1)

我发现至少修复回归的唯一方法是在测试数据库中使用MMAPv1存储引擎:

mongod ... --storageEngine=mmapv1

这有助于回到最初的测试运行时间,因为新的3.2 MongoDB版本默认使用WiredTiger存储引擎,并且删除数据库/集合的速度很慢。