now() ); $testRegistry = new DiscoveryRegistry(); // Add some test data to registry $testRegistry->addRoute('/test/route', 'TestController::index'); $testRegistry->addRoute('/api/test', 'ApiController::test'); // Test basic store/retrieve $stored = $basicCacheManager->store($testContext, $testRegistry); echo " Standard storage: " . ($stored ? 'โœ…' : 'โŒ') . "\n"; $retrieved = $basicCacheManager->get($testContext); $retrieveSuccess = $retrieved !== null && count($retrieved) === count($testRegistry); echo " Standard retrieval: " . ($retrieveSuccess ? 'โœ…' : 'โŒ') . "\n"; $healthStatus = $basicCacheManager->getHealthStatus(); echo " Memory-aware: " . ($healthStatus['memory_aware'] ? 'enabled' : 'disabled') . "\n"; // Test 2: Enhanced Cache Manager with Memory Management echo "\n๐Ÿงช Test 2: Enhanced Cache Manager (Memory-Aware Mode)\n"; // Setup memory management $memoryLimit = Byte::fromMegabytes(16); // Small limit for testing $strategy = MemoryStrategy::ADAPTIVE; $memoryMonitor = new MemoryMonitor(); $memoryManager = new DiscoveryMemoryManager( strategy: $strategy, memoryLimit: $memoryLimit, memoryPressureThreshold: 0.5, // Low threshold for testing memoryMonitor: $memoryMonitor, logger: $logger, eventDispatcher: $eventDispatcher, clock: $clock ); $enhancedCacheManager = new DiscoveryCacheManager( cache: $cache, clock: $clock, fileSystemService: $fileSystemService, logger: $logger, ttlHours: 2, memoryManager: $memoryManager, eventDispatcher: $eventDispatcher, compressionThreshold: 0.3, // Low threshold for testing evictionThreshold: 0.6 ); echo "โœ… Enhanced cache manager created with memory management\n"; // Test memory-aware storage $enhancedStored = $enhancedCacheManager->store($testContext, $testRegistry); echo " Memory-aware storage: " . ($enhancedStored ? 'โœ…' : 'โŒ') . "\n"; $enhancedRetrieved = $enhancedCacheManager->get($testContext); $enhancedRetrieveSuccess = $enhancedRetrieved !== null && count($enhancedRetrieved) === count($testRegistry); echo " Memory-aware retrieval: " . ($enhancedRetrieveSuccess ? 'โœ…' : 'โŒ') . "\n"; // Test health status with memory info $enhancedHealthStatus = $enhancedCacheManager->getHealthStatus(); echo " Memory-aware: " . ($enhancedHealthStatus['memory_aware'] ? 'enabled' : 'disabled') . "\n"; if (isset($enhancedHealthStatus['memory_management'])) { echo " Memory status: " . $enhancedHealthStatus['memory_management']['status'] . "\n"; echo " Memory pressure: " . $enhancedHealthStatus['memory_management']['memory_pressure'] . "\n"; echo " Cache level: " . $enhancedHealthStatus['memory_management']['cache_level'] . "\n"; } // Test 3: Cache Metrics echo "\n๐Ÿงช Test 3: Cache Metrics & Analytics\n"; $cacheMetrics = $enhancedCacheManager->getCacheMetrics(); if ($cacheMetrics !== null) { echo "โœ… Cache metrics available\n"; echo " Total items: " . $cacheMetrics->totalItems . "\n"; echo " Hit rate: " . $cacheMetrics->hitRate->toPercentage()->toString() . "\n"; echo " Total size: " . $cacheMetrics->totalSize->toHumanReadable() . "\n"; echo " Compression ratio: " . $cacheMetrics->compressionRatio->toPercentage()->toString() . "\n"; echo " Health status: " . $cacheMetrics->getHealthStatus() . "\n"; echo " Quality score: " . $cacheMetrics->getQualityScore()->toString() . "\n"; echo " Memory pressure impact: " . $cacheMetrics->getMemoryPressureImpact() . "\n"; $recommendations = $cacheMetrics->getRecommendedActions(); if (! empty($recommendations)) { echo " Recommendations:\n"; foreach ($recommendations as $recommendation) { echo " - " . $recommendation . "\n"; } } } else { echo " Cache metrics: not available (expected for basic manager)\n"; } // Test 4: Memory Pressure Management echo "\n๐Ÿงช Test 4: Memory Pressure Management\n"; $managementResult = $enhancedCacheManager->performMemoryPressureManagement(); echo "โœ… Memory pressure management executed\n"; echo " Actions performed: " . count($managementResult['actions']) . "\n"; if (! empty($managementResult['actions'])) { foreach ($managementResult['actions'] as $action) { echo " - " . $action . "\n"; } } echo " Cache level: " . $managementResult['cache_level'] . "\n"; // Test 5: Large Data Processing (to trigger compression) echo "\n๐Ÿงช Test 5: Large Data Processing & Compression\n"; // Create larger registry to trigger compression $largeRegistry = new DiscoveryRegistry(); for ($i = 0; $i < 100; $i++) { $largeRegistry->addRoute("/large/route/{$i}", "LargeController::method{$i}"); $largeRegistry->addMiddleware("Middleware{$i}", "middleware.{$i}"); } $largeContext = new DiscoveryContext( paths: ['/large/test/path'], scanType: ScanType::FULL, options: new DiscoveryOptions(), startTime: $clock->now() ); $largeStored = $enhancedCacheManager->store($largeContext, $largeRegistry); echo " Large data storage: " . ($largeStored ? 'โœ…' : 'โŒ') . "\n"; $largeRetrieved = $enhancedCacheManager->get($largeContext); $largeRetrieveSuccess = $largeRetrieved !== null && count($largeRetrieved) === count($largeRegistry); echo " Large data retrieval: " . ($largeRetrieveSuccess ? 'โœ…' : 'โŒ') . "\n"; // Check if compression was applied $updatedMetrics = $enhancedCacheManager->getCacheMetrics(); if ($updatedMetrics !== null) { echo " Updated total size: " . $updatedMetrics->totalSize->toHumanReadable() . "\n"; echo " Compression effectiveness: " . $updatedMetrics->getCompressionEffectiveness() . "\n"; } // Test 6: Cache Invalidation echo "\n๐Ÿงช Test 6: Cache Invalidation\n"; $invalidated = $enhancedCacheManager->invalidate($testContext); echo " Context invalidation: " . ($invalidated ? 'โœ…' : 'โŒ') . "\n"; $afterInvalidation = $enhancedCacheManager->get($testContext); $invalidationWorked = $afterInvalidation === null; echo " Invalidation verification: " . ($invalidationWorked ? 'โœ…' : 'โŒ') . "\n"; // Test 7: Integration Testing with Different Memory Pressures echo "\n๐Ÿงช Test 7: Memory Pressure Integration Testing\n"; // Simulate different memory pressures $pressureTests = [ ['name' => 'Low Pressure', 'limit' => Byte::fromMegabytes(100)], ['name' => 'Medium Pressure', 'limit' => Byte::fromMegabytes(32)], ['name' => 'High Pressure', 'limit' => Byte::fromMegabytes(8)], ]; foreach ($pressureTests as $test) { $pressureMemoryManager = new DiscoveryMemoryManager( strategy: MemoryStrategy::CONSERVATIVE, memoryLimit: $test['limit'], memoryPressureThreshold: 0.4, memoryMonitor: $memoryMonitor, logger: $logger, eventDispatcher: $eventDispatcher, clock: $clock ); $pressureCacheManager = new DiscoveryCacheManager( cache: $cache, clock: $clock, fileSystemService: $fileSystemService, logger: $logger, ttlHours: 1, memoryManager: $pressureMemoryManager, eventDispatcher: $eventDispatcher ); $pressureContext = new DiscoveryContext( paths: ["/pressure/{$test['name']}"], scanType: ScanType::FULL, options: new DiscoveryOptions(), startTime: $clock->now() ); $pressureStored = $pressureCacheManager->store($pressureContext, $largeRegistry); $pressureHealthStatus = $pressureCacheManager->getHealthStatus(); echo " {$test['name']} ({$test['limit']->toHumanReadable()}):\n"; echo " Storage: " . ($pressureStored ? 'โœ…' : 'โŒ') . "\n"; if (isset($pressureHealthStatus['memory_management'])) { echo " Memory status: " . $pressureHealthStatus['memory_management']['status'] . "\n"; echo " Cache level: " . $pressureHealthStatus['memory_management']['cache_level'] . "\n"; } } echo "\n๐ŸŽ‰ All integrated cache manager tests completed successfully!\n"; echo "\n๐Ÿ† Key Integration Features Verified:\n"; echo " โœ… Backward compatibility with standard caching\n"; echo " โœ… Optional memory-aware enhancement\n"; echo " โœ… Automatic tiered caching with compression\n"; echo " โœ… Memory pressure-based cache management\n"; echo " โœ… Comprehensive cache metrics with Score objects\n"; echo " โœ… Event-driven cache monitoring\n"; echo " โœ… Intelligent cache level adjustment\n"; echo " โœ… Large data handling with compression\n"; echo " โœ… Cache invalidation and cleanup\n"; echo " โœ… Multi-pressure scenario handling\n"; echo "\n๐Ÿ”„ No Functionality Duplication - Single Enhanced Manager!\n"; } catch (Throwable $e) { echo "\nโŒ Error during integrated cache testing:\n"; echo " Message: " . $e->getMessage() . "\n"; echo " File: " . $e->getFile() . ":" . $e->getLine() . "\n"; echo " Stack trace:\n" . $e->getTraceAsString() . "\n"; exit(1); }