forked from mtusi2026/mtusi2026lab4
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Простой профиль параллельных загрузок для измерения RAM контейнеров API."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import json
|
|
import time
|
|
import uuid
|
|
|
|
from client import Client, ROOT, arguments, run
|
|
|
|
|
|
def main():
|
|
def configure(parser):
|
|
parser.add_argument('--concurrency', type=int, default=10)
|
|
parser.add_argument('--uploads', type=int, default=30)
|
|
|
|
args = arguments(
|
|
'Parallel attachment uploads',
|
|
configure,
|
|
)
|
|
if args.concurrency < 10:
|
|
raise ValueError('concurrency must be at least 10 for lab4 evidence')
|
|
if args.uploads < args.concurrency:
|
|
raise ValueError('uploads must be greater than or equal to concurrency')
|
|
client = Client(args.base_url, args.ca_file, timeout=30)
|
|
alice, _bob, _eve, chat = client.bootstrap()
|
|
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
|
|
|
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=load-{index}-{uuid.uuid4().hex}.png',
|
|
201,
|
|
actor=alice,
|
|
raw=content,
|
|
headers={'Content-Type': 'image/png'},
|
|
schema='Attachment',
|
|
)
|
|
return response['headers']['X-Instance-Id'], time.monotonic() - started
|
|
|
|
started = time.monotonic()
|
|
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
|
results = list(pool.map(upload, range(args.uploads)))
|
|
duration = time.monotonic() - started
|
|
latencies = sorted(item[1] for item in results)
|
|
print(
|
|
json.dumps(
|
|
{
|
|
'concurrency': args.concurrency,
|
|
'file_size_bytes': len(content),
|
|
'successful_uploads': len(results),
|
|
'instances': len({item[0] for item in results}),
|
|
'duration_seconds': round(duration, 3),
|
|
'max_latency_seconds': round(latencies[-1], 3),
|
|
},
|
|
ensure_ascii=False,
|
|
)
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|