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