Check memory stability over many iterations.
(results, iterations=10, chunks=1000)
| 370 | |
| 371 | |
| 372 | async def benchmark_memory_stability(results, iterations=10, chunks=1000): |
| 373 | """Check memory stability over many iterations.""" |
| 374 | worker = create_worker() |
| 375 | |
| 376 | gc.collect() |
| 377 | tracemalloc.start() |
| 378 | memory_samples = [tracemalloc.get_traced_memory()[0]] |
| 379 | |
| 380 | for i in range(iterations): |
| 381 | writer = MockStreamWriter() |
| 382 | |
| 383 | async def gen_chunks(): |
| 384 | for j in range(chunks): |
| 385 | yield f"chunk-{j}" |
| 386 | |
| 387 | async def mock_execute(app_path, action, args, kwargs): |
| 388 | return gen_chunks() |
| 389 | |
| 390 | with mock.patch.object(worker, 'execute', side_effect=mock_execute): |
| 391 | request = make_request(f"bench-mem-{i}", "benchmark:App", "stream") |
| 392 | await worker.handle_request(request, writer) |
| 393 | |
| 394 | gc.collect() |
| 395 | memory_samples.append(tracemalloc.get_traced_memory()[0]) |
| 396 | |
| 397 | tracemalloc.stop() |
| 398 | |
| 399 | memory_start = memory_samples[0] |
| 400 | memory_end = memory_samples[-1] |
| 401 | memory_max = max(memory_samples) |
| 402 | |
| 403 | print(f"\nMemory stability ({iterations} iterations of {chunks} chunks):") |
| 404 | print(f" Start: {memory_start / 1024 / 1024:.2f}MB") |
| 405 | print(f" End: {memory_end / 1024 / 1024:.2f}MB") |
| 406 | print(f" Max: {memory_max / 1024 / 1024:.2f}MB") |
| 407 | print(f" Delta: {(memory_end - memory_start) / 1024 / 1024:.2f}MB") |
| 408 | |
| 409 | results.add( |
| 410 | f"Memory stability ({iterations} x {chunks} chunks)", |
| 411 | iterations=iterations * chunks, |
| 412 | duration=0.001, # Use small non-zero value to avoid division by zero |
| 413 | memory_start=memory_start, |
| 414 | memory_end=memory_end |
| 415 | ) |
| 416 | |
| 417 | |
| 418 | class MockClientReader: |
no test coverage detected