forked from mtusi2026/mtusi2026lab4
Лабораторная 4
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python3
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from PIL import Image
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
def png_bytes(size=(512, 512)):
|
||||
out = io.BytesIO()
|
||||
Image.new("RGBA", size, (30, 90, 170, 255)).save(out, format="PNG")
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
def docker_memory():
|
||||
result = subprocess.run(
|
||||
["docker", "stats", "--no-stream", "--format", "{{json .}}", "mtusi2026lab4-api1-1", "mtusi2026lab4-api2-1"],
|
||||
capture_output=True, text=True, check=False,
|
||||
)
|
||||
rows = []
|
||||
for line in result.stdout.splitlines():
|
||||
try:
|
||||
rows.append(json.loads(line))
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
return rows
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments("10 concurrent attachment uploads; Docker memory snapshot")
|
||||
c = Client(args.base_url, args.ca_file)
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
payload = png_bytes()
|
||||
before = docker_memory()
|
||||
|
||||
def upload(index):
|
||||
local = Client(args.base_url, args.ca_file, timeout=30)
|
||||
started = time.monotonic()
|
||||
response = local.call(
|
||||
"POST", f"/api/v1/chats/{chat['id']}/attachments?filename=concurrent-{index}.png",
|
||||
201, actor=alice, raw=payload,
|
||||
headers={"Content-Type": "image/png"},
|
||||
schema="Attachment",
|
||||
)
|
||||
return response["elapsed"], response["json"]["sha256"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
results = list(pool.map(upload, range(10)))
|
||||
time.sleep(1)
|
||||
after = docker_memory()
|
||||
|
||||
latencies = [x[0] for x in results]
|
||||
report = {
|
||||
"concurrency": 10,
|
||||
"payload_bytes": len(payload),
|
||||
"successful_uploads": len(results),
|
||||
"latency_seconds": {
|
||||
"min": round(min(latencies), 3),
|
||||
"max": round(max(latencies), 3),
|
||||
"avg": round(sum(latencies) / len(latencies), 3),
|
||||
},
|
||||
"docker_stats_before": before,
|
||||
"docker_stats_after": after,
|
||||
}
|
||||
print(json.dumps(report, ensure_ascii=False, indent=2))
|
||||
Path = __import__("pathlib").Path
|
||||
evidence = Path(__file__).resolve().parents[1] / "evidence" / "attachment-load.json"
|
||||
evidence.parent.mkdir(parents=True, exist_ok=True)
|
||||
evidence.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
check(len(results) == 10, "not all concurrent uploads succeeded")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run(main)
|
||||
Reference in New Issue
Block a user