From 450ee108c1d1e1c8c4ec77e235cb3a193955ebc6 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 04:02:07 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Add=20test=20coverage=20for=20Me?= =?UTF-8?q?moryOptimizer=20Core=20functionality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: segin <480709+segin@users.noreply.github.com> --- tests/Makefile.am | 21 ++++++++- tests/test_memory_optimizer.cpp | 76 +++++++++++++++++++++++++++++---- 2 files changed, 87 insertions(+), 10 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index 2bc7150e..bb47bd55 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -307,6 +307,25 @@ debug_dual_flac_LDADD = \ $(top_builddir)/src/core/libpsymp3-core.a \ $(AM_LDFLAGS) +test_memory_optimizer_SOURCES = test_memory_optimizer.cpp +test_memory_optimizer_LDADD = \ + libtest_utilities.a \ + $(top_builddir)/src/io/file/libpsymp3-io-file.a \ + $(top_builddir)/src/io/libpsymp3-io.a \ + $(top_builddir)/src/debug.o \ + $(top_builddir)/src/core/libpsymp3-core.a \ + $(AM_LDFLAGS) + +test_memory_leak_prevention_SOURCES = test_memory_leak_prevention.cpp +test_memory_leak_prevention_LDADD = \ + libtest_utilities.a \ + $(top_builddir)/src/io/file/libpsymp3-io-file.a \ + $(top_builddir)/src/io/http/libpsymp3-io-http.a \ + $(top_builddir)/src/io/libpsymp3-io.a \ + $(top_builddir)/src/debug.o \ + $(top_builddir)/src/core/libpsymp3-core.a \ + $(AM_LDFLAGS) + debug_bit_extraction_SOURCES = debug_bit_extraction.cpp debug_bit_extraction_LDADD = $(AM_LDFLAGS) @@ -553,7 +572,7 @@ endif check_PROGRAMS += test_codec_selection_validation_simple # Performance and thread safety tests -check_PROGRAMS += test_codec_performance test_codec_thread_safety test_codec_concurrent_instances test_codec_performance_simple test_codec_thread_safety_simple test_threading_safety_baseline test_audio_thread_safety test_audio_threading_pattern test_iohandler_thread_safety_comprehensive test_iohandler_memory_deadlock_prevention test_memory_pool_manager_integration test_memory_pool_manager_thread_safety_comprehensive test_memory_pool_manager_basic_threading test_memory_pool_allocation_failure test_surface_thread_safety test_surface_performance_regression test_system_wide_threading_integration test_threading_performance_regression +check_PROGRAMS += test_codec_performance test_codec_thread_safety test_codec_concurrent_instances test_codec_performance_simple test_codec_thread_safety_simple test_threading_safety_baseline test_audio_thread_safety test_audio_threading_pattern test_iohandler_thread_safety_comprehensive test_iohandler_memory_deadlock_prevention test_memory_pool_manager_integration test_memory_pool_manager_thread_safety_comprehensive test_memory_pool_manager_basic_threading test_memory_pool_allocation_failure test_surface_thread_safety test_surface_performance_regression test_system_wide_threading_integration test_threading_performance_regression test_memory_optimizer test_memory_leak_prevention # SimplePCMCodec integration tests check_PROGRAMS += test_simplepcmcodec_integration_minimal test_simplepcmcodec_real_integration test_simplepcmcodec_comprehensive_integration diff --git a/tests/test_memory_optimizer.cpp b/tests/test_memory_optimizer.cpp index ade8d6cc..e8286189 100644 --- a/tests/test_memory_optimizer.cpp +++ b/tests/test_memory_optimizer.cpp @@ -85,11 +85,8 @@ void testBoundedQueue() { } // Get stats - auto stats = queue.getStats(); - std::cout << "Queue stats: " << stats.current_items << " items, " - << stats.current_memory_bytes << " bytes, " - << stats.total_items_pushed << " pushed, " - << stats.total_items_dropped << " dropped" << std::endl; + std::cout << "Queue size: " << queue.size() << " items, " + << queue.memoryUsage() << " bytes" << std::endl; // Pop items int value; @@ -98,9 +95,8 @@ void testBoundedQueue() { } // Get stats after pop - stats = queue.getStats(); - std::cout << "Queue stats after pop: " << stats.current_items << " items, " - << stats.current_memory_bytes << " bytes" << std::endl; + std::cout << "Queue size after pop: " << queue.size() << " items, " + << queue.memoryUsage() << " bytes" << std::endl; std::cout << "BoundedQueue test completed." << std::endl; } @@ -124,7 +120,7 @@ void testMemoryTracker() { std::cout << " Total physical memory: " << (stats.total_physical_memory / (1024 * 1024)) << " MB" << std::endl; std::cout << " Available physical memory: " << (stats.available_physical_memory / (1024 * 1024)) << " MB" << std::endl; std::cout << " Process memory usage: " << (stats.process_memory_usage / (1024 * 1024)) << " MB" << std::endl; - std::cout << " Memory pressure level: " << stats.memory_pressure_level << "%" << std::endl; + std::cout << " Memory pressure level: " << tracker.getMemoryPressureLevel() << "%" << std::endl; // Allocate some memory to change pressure std::vector> memory_blocks; @@ -145,6 +141,65 @@ void testMemoryTracker() { std::cout << "MemoryTracker test completed." << std::endl; } +// Test the MemoryOptimizer Core functionality +void testMemoryOptimizerCore() { + std::cout << "Testing MemoryOptimizer Core functionality..." << std::endl; + + MemoryOptimizer& optimizer = MemoryOptimizer::getInstance(); + IOBufferPool& pool = IOBufferPool::getInstance(); + + // Initial state + pool.clear(); + std::cout << "Initial pool size: " << pool.getStats().at("current_pool_size") << " bytes" << std::endl; + + // Set memory limits for MemoryOptimizer + optimizer.setMemoryLimits(1024 * 1024, 512 * 1024); // 1MB total, 512KB buffer + + // Register some allocations to simulate usage + optimizer.registerAllocation(500 * 1024, "test_component1"); + optimizer.registerAllocation(200 * 1024, "test_component2"); + + auto mem_stats = optimizer.getMemoryStats(); + std::cout << "MemoryOptimizer total usage: " << mem_stats["total_memory_usage"] << " bytes" << std::endl; + + // Populate the IOBufferPool + std::vector buffers; + for (int i = 0; i < 50; i++) { + buffers.push_back(pool.acquire(8192)); // 8KB buffers + } + + // Release them back to populate the pool + for (auto& buffer : buffers) { + buffer.release(); + } + buffers.clear(); + + std::cout << "Pool size before optimization: " << pool.getStats().at("current_pool_size") << " bytes" << std::endl; + + // Set a critical memory pressure level + // Memory usage is 716800 bytes, total limit is 1MB. (716KB / 1024KB = 70%) + // Let's force optimization manually or change memory limits to trigger pressure + optimizer.setMemoryLimits(700 * 1024, 512 * 1024); + + // Call optimizeMemoryUsage + optimizer.optimizeMemoryUsage(); + + std::cout << "Pool size after optimization: " << pool.getStats().at("current_pool_size") << " bytes" << std::endl; + + // Assert cache trimming + if (pool.getStats().at("current_pool_size") >= 65536) { + std::cerr << "Assertion failed: Pool size was not trimmed during optimization!" << std::endl; + std::exit(1); + } + + // Clean up + pool.clear(); + optimizer.registerDeallocation(500 * 1024, "test_component1"); + optimizer.registerDeallocation(200 * 1024, "test_component2"); + + std::cout << "MemoryOptimizer Core test completed." << std::endl; +} + // Main test function int main() { std::srand(static_cast(std::time(nullptr))); @@ -161,6 +216,9 @@ int main() { testMemoryTracker(); std::cout << std::endl; + testMemoryOptimizerCore(); + std::cout << std::endl; + std::cout << "All tests completed." << std::endl; return 0;