1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| <?php
namespace App\Services\Performance;
use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log;
class PerformanceTracker { protected ?Transaction $currentTransaction = null; protected array $spans = []; public function startTransaction(Request $request): void { $this->currentTransaction = new Transaction( id: $request->header('X-Request-ID', uniqid()), name: $request->route()?->getName() ?? $request->path(), type: 'request', start: microtime(true), context: [ 'method' => $request->method(), 'url' => $request->fullUrl(), 'route' => $request->route()?->getName(), 'ip' => $request->ip(), 'user_agent' => $request->userAgent(), ] ); DB::listen(function ($query) { $this->addSpan(new Span( name: 'db.query', start: microtime(true), duration: $query->time, context: [ 'sql' => $query->sql, 'bindings' => $query->bindings, ] )); }); } public function endTransaction(Request $request, Response $response): void { if (!$this->currentTransaction) { return; } $this->currentTransaction->end = microtime(true); $this->currentTransaction->result = $response->status(); $this->currentTransaction->spans = $this->spans; $this->recordTransaction($this->currentTransaction); $this->spans = []; $this->currentTransaction = null; } public function addSpan(Span $span): void { $this->spans[] = $span; } public function getDuration(): float { return $this->currentTransaction ? ($this->currentTransaction->end ?? microtime(true)) - $this->currentTransaction->start : 0; } public function getMemoryUsage(): int { return memory_get_usage(true); } public function getQueryCount(): int { return count(array_filter($this->spans, fn($s) => $s->name === 'db.query')); } protected function recordTransaction(Transaction $transaction): void { $duration = ($transaction->end - $transaction->start) * 1000; if ($duration > config('performance.slow_request_threshold', 1000)) { Log::channel('performance')->warning('Slow request detected', [ 'transaction' => $transaction->toArray(), 'duration_ms' => $duration, ]); } $this->storeMetrics($transaction); } protected function storeMetrics(Transaction $transaction): void { $metrics = [ 'response_time' => ($transaction->end - $transaction->start) * 1000, 'memory_peak' => memory_get_peak_usage(true), 'query_count' => $this->getQueryCount(), 'request_id' => $transaction->id, ]; cache()->put( 'performance:' . $transaction->id, $metrics, now()->addHours(1) ); } }
class Transaction { public function __construct( public string $id, public string $name, public string $type, public float $start, public ?float $end = null, public mixed $result = null, public array $context = [], public array $spans = [] ) {} public function toArray(): array { return [ 'id' => $this->id, 'name' => $this->name, 'type' => $this->type, 'start' => $this->start, 'end' => $this->end, 'duration' => $this->end ? ($this->end - $this->start) * 1000 : null, 'result' => $this->result, 'context' => $this->context, 'spans' => array_map(fn($s) => $s->toArray(), $this->spans), ]; } }
class Span { public function __construct( public string $name, public float $start, public ?float $duration = null, public array $context = [] ) {} public function toArray(): array { return [ 'name' => $this->name, 'start' => $this->start, 'duration' => $this->duration, 'context' => $this->context, ]; } }
|