]> git.mxchange.org Git - friendica.git/blob - src/Util/Profiler.php
wrapping up 2019.12
[friendica.git] / src / Util / Profiler.php
1 <?php
2
3 namespace Friendica\Util;
4
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Configuration;
7 use Psr\Container\ContainerExceptionInterface;
8 use Psr\Container\ContainerInterface;
9 use Psr\Container\NotFoundExceptionInterface;
10 use Psr\Log\LoggerInterface;
11
12 /**
13  * A class to store profiling data
14  * It can handle different logging data for specific functions or global performance measures
15  *
16  * It stores the data as log entries (@see LoggerInterface)
17  */
18 class Profiler implements ContainerInterface
19 {
20         /**
21          * @var array The global performance array
22          */
23         private $performance;
24         /**
25          * @var array The function specific callstack
26          */
27         private $callstack;
28         /**
29          * @var bool True, if the Profiler is enabled
30          */
31         private $enabled;
32         /**
33          * @var bool True, if the Profiler should measure the whole rendertime including functions
34          */
35         private $rendertime;
36
37         /**
38          * True, if the Profiler should measure the whole rendertime including functions
39          *
40          * @return bool
41          */
42         public function isRendertime()
43         {
44                 return $this->rendertime;
45         }
46
47         /**
48          * Updates the enabling of the current profiler
49          *
50          * @param Configuration $config
51          */
52         public function update(Configuration $config)
53         {
54                 $this->enabled = $config->get('system', 'profiler');
55                 $this->rendertime = $config->get('rendertime', 'callstack');
56         }
57
58         /**
59          * @param ConfigCache $configCache The configuration cache
60          */
61         public function __construct(ConfigCache $configCache)
62         {
63                 $this->enabled = $configCache->get('system', 'profiler');
64                 $this->rendertime = $configCache->get('rendertime', 'callstack');
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 = floatval(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 }