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
|
|
|
|
|
2024-01-05 13:20:10 +03:00
|
|
|
from django.utils.timezone import now
|
2018-11-29 13:19:25 +03:00
|
|
|
|
|
|
|
# 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
|
2021-07-08 10:27:35 +03:00
|
|
|
from django.db.models import F # noqa: E402
|
|
|
|
from tests.clickhouse_models import ClickHouseCollapseTestModel # noqa: E402
|
|
|
|
from tests.models import TestModel # noqa: E402
|
2018-11-29 13:19:25 +03:00
|
|
|
|
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([
|
2024-01-05 13:20:10 +03:00
|
|
|
TestModel(created=now(), created_date='2018-01-01', value=iteration * batch_size + i)
|
2019-01-14 10:45:46 +03:00
|
|
|
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:
|
2024-01-05 13:20:10 +03:00
|
|
|
start = now()
|
|
|
|
while (now() - start).total_seconds() < test_time:
|
2018-12-03 14:41:34 +03:00
|
|
|
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())
|
|
|
|
|
2019-02-03 19:05:01 +03:00
|
|
|
# Disable registering not needed models
|
|
|
|
TestModel._clickhouse_sync_models = {ClickHouseCollapseTestModel}
|
|
|
|
|
2018-11-29 13:19:25 +03:00
|
|
|
func_name = params['process']
|
|
|
|
method = locals()[func_name]
|
|
|
|
method(**params)
|