Files
mtusi2026lab4/tests/custom/concurrent_uploads.py
T
2026-11-06 19:06:21 +03:00

130 lines
4.1 KiB
Python

#!/usr/bin/env python3
"""Upload ten or more large-but-valid PNGs at the same time.
The payload is generated in memory and shared read-only between threads, so the
client itself does not multiply the image. This keeps the measurement focused on
API memory: streaming temp files plus bounded image decoding.
"""
import argparse
import hashlib
import json
import os
import struct
import sys
import threading
import time
import zlib
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check # noqa: E402
def chunk(kind, payload):
return (
struct.pack(">I", len(payload))
+ kind
+ payload
+ struct.pack(">I", zlib.crc32(kind + payload) & 0xFFFFFFFF)
)
def make_png(width, height):
compressor = zlib.compressobj(1)
rows = bytearray()
for _ in range(height):
rows += b"\x00" + os.urandom(width * 3)
body = compressor.compress(bytes(rows)) + compressor.flush()
return (
b"\x89PNG\r\n\x1a\n"
+ chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
+ chunk(b"IDAT", body)
+ chunk(b"IEND", b"")
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--ca-file", default=None)
parser.add_argument("--uploads", type=int, default=10)
parser.add_argument("--width", type=int, default=1800)
parser.add_argument("--height", type=int, default=1400)
args = parser.parse_args()
c = Client(args.base_url, args.ca_file)
alice, _, _, chat = c.bootstrap()
png = make_png(args.width, args.height)
digest = hashlib.sha256(png).hexdigest()
check(len(png) <= 10 * 1024 * 1024, "generated fixture exceeds the upload limit")
barrier = threading.Barrier(args.uploads)
results = []
errors = []
lock = threading.Lock()
def worker(index):
client = Client(args.base_url, args.ca_file)
try:
barrier.wait(timeout=30)
response = client.call(
"POST",
f"/api/v1/chats/{chat['id']}/attachments?filename=concurrent-{index}.png",
201,
actor=alice,
raw=png,
headers={"Content-Type": "image/png"},
schema="Attachment",
)
with lock:
results.append(
{
"id": response["json"]["id"],
"instance": response["headers"].get("X-Instance-Id"),
}
)
except Exception as error: # noqa: BLE001 - report every worker failure
with lock:
errors.append(f"{type(error).__name__}: {error}")
threads = [threading.Thread(target=worker, args=(i,)) for i in range(args.uploads)]
started = time.monotonic()
for thread in threads:
thread.start()
for thread in threads:
thread.join()
elapsed = time.monotonic() - started
check(not errors, "concurrent uploads failed: " + "; ".join(errors[:3]))
check(len(results) == args.uploads, "not all concurrent uploads completed")
instances = sorted({item["instance"] for item in results if item["instance"]})
check(bool(instances), "no X-Instance-Id was observed")
# Read one uploaded object back and compare the exact bytes before reporting
# success; ten 201 responses alone are not enough.
download = c.call(
"GET", f"/api/v1/attachments/{results[0]['id']}/content", actor=alice
)
check(
hashlib.sha256(download["bytes"]).hexdigest() == digest,
"downloaded object differs from the generated original",
)
report = {
"uploads": args.uploads,
"width": args.width,
"height": args.height,
"image_bytes": len(png),
"sha256": digest,
"elapsed_seconds": round(elapsed, 3),
"instances": instances,
"download_verified": True,
}
print(json.dumps(report))
if __name__ == "__main__":
main()