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