import argparse import asyncio import csv import hashlib import io import statistics import time import uuid from pathlib import Path from PIL import Image from sqlalchemy import select from app.config import S3_BUCKET from app.models.attachment import Attachment from app.models.outbox import OutboxEvent from app.outbox_dispatcher import session_maker from app.worker import minio_client IMAGE_SIZES = [512, 1024, 2048] def make_image(size): image = Image.effect_noise((size, size), 80).convert("RGB") buffer = io.BytesIO() image.save(buffer, format="JPEG", quality=85) return buffer.getvalue() def percentile(values, percent): values = sorted(values) if not values: return None position = (len(values) - 1) * percent / 100 lower = int(position) upper = min(lower + 1, len(values) - 1) return values[lower] + ( values[upper] - values[lower] ) * (position - lower) async def get_source(): async with session_maker() as session: result = await session.execute( select(Attachment) .where(Attachment.status == "ready") .order_by(Attachment.created_at.desc()) .limit(1) ) source = result.scalar_one_or_none() if source is None: raise RuntimeError( "Нет готовой фотографии для создания тестовых записей" ) return source.chat_id, source.user_id async def create_jobs(count_per_size, run_id): chat_id, user_id = await get_source() jobs = [] for size in IMAGE_SIZES: data = make_image(size) print( f"[LOAD] Изображение {size}x{size}: " f"{len(data) / 1024:.1f} KB", flush=True, ) for index in range(count_per_size): key = f"benchmark/{run_id}/{size}_{index}.jpg" minio_client.put_object( S3_BUCKET, key, io.BytesIO(data), len(data), content_type="image/jpeg", ) jobs.append({ "size": size, "key": key, "data": data, }) async with session_maker() as session: async with session.begin(): for job in jobs: data = job["data"] attachment = Attachment( object_key=job["key"], filename=f"benchmark_{job['size']}.jpg", mime_type="image/jpeg", size_bytes=len(data), chat_id=chat_id, user_id=user_id, sha256=hashlib.sha256(data).hexdigest(), status="queued", ) session.add(attachment) await session.flush() session.add( OutboxEvent(attachment_id=attachment.id) ) job["id"] = attachment.id del job["data"] return jobs async def monitor_jobs(jobs, timeout): ids = [job["id"] for job in jobs] started = time.perf_counter() last_report = started first_processing = {} ready_times = {} failed = set() peak_processing = 0 while True: now = time.perf_counter() elapsed = now - started async with session_maker() as session: result = await session.execute( select(Attachment.id, Attachment.status) .where(Attachment.id.in_(ids)) ) statuses = result.all() processing_count = 0 for attachment_id, status in statuses: if status == "processing": processing_count += 1 if attachment_id not in first_processing: first_processing[attachment_id] = elapsed elif status == "ready": if attachment_id not in ready_times: ready_times[attachment_id] = elapsed elif status == "failed": failed.add(attachment_id) peak_processing = max( peak_processing, processing_count, ) completed = len(ready_times) + len(failed) if now - last_report >= 5: print( f"[LOAD] Готово: {len(ready_times)}/{len(jobs)}, " f"failed={len(failed)}, " f"processing={processing_count}, " f"elapsed={elapsed:.1f}s", flush=True, ) last_report = now if completed == len(jobs): break if elapsed >= timeout: print("[LOAD] Достигнут таймаут", flush=True) break await asyncio.sleep(0.1) return { "ready_times": ready_times, "first_processing": first_processing, "failed": failed, "peak_processing": peak_processing, "elapsed": time.perf_counter() - started, } def save_results(jobs, results, run_id): output = Path(f"benchmark_{run_id}.csv") with output.open("w", newline="") as file: writer = csv.writer(file) writer.writerow([ "attachment_id", "image_size", "status", "queue_lag_estimate_s", "time_to_ready_s", ]) for job in jobs: attachment_id = job["id"] ready = results["ready_times"].get(attachment_id) lag = results["first_processing"].get(attachment_id) status = ( "ready" if ready is not None else "failed" if attachment_id in results["failed"] else "unfinished" ) writer.writerow([ str(attachment_id), job["size"], status, round(lag, 3) if lag is not None else "", round(ready, 3) if ready is not None else "", ]) return output def print_results(jobs, results): ready_times = results["ready_times"] first_processing = results["first_processing"] total = len(jobs) completed = len(ready_times) throughput = ( completed / results["elapsed"] if results["elapsed"] > 0 else 0 ) print("\n========== BENCHMARK RESULTS ==========") print(f"Total jobs: {total}") print(f"Ready: {completed}") print(f"Failed: {len(results['failed'])}") print(f"Duration: {results['elapsed']:.2f} s") print(f"Throughput: {throughput:.2f} images/s") print(f"Peak observed processing: {results['peak_processing']}") for size in IMAGE_SIZES: size_jobs = [ job for job in jobs if job["size"] == size ] times = [ ready_times[job["id"]] for job in size_jobs if job["id"] in ready_times ] lags = [ first_processing[job["id"]] for job in size_jobs if job["id"] in first_processing ] print(f"\nImage size: {size}x{size}") print(f"Completed: {len(times)}/{len(size_jobs)}") if times: print(f"Mean ready: {statistics.mean(times):.2f} s") print(f"P95 ready: {percentile(times, 95):.2f} s") if lags: print(f"Mean observed queue lag: {statistics.mean(lags):.2f} s") print(f"Queue lag samples: {len(lags)}/{len(size_jobs)}") print("\n=======================================") async def main(): parser = argparse.ArgumentParser() parser.add_argument( "--count", type=int, default=10, help="Количество изображений каждого размера", ) parser.add_argument( "--timeout", type=int, default=300, ) args = parser.parse_args() if args.count < 1 or args.timeout < 1: parser.error("--count и --timeout должны быть положительными") run_id = uuid.uuid4().hex[:8] print(f"[LOAD] Run ID: {run_id}") print("[LOAD] Подготовка изображений...") jobs = await create_jobs(args.count, run_id) print(f"[LOAD] Создано заданий: {len(jobs)}") print("[LOAD] Ожидание обработки...") results = await monitor_jobs(jobs, args.timeout) print_results(jobs, results) output = save_results(jobs, results, run_id) print(f"\nCSV: {output}") if len(results["ready_times"]) != len(jobs): raise SystemExit(1) if __name__ == "__main__": asyncio.run(main())