- Add comprehensive health check system with multiple endpoints - Add Prometheus metrics endpoint - Add production logging configurations (5 strategies) - Add complete deployment documentation suite: * QUICKSTART.md - 30-minute deployment guide * DEPLOYMENT_CHECKLIST.md - Printable verification checklist * DEPLOYMENT_WORKFLOW.md - Complete deployment lifecycle * PRODUCTION_DEPLOYMENT.md - Comprehensive technical reference * production-logging.md - Logging configuration guide * ANSIBLE_DEPLOYMENT.md - Infrastructure as Code automation * README.md - Navigation hub * DEPLOYMENT_SUMMARY.md - Executive summary - Add deployment scripts and automation - Add DEPLOYMENT_PLAN.md - Concrete plan for immediate deployment - Update README with production-ready features All production infrastructure is now complete and ready for deployment.
200 lines
6.7 KiB
PHP
200 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Framework\Cache\Cache;
|
|
use App\Framework\Cache\CacheKey;
|
|
use App\Framework\Cache\FileCache;
|
|
use App\Framework\Cache\Warming\CacheWarmingService;
|
|
use App\Framework\Cache\Warming\Strategies\CriticalPathWarmingStrategy;
|
|
use App\Framework\Cache\Warming\ScheduledWarmupJob;
|
|
use App\Framework\Core\CompiledRoutes;
|
|
use App\Framework\Config\Environment;
|
|
use App\Framework\Logging\Logger;
|
|
use App\Framework\Core\ValueObjects\Duration;
|
|
|
|
describe('Cache Warming Integration', function () {
|
|
beforeEach(function () {
|
|
// Use real FileCache for integration test
|
|
$this->cacheDir = sys_get_temp_dir() . '/cache_warming_test_' . uniqid();
|
|
mkdir($this->cacheDir, 0777, true);
|
|
|
|
$this->cache = new FileCache($this->cacheDir);
|
|
|
|
$this->logger = Mockery::mock(Logger::class);
|
|
$this->logger->shouldReceive('info')->andReturnNull();
|
|
$this->logger->shouldReceive('debug')->andReturnNull();
|
|
$this->logger->shouldReceive('error')->andReturnNull();
|
|
|
|
$this->compiledRoutes = Mockery::mock(CompiledRoutes::class);
|
|
$this->compiledRoutes->shouldReceive('getStaticRoutes')->andReturn([
|
|
'/home' => 'HomeController',
|
|
'/about' => 'AboutController',
|
|
]);
|
|
$this->compiledRoutes->shouldReceive('getDynamicRoutes')->andReturn([
|
|
'/users/{id}' => 'UserController',
|
|
]);
|
|
|
|
$this->environment = Mockery::mock(Environment::class);
|
|
});
|
|
|
|
afterEach(function () {
|
|
// Cleanup test cache directory
|
|
if (is_dir($this->cacheDir)) {
|
|
array_map('unlink', glob($this->cacheDir . '/*'));
|
|
rmdir($this->cacheDir);
|
|
}
|
|
|
|
Mockery::close();
|
|
});
|
|
|
|
it('warms cache end-to-end', function () {
|
|
$strategy = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy],
|
|
logger: $this->logger
|
|
);
|
|
|
|
// Execute warmup
|
|
$metrics = $service->warmAll();
|
|
|
|
expect($metrics->totalStrategiesExecuted)->toBe(1);
|
|
expect($metrics->totalItemsWarmed)->toBeGreaterThan(0);
|
|
expect($metrics->isSuccess())->toBeTrue();
|
|
|
|
// Verify cache was populated
|
|
$routesKey = CacheKey::fromString('routes_static');
|
|
$cachedRoutes = $this->cache->get($routesKey);
|
|
|
|
expect($cachedRoutes)->not->toBeNull();
|
|
expect($cachedRoutes->value)->toBeArray();
|
|
});
|
|
|
|
it('integrates with ScheduledWarmupJob', function () {
|
|
$strategy = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy],
|
|
logger: $this->logger
|
|
);
|
|
|
|
$scheduledJob = new ScheduledWarmupJob(
|
|
warmingService: $service,
|
|
logger: $this->logger
|
|
);
|
|
|
|
// Execute scheduled warmup
|
|
$result = $scheduledJob->warmCriticalPaths();
|
|
|
|
expect($result)->toBeArray();
|
|
expect($result['total_strategies_executed'])->toBe(1);
|
|
expect($result['total_items_warmed'])->toBeGreaterThan(0);
|
|
});
|
|
|
|
it('handles priority-based warmup', function () {
|
|
$strategy = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy],
|
|
logger: $this->logger
|
|
);
|
|
|
|
// Warm only high priority
|
|
$metrics = $service->warmByPriority(500); // HIGH priority threshold
|
|
|
|
expect($metrics->totalStrategiesExecuted)->toBe(1); // Critical > High
|
|
expect($metrics->totalItemsWarmed)->toBeGreaterThan(0);
|
|
});
|
|
|
|
it('supports forced warmup', function () {
|
|
$strategy = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy],
|
|
logger: $this->logger
|
|
);
|
|
|
|
// First warmup
|
|
$service->warmAll();
|
|
|
|
// Second warmup (forced) should re-warm even if cache exists
|
|
$metrics = $service->warmAll(force: true);
|
|
|
|
expect($metrics->totalStrategiesExecuted)->toBe(1);
|
|
expect($metrics->totalItemsWarmed)->toBeGreaterThan(0);
|
|
});
|
|
|
|
it('provides accurate metrics', function () {
|
|
$strategy = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy],
|
|
logger: $this->logger
|
|
);
|
|
|
|
$startTime = microtime(true);
|
|
$metrics = $service->warmAll();
|
|
$actualDuration = microtime(true) - $startTime;
|
|
|
|
expect($metrics->totalDurationSeconds)->toBeGreaterThan(0);
|
|
expect($metrics->totalDurationSeconds)->toBeLessThan($actualDuration + 1); // Allow 1s margin
|
|
expect($metrics->totalMemoryUsedBytes)->toBeGreaterThan(0);
|
|
expect($metrics->getOverallSuccessRate())->toBeGreaterThan(0.5);
|
|
});
|
|
|
|
it('handles multiple strategies correctly', function () {
|
|
$strategy1 = new CriticalPathWarmingStrategy(
|
|
cache: $this->cache,
|
|
compiledRoutes: $this->compiledRoutes,
|
|
environment: $this->environment
|
|
);
|
|
|
|
// Create a second mock strategy
|
|
$strategy2 = Mockery::mock(\App\Framework\Cache\Warming\WarmupStrategy::class);
|
|
$strategy2->shouldReceive('getName')->andReturn('test_strategy');
|
|
$strategy2->shouldReceive('getPriority')->andReturn(100);
|
|
$strategy2->shouldReceive('shouldRun')->andReturn(true);
|
|
$strategy2->shouldReceive('getEstimatedDuration')->andReturn(1);
|
|
$strategy2->shouldReceive('warmup')->andReturn(
|
|
new \App\Framework\Cache\Warming\ValueObjects\WarmupResult(
|
|
strategyName: 'test_strategy',
|
|
itemsWarmed: 5,
|
|
itemsFailed: 0,
|
|
durationSeconds: 0.5,
|
|
memoryUsedBytes: 512
|
|
)
|
|
);
|
|
|
|
$service = new CacheWarmingService(
|
|
strategies: [$strategy1, $strategy2],
|
|
logger: $this->logger
|
|
);
|
|
|
|
$metrics = $service->warmAll();
|
|
|
|
expect($metrics->totalStrategiesExecuted)->toBe(2);
|
|
expect($metrics->strategyResults)->toHaveCount(2);
|
|
});
|
|
});
|