1 | <?php |
||
17 | trait BenchmarkTrait |
||
18 | { |
||
19 | /** |
||
20 | * @invisible |
||
21 | * |
||
22 | * @var BenchmarkerInterface |
||
23 | */ |
||
24 | private $benchmarker; |
||
25 | |||
26 | /** |
||
27 | * Set custom benchmarker. |
||
28 | * |
||
29 | * @param BenchmarkerInterface $benchmarker |
||
30 | */ |
||
31 | 1 | public function setBenchmarker(BenchmarkerInterface $benchmarker) |
|
32 | { |
||
33 | 1 | $this->benchmarker = $benchmarker; |
|
34 | 1 | } |
|
35 | |||
36 | /** |
||
37 | * Benchmarks used to record long or important operations inside spiral components. Method |
||
38 | * should return elapsed time when record are be closed (same set of arguments has to be |
||
39 | * provided). |
||
40 | * |
||
41 | * @param string|array $record Benchmark record name or set of names. |
||
42 | * @param string $context Record context (if any). |
||
43 | * |
||
44 | * @return bool|float|mixed |
||
45 | */ |
||
46 | 3 | private function benchmark($record, string $context = '') |
|
|
|||
47 | { |
||
48 | 3 | if (empty($this->benchmarker)) { |
|
49 | 2 | $container = $this->iocContainer(); |
|
50 | |||
51 | 2 | if (empty($container) || !$container->has(BenchmarkerInterface::class)) { |
|
52 | //Nothing to do |
||
53 | 1 | return false; |
|
54 | } |
||
55 | |||
56 | 1 | $this->benchmarker = $container->get(BenchmarkerInterface::class); |
|
57 | } |
||
58 | |||
59 | 2 | return $this->benchmarker->benchmark($this, $record, $context); |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return ContainerInterface |
||
64 | */ |
||
65 | abstract protected function iocContainer(); |
||
66 | } |
||
67 |