"""Closed-loop генератор POST сообщений с записью каждого запроса. В отличие от tests/load.py, сохраняет для каждого запроса X-Request-Id, время начала, длительность и исход — чтобы потом проверить, попала ли именно эта трасса в Tempo (трек 1: какие ошибки и медленные запросы теряет sampling). Токены не сохраняются. """ import math import threading import time import uuid from concurrent.futures import ThreadPoolExecutor import labutil # noqa: F401 (добавляет tests/ в sys.path) from client import Client, Failure class Load: def __init__(self, base_url, ca_file, actor, chat, concurrency=4, timeout=10, prefix='load'): self.base_url, self.ca_file = base_url, ca_file self.actor, self.chat = actor, chat self.concurrency, self.timeout, self.prefix = concurrency, timeout, prefix self.records = [] # (rid, t_start_epoch, seconds, status) status: int или 'error' self._lock = threading.Lock() self._stop = threading.Event() self._pool = None self._futures = [] def _worker(self, wid): c = Client(self.base_url, self.ca_file, timeout=self.timeout) i = 0 while not self._stop.is_set(): rid = '%s-%s' % (self.prefix, uuid.uuid4().hex[:20]) t0, m0 = time.time(), time.monotonic() status = 'error' try: r = c.call('POST', '/api/v1/chats/%s/messages' % self.chat['id'], 201, actor=self.actor, body={'text': 'load %d-%d' % (wid, i)}, headers={'Idempotency-Key': uuid.uuid4().hex, 'X-Request-Id': rid}) status = r['status'] except Failure as e: msg = str(e) status = 'error' for code in (500, 502, 503, 504): if str(code) in msg: status = code break except Exception: status = 'error' with self._lock: self.records.append((rid, t0, time.monotonic() - m0, status)) i += 1 def start(self): self._pool = ThreadPoolExecutor(max_workers=self.concurrency) self._futures = [self._pool.submit(self._worker, w) for w in range(self.concurrency)] self.started = time.time() return self def stop(self): self._stop.set() for f in self._futures: f.result() self._pool.shutdown() self.stopped = time.time() return self def summary(self, exclude=()): """exclude: [(t_from, t_to)] — окна внедрённых сбоев для «нормальной» задержки.""" recs = self.records ok = sorted(d for _, t, d, s in recs if s == 201) normal = sorted(d for _, t, d, s in recs if s == 201 and not any(a <= t <= b for a, b in exclude)) err = sum(1 for r in recs if r[3] != 201) elapsed = max(1e-9, self.stopped - self.started) def pct(v, p): return round(1000 * v[max(0, math.ceil(p * len(v)) - 1)], 2) if v else None return { 'requests': len(recs), 'ok': len(ok), 'errors': err, 'error_ratio': round(err / len(recs), 5) if recs else None, 'ok_rps': round(len(ok) / elapsed, 1), 'seconds': round(elapsed, 1), 'latency_ms_all_ok': {'p50': pct(ok, .5), 'p95': pct(ok, .95), 'p99': pct(ok, .99)}, 'latency_ms_outside_faults': {'p50': pct(normal, .5), 'p95': pct(normal, .95), 'p99': pct(normal, .99)}, }