def get_speed(col_name, start_time, end_time):
    condition = {
        'insert_time': {
            '$gt': time_util.DatetimeConverter(start_time).datetime_obj,
            '$lt': time_util.DatetimeConverter(end_time).datetime_obj
        },
    }
    # condition = {
    #     'insert_time_str': {'$gt': time_util.DatetimeConverter(time.time() - 60).datetime_str},
    # }
    # nb_print(condition)
    with decorators.TimerContextManager():
        success_num = db.get_collection(col_name).count({
            **{
                'success': True
            },
            **condition
        })
        fail_num = db.get_collection(col_name).count({
            **{
                'success': False
            },
            **condition
        })
        qps = (success_num +
               fail_num) / (time_util.DatetimeConverter(end_time).timestamp -
                            time_util.DatetimeConverter(start_time).timestamp)
        return {
            'success_num': success_num,
            'fail_num': fail_num,
            'qps': round(qps, 1)
        }
 def test_mongo_bulk_write(self):
     # col = MongoMixin().mongo_16_client.get_database('test').get_collection('ydf_test2')
     col = MongoClient().get_database('test').get_collection('ydf_test2')
     with decorators.TimerContextManager():
         for i in range(50000 + 13):
             # time.sleep(0.01)
             item = {'_id': i, 'field1': i * 2}
             mongo_helper = MongoBulkWriteHelper(col, 10000, is_print_log=True)
             mongo_helper.add_task(UpdateOne({'_id': item['_id']}, {'$set': item}, upsert=True))
    def test_redis_bulk_write(self):

        with decorators.TimerContextManager():
            # r = redis.Redis(password='******')
            r = RedisMixin().redis_db0
            redis_helper = RedisBulkWriteHelper(r, 200)
            # redis_helper = RedisBulkWriteHelper(r, 100)  # 放在外面可以
            for i in range(1003):
                # time.sleep(0.2)
                # 也可以在这里无限实例化
                redis_helper.add_task(RedisOperation('sadd', 'key1', str(i)))
    def build_result(self):
        with decorators.TimerContextManager():
            for i in range(10):
                t1 = datetime.datetime.now() + datetime.timedelta(
                    days=-(9 - i))
                t2 = datetime.datetime.now() + datetime.timedelta(
                    days=-(8 - i))
                self.result['recent_10_days']['time_arr'].append(
                    time_util.DatetimeConverter(t1).date_str)
                count = self.statistic_by_period(
                    time_util.DatetimeConverter(t1).date_str + ' 00:00:00',
                    time_util.DatetimeConverter(t2).date_str + ' 00:00:00')
                self.result['recent_10_days']['count_arr'].append(count)

            for i in range(0, 24):
                t1 = datetime.datetime.now() + datetime.timedelta(
                    hours=-(23 - i))
                t2 = datetime.datetime.now() + datetime.timedelta(
                    hours=-(22 - i))
                self.result['recent_24_hours']['time_arr'].append(
                    t1.strftime('%Y-%m-%d %H:00:00'))
                # hour1_str = f'0{i}' if i < 10 else i
                count = self.statistic_by_period(
                    t1.strftime('%Y-%m-%d %H:00:00'),
                    t2.strftime('%Y-%m-%d %H:00:00'))
                self.result['recent_24_hours']['count_arr'].append(count)

            for i in range(0, 60):
                t1 = datetime.datetime.now() + datetime.timedelta(
                    minutes=-(59 - i))
                t2 = datetime.datetime.now() + datetime.timedelta(
                    minutes=-(58 - i))
                self.result['recent_60_minutes']['time_arr'].append(
                    t1.strftime('%Y-%m-%d %H:%M:00'))
                count = self.statistic_by_period(
                    t1.strftime('%Y-%m-%d %H:%M:00'),
                    t2.strftime('%Y-%m-%d %H:%M:00'))
                self.result['recent_60_minutes']['count_arr'].append(count)

            for i in range(0, 60):
                t1 = datetime.datetime.now() + datetime.timedelta(
                    seconds=-(59 - i))
                t2 = datetime.datetime.now() + datetime.timedelta(
                    seconds=-(58 - i))
                self.result['recent_60_seconds']['time_arr'].append(
                    t1.strftime('%Y-%m-%d %H:%M:%S'))
                count = self.statistic_by_period(
                    t1.strftime('%Y-%m-%d %H:%M:%S'),
                    t2.strftime('%Y-%m-%d %H:%M:%S'))
                self.result['recent_60_seconds']['count_arr'].append(count)
 def test_mongo_bulk_write(self):
     # col = MongoMixin().mongo_16_client.get_database('test').get_collection('ydf_test2')
     col = MongoClient(
         'mongodb://*****:*****@192.168.199.202:27016/admin'
     ).get_database('test').get_collection('ydf_test3')
     with decorators.TimerContextManager():
         for i in range(5000 + 13):
             # time.sleep(0.01)
             item = {'_id': i, 'field1': i * 2}
             mongo_helper = MongoBulkWriteHelper(col,
                                                 100,
                                                 is_print_log=True)
             # mongo_helper.add_task(UpdateOne({'_id': item['_id']}, {'$set': item}, upsert=True))
             mongo_helper.add_task(InsertOne({'_id': item['_id']}))