]> git.mxchange.org Git - friendica.git/blob - src/Util/Profiler.php
5bad5eb6ae60091f2abf5a00f6ce66fa2b73586f
[friendica.git] / src / Util / Profiler.php
1 <?php
2
3 namespace Friendica\Util;
4
5 use Psr\Container\ContainerExceptionInterface;
6 use Psr\Container\ContainerInterface;
7 use Psr\Container\NotFoundExceptionInterface;
8 use Psr\Log\LoggerInterface;
9
10 /**
11  * A class to store profiling data
12  * It can handle different logging data for specific functions or global performance measures
13  *
14  * It stores the data as log entries (@see LoggerInterface )
15  */
16 class Profiler implements ContainerInterface
17 {
18         /**
19          * @var array The global performance array
20          */
21         private $performance;
22         /**
23          * @var array The function specific callstack
24          */
25         private $callstack;
26         /**
27          * @var bool True, if the Profiler is enabled
28          */
29         private $enabled;
30         /**
31          * @var bool True, if the Profiler should measure the whole rendertime including functions
32          */
33         private $rendertime;
34
35         /**
36          * @var LoggerInterface The profiler logger
37          */
38         private $logger;
39
40         /**
41          * @param LoggerInterface $logger The profiler logger
42          * @param bool $enabled           True, if the Profiler is enabled
43          * @param bool $renderTime        True, if the Profiler should measure the whole rendertime including functions
44          */
45         public function __construct(LoggerInterface $logger, $enabled = false, $renderTime = false)
46         {
47                 $this->enabled = $enabled;
48                 $this->rendertime = $renderTime;
49                 $this->logger = $logger;
50                 $this->reset();
51         }
52
53         /**
54          * Saves a timestamp for a value - f.e. a call
55          * Necessary for profiling Friendica
56          *
57          * @param int $timestamp the Timestamp
58          * @param string $value A value to profile
59          * @param string $callstack The callstack of the current profiling data
60          */
61         public function saveTimestamp($timestamp, $value, $callstack = '')
62         {
63                 if (!$this->enabled) {
64                         return;
65                 }
66
67                 $duration = (float) (microtime(true) - $timestamp);
68
69                 if (!isset($this->performance[$value])) {
70                         // Prevent ugly E_NOTICE
71                         $this->performance[$value] = 0;
72                 }
73
74                 $this->performance[$value] += (float) $duration;
75                 $this->performance['marktime'] += (float) $duration;
76
77                 if (!isset($this->callstack[$value][$callstack])) {
78                         // Prevent ugly E_NOTICE
79                         $this->callstack[$value][$callstack] = 0;
80                 }
81
82                 $this->callstack[$value][$callstack] += (float) $duration;
83         }
84
85         /**
86          * Resets the performance and callstack profiling
87          *
88          * @param bool $performance If true, reset the performance (Default true)
89          * @param bool $callstack   If true, reset the callstack (Default true)
90          */
91         public function reset($performance = true, $callstack = true)
92         {
93                 if ($performance) {
94                         $this->resetPerformance();
95                 }
96
97                 if ($callstack) {
98                         $this->resetCallstack();
99                 }
100         }
101
102         /**
103          * Resets the performance profiling data
104          */
105         public function resetPerformance()
106         {
107                 $this->performance = [];
108                 $this->performance['start'] = microtime(true);
109                 $this->performance['database'] = 0;
110                 $this->performance['database_write'] = 0;
111                 $this->performance['cache'] = 0;
112                 $this->performance['cache_write'] = 0;
113                 $this->performance['network'] = 0;
114                 $this->performance['file'] = 0;
115                 $this->performance['rendering'] = 0;
116                 $this->performance['parser'] = 0;
117                 $this->performance['marktime'] = 0;
118                 $this->performance['marktime'] = microtime(true);
119         }
120
121         /**
122          * Resets the callstack profiling data
123          */
124         public function resetCallstack()
125         {
126                 $this->callstack = [];
127                 $this->callstack['database'] = [];
128                 $this->callstack['database_write'] = [];
129                 $this->callstack['cache'] = [];
130                 $this->callstack['cache_write'] = [];
131                 $this->callstack['network'] = [];
132                 $this->callstack['file'] = [];
133                 $this->callstack['rendering'] = [];
134                 $this->callstack['parser'] = [];
135         }
136
137         /**
138          * Save the current profiling data to a log entry
139          *
140          * @param string $message Additional message for the log
141          */
142         public function saveLog($message = '')
143         {
144                 // Write down the performance values into the log
145                 if (!$this->enabled) {
146                         return;
147                 }
148                 $duration = microtime(true) - $this->get('start');
149                 $this->logger->info(
150                         $message,
151                         [
152                                 'module' => 'api',
153                                 'action' => 'call',
154                                 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
155                                 'database_write' => round($this->get('database_write'), 3),
156                                 'cache_read' => round($this->get('cache'), 3),
157                                 'cache_write' => round($this->get('cache_write'), 3),
158                                 'network_io' => round($this->get('network'), 2),
159                                 'file_io' => round($this->get('file'), 2),
160                                 'other_io' => round($duration - ($this->get('database')
161                                                 + $this->get('cache') + $this->get('cache_write')
162                                                 + $this->get('network') + $this->get('file')), 2),
163                                 'total' => round($duration, 2)
164                         ]
165                 );
166
167                 if (!$this->rendertime) {
168                         return;
169                 }
170                 
171                 $o = '';
172                 if (isset($this->callstack["database"])) {
173                         $o .= "\nDatabase Read:\n";
174                         foreach ($this->callstack["database"] as $func => $time) {
175                                 $time = round($time, 3);
176                                 if ($time > 0) {
177                                         $o .= $func . ": " . $time . "\n";
178                                 }
179                         }
180                 }
181                 if (isset($this->callstack["database_write"])) {
182                         $o .= "\nDatabase Write:\n";
183                         foreach ($this->callstack["database_write"] as $func => $time) {
184                                 $time = round($time, 3);
185                                 if ($time > 0) {
186                                         $o .= $func . ": " . $time . "\n";
187                                 }
188                         }
189                 }
190                 if (isset($this->callstack["cache"])) {
191                         $o .= "\nCache Read:\n";
192                         foreach ($this->callstack["cache"] as $func => $time) {
193                                 $time = round($time, 3);
194                                 if ($time > 0) {
195                                         $o .= $func . ": " . $time . "\n";
196                                 }
197                         }
198                 }
199                 if (isset($this->callstack["cache_write"])) {
200                         $o .= "\nCache Write:\n";
201                         foreach ($this->callstack["cache_write"] as $func => $time) {
202                                 $time = round($time, 3);
203                                 if ($time > 0) {
204                                         $o .= $func . ": " . $time . "\n";
205                                 }
206                         }
207                 }
208                 if (isset($this->callstack["network"])) {
209                         $o .= "\nNetwork:\n";
210                         foreach ($this->callstack["network"] as $func => $time) {
211                                 $time = round($time, 3);
212                                 if ($time > 0) {
213                                         $o .= $func . ": " . $time . "\n";
214                                 }
215                         }
216                 }
217                 $this->logger->info($message . ": " . $o);
218         }
219
220         /**
221          * Finds an entry of the container by its identifier and returns it.
222          *
223          * @param string $id Identifier of the entry to look for.
224          *
225          * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
226          * @throws ContainerExceptionInterface Error while retrieving the entry.
227          *
228          * @return int Entry.
229          */
230         public function get($id)
231         {
232                 if (!$this->has($id)) {
233                         return 0;
234                 } else {
235                         return $this->performance[$id];
236                 }
237         }
238
239         /**
240          * Returns true if the container can return an entry for the given identifier.
241          * Returns false otherwise.
242          *
243          * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
244          * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
245          *
246          * @param string $id Identifier of the entry to look for.
247          *
248          * @return bool
249          */
250         public function has($id)
251         {
252                 return isset($this->performance[$id]);
253         }
254 }