Добавьте воспроизводимые эксперименты по памяти и согласованности
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
"""Offline checks for reproducible PNGs and Docker memory evidence."""
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
import struct
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
import io
|
||||
import zlib
|
||||
|
||||
|
||||
class UploadMemoryTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
spec = importlib.util.spec_from_file_location('upload_memory', Path(__file__).with_name('upload-memory.py'))
|
||||
cls.module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(cls.module)
|
||||
|
||||
def inspect_png(self, payload, width, height):
|
||||
self.assertEqual(payload[:8], b'\x89PNG\r\n\x1a\n')
|
||||
pos, chunks, compressed = 8, [], bytearray()
|
||||
while pos < len(payload):
|
||||
size = struct.unpack_from('>I', payload, pos)[0]
|
||||
kind = payload[pos + 4:pos + 8]
|
||||
data = payload[pos + 8:pos + 8 + size]
|
||||
crc = struct.unpack_from('>I', payload, pos + 8 + size)[0]
|
||||
self.assertEqual(crc, zlib.crc32(kind + data) & 0xffffffff)
|
||||
if kind == b'IHDR':
|
||||
self.assertEqual(struct.unpack('>IIBBBBB', data), (width, height, 8, 2, 0, 0, 0))
|
||||
if kind == b'IDAT':
|
||||
compressed.extend(data)
|
||||
chunks.append(kind)
|
||||
pos += size + 12
|
||||
self.assertEqual(pos, len(payload))
|
||||
self.assertEqual(chunks[0], b'IHDR')
|
||||
self.assertEqual(chunks[-1], b'IEND')
|
||||
self.assertIn(b'npAD', chunks)
|
||||
rows = zlib.decompress(compressed)
|
||||
self.assertEqual(len(rows), height * (width * 3 + 1))
|
||||
self.assertTrue(all(rows[row * (width * 3 + 1)] == 0 for row in range(height)))
|
||||
|
||||
def test_seeded_png_has_correct_crc_pixels_and_exact_size(self):
|
||||
first = self.module.make_png(32, 24, 4096, 123)
|
||||
self.assertEqual(len(first), 4096)
|
||||
self.assertEqual(first, self.module.make_png(32, 24, 4096, 123))
|
||||
self.assertNotEqual(first, self.module.make_png(32, 24, 4096, 124))
|
||||
self.inspect_png(first, 32, 24)
|
||||
|
||||
def test_both_experiment_payloads_decode_and_reach_exact_limits(self):
|
||||
for name, width, height, size, seed in self.module.CASES:
|
||||
with self.subTest(name=name):
|
||||
payload = self.module.make_png(width, height, size, seed)
|
||||
self.assertEqual(len(payload), size)
|
||||
self.assertLessEqual(len(payload), 10 * 1024 * 1024)
|
||||
self.assertLessEqual(width * height, 40_000_000)
|
||||
self.inspect_png(payload, width, height)
|
||||
|
||||
def test_impossible_png_size_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.module.make_png(32, 32, 64, 1)
|
||||
|
||||
def test_docker_memory_uses_binary_and_decimal_units(self):
|
||||
for raw, expected in [('1.5MiB / 512MiB', 1572864), ('2MB / 512MB', 2000000), ('4096B / 1GiB', 4096)]:
|
||||
self.assertEqual(self.module.memory_bytes(raw), expected)
|
||||
with self.assertRaises(ValueError):
|
||||
self.module.memory_bytes('not available')
|
||||
|
||||
def test_memory_summary_separates_baseline_and_load_for_each_replica(self):
|
||||
samples = [
|
||||
{'container_id': 'a', 'phase': 'baseline', 'used_bytes': 10},
|
||||
{'container_id': 'b', 'phase': 'baseline', 'used_bytes': 20},
|
||||
{'container_id': 'a', 'phase': 'baseline', 'used_bytes': 12},
|
||||
{'container_id': 'a', 'phase': 'load', 'used_bytes': 35},
|
||||
{'container_id': 'a', 'phase': 'load', 'used_bytes': 30},
|
||||
{'container_id': 'b', 'phase': 'load', 'used_bytes': 50},
|
||||
]
|
||||
result = self.module.summarize_memory(samples, ['a', 'b'])
|
||||
self.assertEqual(result['a']['baseline_bytes'], 12)
|
||||
self.assertEqual(result['a']['sampled_peak_bytes'], 35)
|
||||
self.assertEqual(result['a']['delta_bytes'], 23)
|
||||
self.assertEqual(result['b']['sampled_peak_bytes'], 50)
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.module.summarize_memory(samples, ['a', 'b', 'missing'])
|
||||
|
||||
def test_latency_percentiles_and_overlap_are_explicit(self):
|
||||
self.assertEqual(self.module.latency_summary(list(range(1, 11))),
|
||||
{'p50_ms': 5.5, 'p95_ms': 10, 'max_ms': 10})
|
||||
self.assertEqual(self.module.max_inflight([(0, 4), (1, 3), (2, 5)]), 3)
|
||||
self.assertEqual(self.module.max_inflight([(0, 1), (1, 2)]), 1)
|
||||
|
||||
def test_docker_stream_ignores_ansi_only_lines_between_samples(self):
|
||||
cid = 'a' * 64
|
||||
row = json.dumps({'ID': cid, 'Name': 'lab4-api-1', 'MemUsage': '12MiB / 512MiB'})
|
||||
stats = self.module.DockerStats([cid])
|
||||
stats.process = SimpleNamespace(stdout=iter(['\x1b[H' + row + '\n', ' \x1b[K\n', row + '\n']))
|
||||
stats.mark('small', 'baseline')
|
||||
stats._read()
|
||||
self.assertIsNone(stats.error)
|
||||
self.assertEqual(len(stats.samples), 2)
|
||||
|
||||
def test_rgba64_profile_has_valid_header_crc_and_zero_scanlines(self):
|
||||
payload = self.module.make_rgba64_png(8, 5)
|
||||
pos, compressed = 8, bytearray()
|
||||
while pos < len(payload):
|
||||
size = struct.unpack_from('>I', payload, pos)[0]
|
||||
kind, data = payload[pos + 4:pos + 8], payload[pos + 8:pos + 8 + size]
|
||||
self.assertEqual(struct.unpack_from('>I', payload, pos + 8 + size)[0], zlib.crc32(kind + data) & 0xffffffff)
|
||||
if kind == b'IHDR':
|
||||
self.assertEqual(struct.unpack('>IIBBBBB', data), (8, 5, 16, 6, 0, 0, 0))
|
||||
if kind == b'IDAT':
|
||||
compressed.extend(data)
|
||||
pos += size + 12
|
||||
self.assertEqual(zlib.decompress(compressed), bytes(5 * (1 + 8 * 8)))
|
||||
self.assertEqual(pos, len(payload))
|
||||
|
||||
def test_max_pixels_profile_fits_byte_limit_without_full_raw_buffer(self):
|
||||
payload = self.module.make_rgba64_png(8000, 5000)
|
||||
self.assertLess(len(payload), 10 * 1024 * 1024)
|
||||
self.assertEqual(struct.unpack_from('>IIBB', payload, 16), (8000, 5000, 16, 6))
|
||||
|
||||
def test_upload_uses_requested_jpeg_mime(self):
|
||||
captured = []
|
||||
class Opener:
|
||||
def open(self, request, timeout):
|
||||
captured.append(request)
|
||||
response = io.BytesIO(b'')
|
||||
response.code, response.headers = 415, {}
|
||||
return response
|
||||
client = SimpleNamespace(base='http://localhost', opener=Opener(), timeout=1)
|
||||
import threading
|
||||
result = self.module.upload(None, client, {'token': 'synthetic'}, {'id': 'chat'}, b'jpeg', 'hash',
|
||||
threading.Barrier(1), 0, 'decoder', mime='image/jpeg')
|
||||
self.assertEqual(captured[0].get_header('Content-type'), 'image/jpeg')
|
||||
self.assertTrue(captured[0].full_url.endswith('decoder-0.jpg'))
|
||||
self.assertEqual(result['status'], 415)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user