forked from mtusi2026/mtusi2026lab4
80 lines
3.3 KiB
Python
80 lines
3.3 KiB
Python
"""Ten simultaneous uploads with kernel cgroup peak-memory measurements."""
|
|
import concurrent.futures
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import random
|
|
import struct
|
|
import threading
|
|
import time
|
|
|
|
from verify_attachments import ROOT, Client, check, compose, png_chunk, run, upload, wait_ready
|
|
|
|
|
|
def fixture():
|
|
# 9M pixels; random rows make a ~6.8 MiB valid PNG without another library.
|
|
rng = random.Random(2026)
|
|
import zlib
|
|
compressor = zlib.compressobj(1)
|
|
parts = []
|
|
for row in range(3000):
|
|
pixels = rng.randbytes(9000) if row % 4 == 0 else bytes(9000)
|
|
parts.append(compressor.compress(b"\x00" + pixels))
|
|
parts.append(compressor.flush())
|
|
return (b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", struct.pack(">IIBBBBB", 3000, 3000, 8, 2, 0, 0, 0))
|
|
+ png_chunk(b"IDAT", b"".join(parts)) + png_chunk(b"IEND", b""))
|
|
|
|
|
|
def memory():
|
|
return {service: json.loads(compose("exec", "-T", service, "/messenger", "memory"))
|
|
for service in ("api1", "api2")}
|
|
|
|
|
|
def main():
|
|
client = Client(timeout=60)
|
|
actor, _, _, chat = client.bootstrap()
|
|
# A new container cgroup resets memory.peak for this experiment.
|
|
compose("up", "-d", "--no-deps", "--force-recreate", "--wait", "api1", "api2")
|
|
compose("exec", "-T", "nginx", "nginx", "-s", "reload")
|
|
wait_ready(client)
|
|
raw = fixture()
|
|
digest = hashlib.sha256(raw).hexdigest()
|
|
before = memory()
|
|
barrier = threading.Barrier(11)
|
|
|
|
def send(number):
|
|
c = Client(timeout=60)
|
|
barrier.wait()
|
|
started = time.monotonic()
|
|
response = upload(c, actor, chat, raw, filename="load-%d.png" % number)
|
|
check(response["json"]["sha256"] == digest, "concurrent upload checksum mismatch")
|
|
return response, time.monotonic() - started
|
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
|
|
futures = [pool.submit(send, number) for number in range(10)]
|
|
started = time.monotonic()
|
|
barrier.wait()
|
|
responses = [future.result() for future in futures]
|
|
elapsed = time.monotonic() - started
|
|
after = memory()
|
|
for metrics in after.values():
|
|
check(metrics["memory.peak"] < metrics["memory.max"], "upload memory reached container limit")
|
|
for response, _ in responses:
|
|
downloaded = client.call("GET", "/api/v1/attachments/" + response["json"]["id"] + "/content", actor=actor)
|
|
check(downloaded["bytes"] == raw, "concurrent original changed")
|
|
times = sorted(duration * 1000 for _, duration in responses)
|
|
output = {"concurrent_uploads": 10, "file_size_bytes": len(raw), "pixels": 9000000,
|
|
"successful_uploads": 10, "failed_uploads": 0,
|
|
"elapsed_seconds": round(elapsed, 3), "successful_rps": round(10 / elapsed, 3),
|
|
"latency_ms": {"p50": round(times[4], 3), "p95": round(times[math.ceil(.95 * 10) - 1], 3), "p99": round(times[9], 3)},
|
|
"memory_before_bytes": before, "memory_after_bytes": after,
|
|
"peak_source": "Linux cgroup v2 memory.peak; fresh API containers including startup",
|
|
"decoder_slots_per_replica": 1,
|
|
"instances": sorted({response["headers"]["X-Instance-Id"] for response, _ in responses})}
|
|
(ROOT / "evidence/upload-memory.json").write_text(json.dumps(output, indent=2) + "\n")
|
|
print("PASS:", json.dumps(output))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|