feat(track2): assert dataset size and print representative chat ids
This commit is contained in:
@@ -4,12 +4,13 @@ import base64
|
|||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
sys.path.insert(0, str(ROOT / 'tests'))
|
sys.path.insert(0, str(ROOT / 'tests'))
|
||||||
|
|
||||||
from client import Client, arguments, run # noqa: E402
|
from client import Client, arguments, check, run # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
def make_user(c, label):
|
def make_user(c, label):
|
||||||
@@ -24,19 +25,29 @@ def make_user(c, label):
|
|||||||
return {'id': user['id'], 'username': username, 'token': s['token']}
|
return {'id': user['id'], 'username': username, 'token': s['token']}
|
||||||
|
|
||||||
|
|
||||||
def seed_chat(base_url, ca_file, alice, bob, title, count):
|
def seed_chat(base_url, ca_file, alice, bob, title, count, workers=8):
|
||||||
c = Client(base_url, ca_file)
|
c = Client(base_url, ca_file)
|
||||||
chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={
|
chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={
|
||||||
'title': title, 'member_ids': [alice['id'], bob['id']]},
|
'title': title, 'member_ids': [alice['id'], bob['id']]},
|
||||||
schema='Chat')['json']
|
schema='Chat')['json']
|
||||||
chat_id = chat['id']
|
chat_id = chat['id']
|
||||||
|
|
||||||
|
per_worker = count // workers
|
||||||
|
remainder = count - per_worker * workers
|
||||||
|
|
||||||
|
def worker(wid):
|
||||||
|
local = Client(base_url, ca_file, timeout=20)
|
||||||
|
n = per_worker + (1 if wid < remainder else 0)
|
||||||
|
for i in range(n):
|
||||||
|
local.call('POST', f'/api/v1/chats/{chat_id}/messages', 201,
|
||||||
|
actor=alice, body={'text': f'{title} #{wid}-{i}'},
|
||||||
|
schema='Message')
|
||||||
|
|
||||||
started = time.monotonic()
|
started = time.monotonic()
|
||||||
for i in range(count):
|
with ThreadPoolExecutor(max_workers=workers) as pool:
|
||||||
c.call('POST', f'/api/v1/chats/{chat_id}/messages', 201,
|
list(pool.map(worker, range(workers)))
|
||||||
actor=alice, body={'text': f'{title} #{i}'}, schema='Message')
|
|
||||||
elapsed = time.monotonic() - started
|
elapsed = time.monotonic() - started
|
||||||
print(f' {title}: {count} сообщений за {elapsed:.1f}s')
|
print(f' {title}: {count} сообщений за {elapsed:.1f}s ({count/max(elapsed,0.001):.0f}/s)')
|
||||||
return chat_id
|
return chat_id
|
||||||
|
|
||||||
|
|
||||||
@@ -46,7 +57,25 @@ def main():
|
|||||||
alice = make_user(c, 'track2_a')
|
alice = make_user(c, 'track2_a')
|
||||||
bob = make_user(c, 'track2_b')
|
bob = make_user(c, 'track2_b')
|
||||||
|
|
||||||
seed_chat(args.base_url, args.ca_file, alice, bob, 'hot', 1000)
|
plan = (
|
||||||
|
[('hot', 55_000)] +
|
||||||
|
[('medium-%d' % i, 10_000) for i in range(1, 4)] +
|
||||||
|
[('small-%d' % i, 1_000) for i in range(1, 11)] +
|
||||||
|
[('tiny-%d' % i, 500) for i in range(1, 11)]
|
||||||
|
)
|
||||||
|
total = sum(n for _, n in plan)
|
||||||
|
check(total == 100_000, f'сумма {total} != 100000')
|
||||||
|
|
||||||
|
result = {}
|
||||||
|
started = time.monotonic()
|
||||||
|
for title, count in plan:
|
||||||
|
result[title] = seed_chat(args.base_url, args.ca_file, alice, bob, title, count)
|
||||||
|
elapsed = time.monotonic() - started
|
||||||
|
|
||||||
|
print(f'\nВсего {total} сообщений за {elapsed:.1f}s ({total/elapsed:.0f}/s)')
|
||||||
|
print('chat_id для EXPLAIN (сохраните эти значения):')
|
||||||
|
for title in ('hot', 'medium-1', 'small-1', 'tiny-1'):
|
||||||
|
print(f' {title}: {result[title]}')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user