Files
michaelschiemer/src/Framework/Cache/Warming/ValueObjects/WarmupResult.php

79 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Cache\Warming\ValueObjects;
/**
* Result of a cache warmup operation
*/
final readonly class WarmupResult
{
public function __construct(
public string $strategyName,
public int $itemsWarmed,
public int $itemsFailed,
public float $durationSeconds,
public int $memoryUsedBytes,
public array $errors = [],
public array $metadata = []
) {
if ($itemsWarmed < 0) {
throw new \InvalidArgumentException('Items warmed cannot be negative');
}
if ($itemsFailed < 0) {
throw new \InvalidArgumentException('Items failed cannot be negative');
}
if ($durationSeconds < 0.0) {
throw new \InvalidArgumentException('Duration cannot be negative');
}
if ($memoryUsedBytes < 0) {
throw new \InvalidArgumentException('Memory used cannot be negative');
}
}
public function isSuccess(): bool
{
return $this->itemsFailed === 0;
}
public function getSuccessRate(): float
{
$total = $this->itemsWarmed + $this->itemsFailed;
if ($total === 0) {
return 0.0;
}
return $this->itemsWarmed / $total;
}
public function getItemsPerSecond(): float
{
if ($this->durationSeconds === 0.0) {
return 0.0;
}
return $this->itemsWarmed / $this->durationSeconds;
}
public function getMemoryUsedMB(): float
{
return $this->memoryUsedBytes / 1024 / 1024;
}
public function toArray(): array
{
return [
'strategy_name' => $this->strategyName,
'items_warmed' => $this->itemsWarmed,
'items_failed' => $this->itemsFailed,
'duration_seconds' => $this->durationSeconds,
'memory_used_bytes' => $this->memoryUsedBytes,
'memory_used_mb' => round($this->getMemoryUsedMB(), 2),
'success_rate' => round($this->getSuccessRate() * 100, 2),
'items_per_second' => round($this->getItemsPerSecond(), 2),
'is_success' => $this->isSuccess(),
'errors' => $this->errors,
'metadata' => $this->metadata
];
}
}