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