Завершить уровень 4: проверить отказы, поочерёдную замену и ограничить ожидания кэша
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""Внутрисетевой генератор: постоянное HTTP-соединение на поток, без повторов POST."""
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from collections import Counter
|
||||
import argparse
|
||||
import http.client
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client
|
||||
|
||||
p = argparse.ArgumentParser()
|
||||
p.add_argument('--duration', type=float, default=20)
|
||||
p.add_argument('--concurrency', type=int, default=32)
|
||||
p.add_argument('--warmup', type=float, default=5)
|
||||
p.add_argument('--action', choices=['none', 'rolling'], default='none')
|
||||
args = p.parse_args()
|
||||
c = Client('http://lb:8080')
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
||||
for i in range(100):
|
||||
c.message(alice, chat, 'Начальная история %d' % i)
|
||||
barrier = threading.Barrier(args.concurrency)
|
||||
|
||||
|
||||
def worker(worker_id):
|
||||
conn = http.client.HTTPConnection('lb', 8080, timeout=5)
|
||||
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + alice['token']}
|
||||
barrier.wait()
|
||||
start = time.monotonic()
|
||||
measure_start = start + args.warmup
|
||||
deadline = measure_start + args.duration
|
||||
values, failures, instances = [], Counter(), Counter()
|
||||
warm_count = 0
|
||||
confirmed_ids = set()
|
||||
while time.monotonic() < deadline:
|
||||
before = time.monotonic()
|
||||
measured = before >= measure_start
|
||||
headers['Idempotency-Key'] = uuid.uuid4().hex
|
||||
try:
|
||||
conn.request('POST', path, json.dumps({'text': 'Нагрузка ' + 'я' * 240}).encode(), headers)
|
||||
response = conn.getresponse()
|
||||
raw = response.read()
|
||||
payload = json.loads(raw) if response.status == 201 else {}
|
||||
if response.status == 201 and payload.get('chat_id') == chat['id']:
|
||||
confirmed_ids.add(payload['id'])
|
||||
if measured:
|
||||
values.append(time.monotonic() - before)
|
||||
instances[response.getheader('X-Instance-Id')] += 1
|
||||
else:
|
||||
warm_count += 1
|
||||
elif measured:
|
||||
failures[str(response.status)] += 1
|
||||
except (OSError, ValueError, http.client.HTTPException):
|
||||
conn.close()
|
||||
if measured:
|
||||
failures['connection'] += 1
|
||||
conn.close()
|
||||
return values, failures, instances, warm_count, time.monotonic()-measure_start, confirmed_ids
|
||||
|
||||
|
||||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
||||
results = list(pool.map(worker, range(args.concurrency)))
|
||||
values = sorted(v for r in results for v in r[0])
|
||||
errors, instances = Counter(), Counter()
|
||||
for r in results:
|
||||
errors.update(r[1])
|
||||
instances.update(r[2])
|
||||
elapsed = max(r[4] for r in results)
|
||||
confirmed = set().union(*(r[5] for r in results))
|
||||
seen = set()
|
||||
cursor = None
|
||||
from urllib.parse import quote
|
||||
for _ in range(10000):
|
||||
page = c.call('GET', path+'?limit=100'+('&cursor='+quote(cursor, safe='') if cursor else ''), actor=bob, schema='MessagePage')['json']
|
||||
for item in page['items']:
|
||||
if item['id'] in seen:
|
||||
raise AssertionError('Дубль сообщения в истории')
|
||||
seen.add(item['id'])
|
||||
cursor = page['next_cursor']
|
||||
if cursor is None:
|
||||
break
|
||||
else:
|
||||
raise AssertionError('Превышена граница обхода истории')
|
||||
if not confirmed.issubset(seen):
|
||||
raise AssertionError('Потеря подтверждённого сообщения')
|
||||
|
||||
|
||||
def percentile(q):
|
||||
return round(values[max(0, math.ceil(q*len(values))-1)]*1000, 3) if values else None
|
||||
|
||||
|
||||
print(json.dumps({'duration': args.duration, 'warmup': args.warmup, 'elapsed': elapsed,
|
||||
'concurrency': args.concurrency, 'successful_requests': len(values), 'failed_requests': sum(errors.values()),
|
||||
'errors': dict(errors), 'error_ratio': sum(errors.values()) / max(1, len(values)+sum(errors.values())),
|
||||
'successful_rps': len(values)/elapsed, 'successful_latency_ms': {'p50': percentile(.5), 'p95': percentile(.95), 'p99': percentile(.99)},
|
||||
'instances': dict(instances), 'initial_chat_messages': 100,
|
||||
'confirmed_ids_checked': len(confirmed), 'history_count_after': len(seen), 'confirmed_data_preserved': True,
|
||||
'warmup_messages': sum(r[3] for r in results), 'final_chat_messages_expected_min': 100+sum(r[3] for r in results)+len(values),
|
||||
'note': 'Closed-loop; coordinated omission; percentiles cover successes; no retries; uncertain writes may exist.'}, ensure_ascii=False))
|
||||
Reference in New Issue
Block a user