56 lines
1.9 KiB
Python
56 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Заполняет один чат N сообщениями. Надо EXPLAIN."""
|
|
import base64
|
|
import sys
|
|
import time
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
|
|
from client import Client, arguments, check, run # noqa: E402
|
|
|
|
|
|
def make_user(c, label):
|
|
username = f'{label}_{uuid.uuid4().hex[:12]}'
|
|
password = 'T3st-' + uuid.uuid4().hex
|
|
user = c.call('POST', '/api/v1/users', 201, body={
|
|
'username': username, 'display_name': label.capitalize(),
|
|
'password': password}, schema='User')['json']
|
|
cred = base64.b64encode(f'{username}:{password}'.encode()).decode()
|
|
s = c.call('POST', '/api/v1/auth/sessions', 201,
|
|
headers={'Authorization': 'Basic ' + cred}, schema='Session')['json']
|
|
return {'id': user['id'], 'token': s['token']}
|
|
|
|
|
|
def main():
|
|
def configure(p):
|
|
p.add_argument('--count', type=int, default=10000)
|
|
|
|
args = arguments('Сид для EXPLAIN: создаёт count сообщений в новом чате', configure)
|
|
check(args.count >= 1 and args.count <= 1_000_000, 'count вне диапазона')
|
|
|
|
c = Client(args.base_url, args.ca_file)
|
|
alice = make_user(c, 'seed_a')
|
|
bob = make_user(c, 'seed_b')
|
|
|
|
chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={
|
|
'title': 'Seed chat', 'member_ids': [alice['id'], bob['id']]},
|
|
schema='Chat')['json']
|
|
|
|
started = time.monotonic()
|
|
for i in range(args.count):
|
|
c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 201,
|
|
actor=alice, body={'text': f'seed message {i}'}, schema='Message')
|
|
if (i + 1) % 1000 == 0:
|
|
print(f' {i + 1}/{args.count}')
|
|
|
|
elapsed = time.monotonic() - started
|
|
print(f'seeded {args.count} messages in {elapsed:.1f}s '
|
|
f'({args.count / elapsed:.0f}/s)')
|
|
print(f'chat_id={chat["id"]}')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main) |