forked from mtusi2026/mtusi2026lab4
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
"""Barrier-started >=10 upload clients and sampled RSS of both API processes."""
|
|
import concurrent.futures as cf
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import statistics
|
|
import sys
|
|
import threading
|
|
import time
|
|
import urllib.request
|
|
from PIL import Image
|
|
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
|
|
from client import Client, check
|
|
|
|
|
|
def rss():
|
|
return [json.load(urllib.request.urlopen('http://'+host+':8080/internal-test/memory',timeout=2))
|
|
for host in ('api1','api2')]
|
|
|
|
|
|
def main():
|
|
c = Client('http://lb:8080',timeout=40)
|
|
a,b,e,chat = c.bootstrap()
|
|
summaries = []
|
|
for edge in (512,1536):
|
|
# Uncompressible RGB is a genuine PNG; ~0.75 and ~6.76 MiB.
|
|
import random
|
|
image = Image.frombytes('RGB',(edge,edge),random.Random(42).randbytes(edge*edge*3))
|
|
output = io.BytesIO()
|
|
image.save(output,format='PNG')
|
|
content = output.getvalue()
|
|
del image,output
|
|
path = '/api/v1/chats/'+chat['id']+'/attachments?filename=load.png'
|
|
c.call('POST',path,201,actor=a,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')
|
|
baseline = rss()
|
|
barrier = threading.Barrier(12)
|
|
latencies = []
|
|
errors = []
|
|
samples = []
|
|
done = threading.Event()
|
|
def observe():
|
|
while not done.is_set():
|
|
samples.append(rss())
|
|
done.wait(.025)
|
|
observer = threading.Thread(target=observe)
|
|
observer.start()
|
|
def upload(_):
|
|
barrier.wait()
|
|
for __ in range(3):
|
|
started = time.monotonic()
|
|
try:
|
|
result = c.call('POST',path,201,actor=a,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')
|
|
latencies.append(time.monotonic()-started)
|
|
except Exception as exc:
|
|
errors.append(type(exc).__name__)
|
|
started = time.monotonic()
|
|
try:
|
|
with cf.ThreadPoolExecutor(max_workers=12) as pool:
|
|
list(pool.map(upload,range(12)))
|
|
finally:
|
|
done.set()
|
|
observer.join()
|
|
elapsed = time.monotonic()-started
|
|
check(not errors and len(latencies)==36,'concurrent uploads failed')
|
|
ordered = sorted(latencies)
|
|
peaks = [max(sample[i]['rss_bytes'] for sample in samples) for i in (0,1)]
|
|
cpu_end = rss()
|
|
cpu_delta = sum(cpu_end[i]['cpu_seconds']-baseline[i]['cpu_seconds'] for i in (0,1))
|
|
summaries.append({'size_bytes':len(content),'dimensions':[edge,edge],
|
|
'concurrency':12,'uploads':36,'successful_rps':round(36/elapsed,3),
|
|
'duration_seconds':round(elapsed,3),'errors':0,
|
|
'p50_seconds':round(statistics.median(ordered),4),
|
|
'p95_seconds':round(ordered[int((len(ordered)-1)*.95)],4),
|
|
'p99_seconds':round(ordered[int((len(ordered)-1)*.99)],4),
|
|
'baseline_rss_bytes':[x['rss_bytes'] for x in baseline],
|
|
'peak_rss_bytes':peaks,'cpu_seconds_total':round(cpu_delta,3),
|
|
'baseline_container_memory_bytes':[x['container_memory_bytes'] for x in baseline],
|
|
'sampled_peak_container_memory_bytes':[max(s[i]['container_memory_bytes'] for s in samples) for i in (0,1)],
|
|
'kernel_container_peak_before_bytes':[x['container_peak_bytes'] for x in baseline],
|
|
'kernel_container_peak_after_bytes':[x['container_peak_bytes'] for x in cpu_end],
|
|
'cpu_average_percent_total':round(100*cpu_delta/elapsed,2),
|
|
'memory_samples':len(samples)})
|
|
print(json.dumps({'status':'PASS','profiles':summaries}))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|