forked from mtusi2026/mtusi2026lab4
ЛР 4: S3-вложения и исследование согласованности — Майорова БВТ2301
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
""">=10 simultaneous uploads with sampled cgroup memory and true cgroup peak."""
|
||||
import concurrent.futures
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client
|
||||
from images import png
|
||||
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.check_output(['docker', *args], text=True).strip()
|
||||
|
||||
|
||||
def memory(container):
|
||||
code = "import json; from pathlib import Path; p=Path('/sys/fs/cgroup'); print(json.dumps({k:(p/k).read_text().strip() for k in ['memory.current','memory.peak','memory.events','cpu.stat']}))"
|
||||
return json.loads(docker('exec', container, 'python', '-c', code))
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, bob, eve, chat = c.bootstrap()
|
||||
records = []
|
||||
for width, height, noise, rgb in [(420, 420, True, True), (1800, 1400, True, True), (10000, 4000, False, True)]:
|
||||
content = png(width, height, noise=noise, rgb=rgb)
|
||||
assert len(content) <= 10 * 1024 * 1024
|
||||
digest = hashlib.sha256(content).hexdigest()
|
||||
# Fresh API processes make memory.peak attributable to this one experiment.
|
||||
docker('compose', 'restart', 'api')
|
||||
docker('compose', 'up', '-d', '--wait', '--no-deps', 'api')
|
||||
time.sleep(2)
|
||||
containers = docker('compose', 'ps', '-q', 'api').splitlines()
|
||||
before = {v: memory(v) for v in containers}
|
||||
samples = []
|
||||
done = threading.Event()
|
||||
started = time.monotonic()
|
||||
def sample():
|
||||
while not done.is_set():
|
||||
samples.append({'seconds': round(time.monotonic() - started, 4), 'memory': {v: memory(v) for v in containers}})
|
||||
done.wait(.05)
|
||||
thread = threading.Thread(target=sample)
|
||||
thread.start()
|
||||
barrier = threading.Barrier(10)
|
||||
def upload(i):
|
||||
barrier.wait()
|
||||
begin = time.monotonic()
|
||||
response = Client(timeout=30).call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=concurrent.png', [201, 503], actor=alice, raw=content, headers={'Content-Type': 'image/png'})
|
||||
if response['status'] == 201:
|
||||
# Parse because schema omitted to allow a documented admission rejection.
|
||||
attachment = json.loads(response['bytes'])
|
||||
assert attachment['sha256'] == digest
|
||||
downloaded = Client(timeout=30).call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)
|
||||
assert hashlib.sha256(downloaded['bytes']).hexdigest() == digest
|
||||
return {'index': i, 'status': response['status'], 'instance': response['headers']['X-Instance-Id'], 'elapsed_seconds': round(time.monotonic() - begin, 6)}
|
||||
try:
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
results = list(executor.map(upload, range(10)))
|
||||
finally:
|
||||
done.set()
|
||||
thread.join()
|
||||
after = {v: memory(v) for v in containers}
|
||||
for m in after.values():
|
||||
assert 'oom_kill 0' in m['memory.events']
|
||||
records.append({'width': width, 'height': height, 'bytes': len(content), 'pixels': width * height, 'clients': 10, 'before': before, 'after': after, 'samples': samples, 'results': results, 'seconds': round(time.monotonic() - started, 6)})
|
||||
print(json.dumps({'memory_source': 'Linux cgroup v2: memory.current, memory.peak (lifetime peak; API restarted before each case), memory.events; samples include tmpfs and kernel charges, not Python heap alone', 'experiments': records}, indent=2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user