* @author Tobias Nyholm * * @final */ class CacheDataCollector extends DataCollector implements LateDataCollectorInterface { /** @var TraceableAdapter[] */ private $instances = []; public function addInstance(string $name, TraceableAdapter $instance) { $this->instances[$name] = $instance; } /** * {@inheritdoc} */ public function collect(Request $request, Response $response, \Throwable $exception = null) { $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []]; $this->data = ['instances' => $empty, 'total' => $empty]; foreach ($this->instances as $name => $instance) { $this->data['instances']['calls'][$name] = $instance->getCalls(); } $this->data['instances']['statistics'] = $this->calculateStatistics(); $this->data['total']['statistics'] = $this->calculateTotalStatistics(); } public function reset() { $this->data = []; foreach ($this->instances as $instance) { $instance->clearCalls(); } } public function lateCollect() { $this->data['instances']['calls'] = $this->cloneVar($this->data['instances']['calls']); } /** * {@inheritdoc} */ public function getName(): string { return 'cache'; } /** * Method returns amount of logged Cache reads: "get" calls. */ public function getStatistics(): array { return $this->data['instances']['statistics']; } /** * Method returns the statistic totals. */ public function getTotals(): array { return $this->data['total']['statistics']; } /** * Method returns all logged Cache call objects. * * @return mixed */ public function getCalls() { return $this->data['instances']['calls']; } private function calculateStatistics(): array { $statistics = []; foreach ($this->data['instances']['calls'] as $name => $calls) { $statistics[$name] = [ 'calls' => 0, 'time' => 0, 'reads' => 0, 'writes' => 0, 'deletes' => 0, 'hits' => 0, 'misses' => 0, ]; /** @var TraceableAdapterEvent $call */ foreach ($calls as $call) { ++$statistics[$name]['calls']; $statistics[$name]['time'] += ($call->end ?? microtime(true)) - $call->start; if ('get' === $call->name) { ++$statistics[$name]['reads']; if ($call->hits) { ++$statistics[$name]['hits']; } else { ++$statistics[$name]['misses']; ++$statistics[$name]['writes']; } } elseif ('getItem' === $call->name) { ++$statistics[$name]['reads']; if ($call->hits) { ++$statistics[$name]['hits']; } else { ++$statistics[$name]['misses']; } } elseif ('getItems' === $call->name) { $statistics[$name]['reads'] += $call->hits + $call->misses; $statistics[$name]['hits'] += $call->hits; $statistics[$name]['misses'] += $call->misses; } elseif ('hasItem' === $call->name) { ++$statistics[$name]['reads']; foreach ($call->result ?? [] as $result) { ++$statistics[$name][$result ? 'hits' : 'misses']; } } elseif ('save' === $call->name) { ++$statistics[$name]['writes']; } elseif ('deleteItem' === $call->name) { ++$statistics[$name]['deletes']; } } if ($statistics[$name]['reads']) { $statistics[$name]['hit_read_ratio'] = round(100 * $statistics[$name]['hits'] / $statistics[$name]['reads'], 2); } else { $statistics[$name]['hit_read_ratio'] = null; } } return $statistics; } private function calculateTotalStatistics(): array { $statistics = $this->getStatistics(); $totals = [ 'calls' => 0, 'time' => 0, 'reads' => 0, 'writes' => 0, 'deletes' => 0, 'hits' => 0, 'misses' => 0, ]; foreach ($statistics as $name => $values) { foreach ($totals as $key => $value) { $totals[$key] += $statistics[$name][$key]; } } if ($totals['reads']) { $totals['hit_read_ratio'] = round(100 * $totals['hits'] / $totals['reads'], 2); } else { $totals['hit_read_ratio'] = null; } return $totals; } } __halt_compiler();----SIGNATURE:----qbwBZs9ibRD5Km+sCDRdGxJo2GE++T/2Img5LSnclZhATQEL/xHUKTqVko8eod+BcAnepeZmXAiS2K0+mMotGlpZ3fmkDQQT1kjU0d9G34gNQrntl2K1QvVtMax1ZoMDgOYNxCj44QD8nzjdF2w0evYfaM23Ozo+zMdZe4hc59j1/JWPaPu5KIrJUcVpV8ysdOXx2X6cTVOV2qy2Mh9NGYfYcxTsHIsaaGe0IsnaQJRTA9KuoEzucUf/+DgFmekOZt+pJ6o76FYRbboIzVrkWrPz7I5aKA9r+21MPlM03s/BebnhVePMNK8j73eUbCPXwoZcAGLnMEWMg92vFf++vvLgpcfSeATGdgNdWPqz9A7vOYT2v6OKvryZ4gnsidF15JwD2wzlBdNuYq656dvcN3S0NfZpwOP6XhGqApw/iHyh/clsvKKHifOf9UYnSou26S67jHGzmQswradb5Puxrvx3fJ4Jm9C9XzvH+MuzMtPlstKSEFCEqyc4l+Ahl3z0k+WNJKK+YE/pRzlHCSjVG/3Yil41OXxvzha6SCezNb6dWjuG/rGke4X6aO4AI99Izlpthb45kbfASyIg2DbLP1FAQQiG/fGN/WjUlKn5zNpab45ZK6D9ISv6G3HuyO1iPwxampg06+Q2Anf5v/NNlh32E/qxg3fZOsryaOWiL+E=----ATTACHMENT:----MjAzOTk4NzM2NTgwNjEwNyAzMzY2NDk3OTg5MzYyNjcgNjkxMDU4MTEyOTAyNTA5Nw==