2018-12-03 14:41:34 +03:00
|
|
|
import logging
|
2018-11-29 13:19:25 +03:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import django
|
|
|
|
import os
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
# set Django environment
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'tests.settings')
|
|
|
|
django.setup()
|
|
|
|
|
|
|
|
# This imports must be after django activation
|
|
|
|
from django.db.models import F
|
|
|
|
from tests.clickhouse_models import ClickHouseCollapseTestModel
|
|
|
|
from tests.models import TestModel
|
|
|
|
|
2018-12-03 14:41:34 +03:00
|
|
|
logger = logging.getLogger('django-clickhouse')
|
|
|
|
|
2018-11-29 13:19:25 +03:00
|
|
|
|
|
|
|
def create(batch_size=1000, test_time=60, period=1, **kwargs):
|
|
|
|
for iteration in range(int(test_time / period)):
|
2018-12-03 14:41:34 +03:00
|
|
|
res = TestModel.objects.db_manager('test_db').bulk_create([
|
2019-01-14 10:45:46 +03:00
|
|
|
TestModel(created=datetime.datetime.now(), created_date='2018-01-01', value=iteration * batch_size + i)
|
|
|
|
for i in range(batch_size)
|
2018-11-29 13:19:25 +03:00
|
|
|
])
|
2018-12-03 14:41:34 +03:00
|
|
|
logger.info('django-clickhouse: test created %d records' % len(res))
|
2018-11-29 13:19:25 +03:00
|
|
|
sleep(period)
|
|
|
|
|
|
|
|
|
2018-12-03 14:41:34 +03:00
|
|
|
def update(batch_size=500, test_time=60, period=1, **kwargs):
|
2018-11-29 13:19:25 +03:00
|
|
|
for iteration in range(int(test_time / period)):
|
2018-12-03 14:41:34 +03:00
|
|
|
updated = TestModel.objects.db_manager('test_db').\
|
|
|
|
filter(value__gte=iteration * batch_size, value__lt=(iteration + 1) * batch_size).\
|
|
|
|
annotate(valmod10=F('value') % 10).filter(valmod10=0).update(value=-1)
|
|
|
|
logger.debug('django-clickhouse: test updated %d records' % updated)
|
2018-11-29 13:19:25 +03:00
|
|
|
sleep(period)
|
|
|
|
|
|
|
|
|
2018-12-03 14:41:34 +03:00
|
|
|
def delete(batch_size=500, test_time=60, period=1, **kwargs):
|
2018-11-29 13:19:25 +03:00
|
|
|
for iteration in range(int(test_time / period)):
|
2018-12-03 14:41:34 +03:00
|
|
|
deleted, _ = TestModel.objects.db_manager('test_db'). \
|
|
|
|
filter(value__gte=iteration * batch_size, value__lt=(iteration + 1) * batch_size). \
|
|
|
|
annotate(valmod10=F('value') % 10).filter(valmod10=1).delete()
|
|
|
|
logger.debug('django-clickhouse: test deleted %d records' % deleted)
|
2018-11-29 13:19:25 +03:00
|
|
|
sleep(period)
|
|
|
|
|
|
|
|
|
|
|
|
def sync(period=1, test_time=60, **kwargs):
|
2018-12-03 14:41:34 +03:00
|
|
|
if kwargs['once']:
|
2018-11-29 13:19:25 +03:00
|
|
|
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
2018-12-03 14:41:34 +03:00
|
|
|
else:
|
|
|
|
start = datetime.datetime.now()
|
|
|
|
while (datetime.datetime.now() - start).total_seconds() < test_time:
|
|
|
|
ClickHouseCollapseTestModel.sync_batch_from_storage()
|
|
|
|
sleep(period)
|
2018-11-29 13:19:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('process', type=str, choices=('sync', 'create', 'update', 'delete'))
|
|
|
|
parser.add_argument('--test-time', type=int, required=False, default=60)
|
2018-12-03 14:41:34 +03:00
|
|
|
parser.add_argument('--batch-size', type=int, required=False, default=1000)
|
|
|
|
parser.add_argument('--period', type=int, required=False, default=1)
|
|
|
|
parser.add_argument('--once', type=bool, required=False, default=False)
|
2018-11-29 13:19:25 +03:00
|
|
|
params = vars(parser.parse_args())
|
|
|
|
|
|
|
|
func_name = params['process']
|
|
|
|
method = locals()[func_name]
|
|
|
|
method(**params)
|