MCPcopy Create free account
hub / github.com/apify/crawlee-python / test_dynamic_memory

Function test_dynamic_memory

tests/unit/_autoscaling/test_snapshotter.py:391–447  ·  view source on GitHub ↗

Test dynamic memory scaling scenario where the system-wide memory can change. Create two memory snapshots. They have same memory usage, but different available memory. First snapshot is created with insufficient memory, so it is overloaded. Second snapshot is created with sufficient mem

(
    *,
    default_cpu_info: CpuInfo,
    event_manager: LocalEventManager,
    dynamic_memory: bool,
)

Source from the content-addressed store, hash-verified

389
390@pytest.mark.parametrize('dynamic_memory', [True, False])
391async def test_dynamic_memory(
392 *,
393 default_cpu_info: CpuInfo,
394 event_manager: LocalEventManager,
395 dynamic_memory: bool,
396) -> None:
397 """Test dynamic memory scaling scenario where the system-wide memory can change.
398
399 Create two memory snapshots. They have same memory usage, but different available memory.
400 First snapshot is created with insufficient memory, so it is overloaded.
401 Second snapshot is created with sufficient memory.
402
403 Based on the Snapshotter configuration, it will either take into account the increased available memory or not.
404 """
405 _initial_memory_info = get_memory_info()
406 ratio_just_below_system_wide_overload = 0.99 * SYSTEM_WIDE_MEMORY_OVERLOAD_THRESHOLD
407
408 memory_mbytes = 0 if dynamic_memory else floor(_initial_memory_info.total_size.to_mb())
409
410 service_locator.set_event_manager(event_manager)
411
412 async with Snapshotter.from_config(
413 Configuration(memory_mbytes=memory_mbytes, available_memory_ratio=ratio_just_below_system_wide_overload)
414 ) as snapshotter:
415 # Default state, memory usage exactly at the overload threshold -> overloaded, but not system-wide overloaded
416 memory_infos = [
417 # Overloaded sample
418 MemoryInfo(
419 total_size=_initial_memory_info.total_size,
420 current_size=_initial_memory_info.total_size * ratio_just_below_system_wide_overload,
421 system_wide_used_size=_initial_memory_info.total_size * ratio_just_below_system_wide_overload,
422 ),
423 # Same as first sample, with twice as memory available in the system
424 MemoryInfo(
425 total_size=_initial_memory_info.total_size * 2, # Simulate increased total memory
426 current_size=_initial_memory_info.total_size * ratio_just_below_system_wide_overload,
427 system_wide_used_size=_initial_memory_info.total_size * ratio_just_below_system_wide_overload,
428 ),
429 ]
430
431 for memory_info in memory_infos:
432 event_manager.emit(
433 event=Event.SYSTEM_INFO,
434 event_data=EventSystemInfoData(
435 cpu_info=default_cpu_info,
436 memory_info=memory_info,
437 ),
438 )
439
440 await event_manager.wait_for_all_listeners_to_complete()
441
442 memory_samples = snapshotter.get_memory_sample()
443 assert len(memory_samples) == 2
444 # First sample will be overloaded.
445 assert memory_samples[0].is_overloaded
446 # Second sample can reflect the increased available memory based on the configuration used to create Snapshotter
447 assert memory_samples[1].is_overloaded == (not dynamic_memory)

Callers

nothing calls this directly

Calls 10

get_memory_infoFunction · 0.90
ConfigurationClass · 0.90
MemoryInfoClass · 0.90
EventSystemInfoDataClass · 0.90
to_mbMethod · 0.80
set_event_managerMethod · 0.80
get_memory_sampleMethod · 0.80
from_configMethod · 0.45
emitMethod · 0.45

Tested by

no test coverage detected