Files

186 lines
6.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Compare proxy upload with presigned direct upload (lab4 track 1).
Measures client-facing LB traffic, API traffic, API anon memory and latency for
two file sizes. Run against a live stand from the repository root.
python3 scripts/lab4_track1.py --base-url https://localhost:8443
"""
from __future__ import annotations
import argparse
import csv
import os
import statistics
import sys
import threading
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT / 'tests'))
sys.path.insert(0, str(ROOT / 'student-tests'))
from client import Client # noqa: E402
from presign_flow import complete, presign, put # noqa: E402
from lab4_memory_experiment import png_of_size # noqa: E402
from lab7_http import BASE_URL, CA_FILE # noqa: E402
from tests_helpers import compose # noqa: E402
OUTPUT = ROOT / 'evidence/lab4/track1'
def containers(service: str) -> list[str]:
ids = compose('ps', '-q', service).stdout.split()
if not ids:
raise SystemExit(f'no running containers for service {service}')
return ids
def read_net(service: str) -> tuple[int, int]:
rx = tx = 0
for container in containers(service):
for direction, path in (('rx', 'rx_bytes'), ('tx', 'tx_bytes')):
value = os.popen(
f'docker exec {container} cat /sys/class/net/eth0/statistics/{path} 2>/dev/null'
).read().strip()
if value.isdigit():
if direction == 'rx':
rx += int(value)
else:
tx += int(value)
return rx, tx
def read_anon() -> int:
total = 0
for container in containers('api'):
stats = os.popen(f'docker exec {container} cat /sys/fs/cgroup/memory.stat 2>/dev/null').read()
for line in stats.splitlines():
if line.startswith('anon '):
total += int(line.split()[1])
return total
class Sampler:
def __init__(self) -> None:
self.stop = threading.Event()
self.peak = 0
self.thread = threading.Thread(target=self.run, daemon=True)
def run(self) -> None:
while not self.stop.is_set():
self.peak = max(self.peak, read_anon())
time.sleep(0.1)
def __enter__(self) -> 'Sampler':
self.thread.start()
return self
def __exit__(self, *_args: object) -> None:
self.stop.set()
self.thread.join(timeout=2)
def proxy_upload(client: Client, actor: dict, chat: dict, content: bytes) -> None:
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=track1.png'
client.call(
'POST', path, 202, actor=actor, raw=content,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)
def presign_upload(client: Client, actor: dict, chat: dict, content: bytes, ca_file=CA_FILE) -> None:
prepared = presign(client, actor, chat, content)
status = put(prepared['upload_url'], content, ca_file=ca_file)
if status != 200:
raise SystemExit(f'presigned PUT failed with {status}')
complete(client, actor, prepared['attachment_id'], prepared['sha256'])
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default=BASE_URL)
parser.add_argument('--ca-file', default=CA_FILE)
parser.add_argument('--iterations', type=int, default=5)
args = parser.parse_args()
client = Client(args.base_url, args.ca_file or CA_FILE, timeout=180)
alice, _, _, chat = client.bootstrap()
files = {
'~1MiB': png_of_size(700, 500, 11),
'~9MiB': png_of_size(2100, 1500, 12),
}
OUTPUT.mkdir(parents=True, exist_ok=True)
rows: list[dict[str, object]] = []
for label, content in files.items():
for flow, operation in (('proxy', proxy_upload), ('presign', presign_upload)):
for iteration in range(args.iterations):
lb_before, lb_tx_before = read_net('lb')
api_before, api_tx_before = read_net('api')
started = time.monotonic()
with Sampler() as sampler:
if flow == 'presign':
operation(client, alice, chat, content, ca_file=args.ca_file or CA_FILE)
else:
operation(client, alice, chat, content)
latency = time.monotonic() - started
lb_after, lb_tx_after = read_net('lb')
api_after, api_tx_after = read_net('api')
rows.append({
'size': label,
'bytes': len(content),
'flow': flow,
'iteration': iteration,
'latency_s': round(latency, 4),
'lb_rx_bytes': lb_after - lb_before,
'lb_tx_bytes': lb_tx_after - lb_tx_before,
'api_rx_bytes': api_after - api_before,
'api_tx_bytes': api_tx_after - api_tx_before,
'api_anon_peak': sampler.peak,
})
csv_path = OUTPUT / 'results.csv'
with csv_path.open('w', newline='') as handle:
writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
writer.writeheader()
writer.writerows(rows)
lines = ['Lab4 track 1: proxy vs presigned upload', '']
lines.append('| size | flow | median latency, s | p95 latency, s | LB rx, bytes | API rx, bytes | API anon peak, bytes |')
lines.append('|---|---|---:|---:|---:|---:|---:|')
for label in files:
for flow in ('proxy', 'presign'):
group = [row for row in rows if row['size'] == label and row['flow'] == flow]
latencies = sorted(float(row['latency_s']) for row in group)
median = statistics.median(latencies)
p95 = latencies[min(len(latencies) - 1, int(round(0.95 * (len(latencies) - 1))))]
lb_rx = sum(int(row['lb_rx_bytes']) for row in group) / len(group)
api_rx = sum(int(row['api_rx_bytes']) for row in group) / len(group)
peak = max(int(row['api_anon_peak']) for row in group)
lines.append(
f'| {label} | {flow} | {median:.3f} | {p95:.3f} | {lb_rx:.0f} | {api_rx:.0f} | {peak} |'
)
lines += [
'',
'Note: presigned upload keeps the file body off the API request path and off the load balancer.',
'Completion still streams the object from S3 once to verify its SHA-256, so api_rx stays close to',
'the file size; the saving is in the client-facing LB leg and in not accepting/buffering the body.',
'',
]
summary = OUTPUT / 'summary.txt'
summary.write_text('\n'.join(lines))
print(summary.read_text())
print('CSV:', csv_path)
if __name__ == '__main__':
main()