forked from mtusi2026/mtusi2026lab4
139 lines
6.4 KiB
Python
Executable File
139 lines
6.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
"""Память реплик при одновременных загрузках файлов на границе 10 МиБ. Только стандартная библиотека.
|
||
|
||
Файл: случайный несжатый PNG примерно на 3,5 Мп, размером почти ровно 10 МиБ. Его декодирование
|
||
дешёвое, поэтому результат показывает расход на приём тела, а не на декодирование 40 Мп (для этого
|
||
есть memory_peak.py).
|
||
|
||
Что измеряется для каждой реплики:
|
||
anon — анонимная память процесса (куча и стеки), опрос memory.stat каждые 0,2 с;
|
||
peak — memory.peak из cgroup, точный максимум, но он включает вытесняемый дисковый кэш.
|
||
Запускать на только что перезапущенных репликах, иначе peak включает более ранние нагрузки.
|
||
"""
|
||
from concurrent.futures import ThreadPoolExecutor
|
||
import hashlib
|
||
import os
|
||
import statistics
|
||
import subprocess
|
||
import threading
|
||
import time
|
||
import struct
|
||
import zlib
|
||
|
||
from common import Client, MAX_BYTES, ROOT, arguments, check, png_chunk, run
|
||
|
||
COMPOSE = ['docker', 'compose', '--env-file', '.env']
|
||
LIMIT = 1024 ** 3
|
||
MIB = 1024 ** 2
|
||
|
||
|
||
def noise_png(target):
|
||
"""Несжимаемый PNG truecolor размером не больше target байт."""
|
||
width = 2048
|
||
height = (target - 1024) // (width * 3 + 1)
|
||
raw = b''.join(b'\0' + os.urandom(width * 3) for _ in range(height))
|
||
ihdr = struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)
|
||
data = b'\x89PNG\r\n\x1a\n' + png_chunk(b'IHDR', ihdr) + png_chunk(b'IDAT', zlib.compress(raw, 0)) + png_chunk(b'IEND', b'')
|
||
check(len(data) <= target, 'test file exceeds target size')
|
||
return data
|
||
|
||
|
||
def replicas():
|
||
return subprocess.run(COMPOSE + ['ps', '-q', 'api'], cwd=ROOT, check=True, capture_output=True, text=True).stdout.split()
|
||
|
||
|
||
def container_read(container, path):
|
||
return subprocess.run(['docker', 'exec', container, 'cat', path], check=True, capture_output=True, text=True).stdout
|
||
|
||
|
||
def anon_bytes(container):
|
||
for line in container_read(container, '/sys/fs/cgroup/memory.stat').splitlines():
|
||
name, value = line.split()
|
||
if name == 'anon':
|
||
return int(value)
|
||
raise RuntimeError('anon is missing from memory.stat')
|
||
|
||
|
||
def memory_event(container, name):
|
||
"""Счётчик из memory.events: max — упор в лимит (ядро вытесняет кэш), oom_kill — убитые процессы."""
|
||
for line in container_read(container, '/sys/fs/cgroup/memory.events').splitlines():
|
||
key, value = line.split()
|
||
if key == name:
|
||
return int(value)
|
||
raise RuntimeError(name + ' is missing from memory.events')
|
||
|
||
|
||
class Sampler(threading.Thread):
|
||
"""Опрашивает анонимную память реплик и помнит максимум."""
|
||
|
||
def __init__(self, containers):
|
||
super().__init__(daemon=True)
|
||
self.containers = containers
|
||
self.peak = {c: 0 for c in containers}
|
||
self.samples = 0
|
||
self.done = threading.Event()
|
||
|
||
def run(self):
|
||
while not self.done.is_set():
|
||
for container in self.containers:
|
||
try:
|
||
self.peak[container] = max(self.peak[container], anon_bytes(container))
|
||
except (subprocess.CalledProcessError, RuntimeError):
|
||
pass
|
||
self.samples += 1
|
||
self.done.wait(0.2)
|
||
|
||
|
||
def main():
|
||
parser_args = arguments('Replica memory under concurrent uploads', None)
|
||
concurrency = int(os.environ.get('UPLOADS', '20'))
|
||
rounds = int(os.environ.get('ROUNDS', '3'))
|
||
check(concurrency >= 10, 'the experiment needs at least 10 concurrent uploads')
|
||
|
||
c = Client(parser_args.base_url, parser_args.ca_file, timeout=120)
|
||
alice, _, _, chat = c.bootstrap()
|
||
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=load.png'
|
||
data = noise_png(MAX_BYTES)
|
||
digest = hashlib.sha256(data).hexdigest()
|
||
print('файл: %d байт, одновременно: %d, раундов: %d' % (len(data), concurrency, rounds))
|
||
|
||
containers = replicas()
|
||
before = {x: anon_bytes(x) for x in containers}
|
||
sampler = Sampler(containers)
|
||
sampler.start()
|
||
|
||
def upload(_):
|
||
client = Client(parser_args.base_url, parser_args.ca_file, timeout=120)
|
||
started = time.monotonic()
|
||
response = client.call('POST', path, [201, 429], actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment')
|
||
if response['status'] == 201:
|
||
check(response['json']['sha256'] == digest, 'checksum differs after concurrent upload')
|
||
return response['status'], time.monotonic() - started
|
||
|
||
results = []
|
||
started = time.monotonic()
|
||
with ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||
for _ in range(rounds):
|
||
results += list(pool.map(upload, range(concurrency)))
|
||
elapsed = time.monotonic() - started
|
||
sampler.done.set()
|
||
sampler.join()
|
||
|
||
ok = sorted(seconds for status, seconds in results if status == 201)
|
||
print('длительность %.1f с; 201=%d, 429=%d; опросов памяти: %d' % (elapsed, len(ok), len(results) - len(ok), sampler.samples))
|
||
if ok:
|
||
p95 = ok[min(len(ok) - 1, int(len(ok) * 0.95))]
|
||
print('время успешной загрузки: p50=%.2f с, p95=%.2f с, max=%.2f с' % (statistics.median(ok), p95, ok[-1]))
|
||
for x in containers:
|
||
peak = int(container_read(x, '/sys/fs/cgroup/memory.peak'))
|
||
state = subprocess.run(['docker', 'inspect', '--format', '{{.State.OOMKilled}} {{.RestartCount}}', x], check=True, capture_output=True, text=True).stdout.split()
|
||
kills = memory_event(x, 'oom_kill')
|
||
print('реплика %s: anon до %d МиБ, пик anon %d МиБ, memory.peak %d МиБ, упоров в лимит %d, oom_kill=%d, перезапусков=%s' % (
|
||
x[:12], before[x] // MIB, sampler.peak[x] // MIB, peak // MIB, memory_event(x, 'max'), kills, state[1]))
|
||
check(sampler.peak[x] < LIMIT and kills == 0 and state == ['false', '0'], 'replica ran out of memory or was restarted')
|
||
print('PASS: concurrent uploads caused no OOM or restart')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
run(main)
|