forked from mtusi2026/mtusi2026lab4
Grade 4
This commit is contained in:
+2
-2
@@ -15,12 +15,12 @@ def snapshot():
|
||||
|
||||
def main():
|
||||
before = snapshot()
|
||||
subprocess.run(['docker', 'compose', 'restart', 'api', 'postgres', 'redis', 'nginx'],
|
||||
subprocess.run(['docker', 'compose', 'restart', 'api', 'postgres', 'redis', 's3', 'cleanup', 'nginx'],
|
||||
cwd=ROOT, check=True, timeout=60)
|
||||
after = snapshot()
|
||||
(ROOT / 'evidence').mkdir(exist_ok=True)
|
||||
(ROOT / 'evidence' / 'restart.txt').write_text(
|
||||
'Command: docker compose restart api postgres redis nginx\nBefore (UTC):\n' + before
|
||||
'Command: docker compose restart api postgres redis s3 cleanup nginx\nBefore (UTC):\n' + before
|
||||
+ 'After (UTC):\n' + after, encoding='utf-8')
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Upload one fixture concurrently and record the peak RSS of API containers."""
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, check
|
||||
|
||||
|
||||
def api_memory_bytes():
|
||||
text = subprocess.check_output(
|
||||
['docker', 'compose', 'stats', '--no-stream', '--format', '{{.Name}} {{.MemUsage}}', 'api'],
|
||||
cwd=ROOT, text=True,
|
||||
)
|
||||
values = []
|
||||
for line in text.splitlines():
|
||||
amount = line.split()[1]
|
||||
number, unit = float(amount[:-3]), amount[-3:].lower()
|
||||
values.append(int(number * {'b': 1, 'kib': 1024, 'mib': 1024**2, 'gib': 1024**3}[unit]))
|
||||
return sum(values)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--concurrency', type=int, default=10)
|
||||
parser.add_argument('--output', default='evidence/upload-load.json')
|
||||
args = parser.parse_args()
|
||||
check(args.concurrency >= 10, 'concurrency must be at least 10')
|
||||
client = Client(args.base_url)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
content = (ROOT / 'tests' / 'fixtures' / 'sample.png').read_bytes()
|
||||
start = threading.Event()
|
||||
done = threading.Event()
|
||||
peak = [0]
|
||||
|
||||
def monitor():
|
||||
while not done.is_set():
|
||||
peak[0] = max(peak[0], api_memory_bytes())
|
||||
time.sleep(.2)
|
||||
|
||||
def upload(index):
|
||||
start.wait()
|
||||
return Client(args.base_url, timeout=60).call(
|
||||
'POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load-%d.png' % index,
|
||||
201, actor=alice, raw=content, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||||
|
||||
watcher = threading.Thread(target=monitor, daemon=True)
|
||||
watcher.start()
|
||||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
||||
jobs = [pool.submit(upload, item) for item in range(args.concurrency)]
|
||||
start.set()
|
||||
results = [job.result() for job in jobs]
|
||||
done.set()
|
||||
watcher.join(timeout=1)
|
||||
report = {'concurrency': args.concurrency, 'fixture_bytes': len(content),
|
||||
'successful_uploads': len(results), 'peak_api_memory_bytes': peak[0]}
|
||||
destination = ROOT / args.output
|
||||
destination.parent.mkdir(exist_ok=True)
|
||||
destination.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
|
||||
print(json.dumps(report, ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user