]> git.mxchange.org Git - friendica.git/blob - src/Util/Profiler.php
Adding composer & tests
[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->performance = [];
95                         $this->performance['start'] = microtime(true);
96                         $this->performance['database'] = 0;
97                         $this->performance['database_write'] = 0;
98                         $this->performance['cache'] = 0;
99                         $this->performance['cache_write'] = 0;
100                         $this->performance['network'] = 0;
101                         $this->performance['file'] = 0;
102                         $this->performance['rendering'] = 0;
103                         $this->performance['parser'] = 0;
104                         $this->performance['marktime'] = 0;
105                         $this->performance['marktime'] = microtime(true);
106                 }
107
108                 if ($callstack) {
109                         $this->callstack['database'] = [];
110                         $this->callstack['database_write'] = [];
111                         $this->callstack['cache'] = [];
112                         $this->callstack['cache_write'] = [];
113                         $this->callstack['network'] = [];
114                         $this->callstack['file'] = [];
115                         $this->callstack['rendering'] = [];
116                         $this->callstack['parser'] = [];
117                 }
118         }
119
120         /**
121          * Save the current profiling data to a log entry
122          *
123          * @param string $message Additional message for the log
124          */
125         public function saveLog($message = '')
126         {
127                 // Write down the performance values into the log
128                 if ($this->enabled) {
129                         $duration = microtime(true)-$this->get('start');
130                         $this->logger->info(
131                                 $message,
132                                 [
133                                         'module' => 'api',
134                                         'action' => 'call',
135                                         'database_read' => round($this->get('database') - $this->get('database_write'), 3),
136                                         'database_write' => round($this->get('database_write'), 3),
137                                         'cache_read' => round($this->get('cache'), 3),
138                                         'cache_write' => round($this->get('cache_write'), 3),
139                                         'network_io' => round($this->get('network'), 2),
140                                         'file_io' => round($this->get('file'), 2),
141                                         'other_io' => round($duration - ($this->get('database')
142                                                         + $this->get('cache') + $this->get('cache_write')
143                                                         + $this->get('network') + $this->get('file')), 2),
144                                         'total' => round($duration, 2)
145                                 ]
146                         );
147
148                         $o = '';
149                         if ($this->rendertime) {
150                                 if (isset($this->callstack["database"])) {
151                                         $o .= "\nDatabase Read:\n";
152                                         foreach ($this->callstack["database"] as $func => $time) {
153                                                 $time = round($time, 3);
154                                                 if ($time > 0) {
155                                                         $o .= $func.": ".$time."\n";
156                                                 }
157                                         }
158                                 }
159                                 if (isset($this->callstack["database_write"])) {
160                                         $o .= "\nDatabase Write:\n";
161                                         foreach ($this->callstack["database_write"] as $func => $time) {
162                                                 $time = round($time, 3);
163                                                 if ($time > 0) {
164                                                         $o .= $func.": ".$time."\n";
165                                                 }
166                                         }
167                                 }
168                                 if (isset($this->callstack["cache"])) {
169                                         $o .= "\nCache Read:\n";
170                                         foreach ($this->callstack["cache"] as $func => $time) {
171                                                 $time = round($time, 3);
172                                                 if ($time > 0) {
173                                                         $o .= $func.": ".$time."\n";
174                                                 }
175                                         }
176                                 }
177                                 if (isset($this->callstack["cache_write"])) {
178                                         $o .= "\nCache Write:\n";
179                                         foreach ($this->callstack["cache_write"] as $func => $time) {
180                                                 $time = round($time, 3);
181                                                 if ($time > 0) {
182                                                         $o .= $func.": ".$time."\n";
183                                                 }
184                                         }
185                                 }
186                                 if (isset($this->callstack["network"])) {
187                                         $o .= "\nNetwork:\n";
188                                         foreach ($this->callstack["network"] as $func => $time) {
189                                                 $time = round($time, 3);
190                                                 if ($time > 0) {
191                                                         $o .= $func.": ".$time."\n";
192                                                 }
193                                         }
194                                 }
195                                 $this->logger->info($message . ": " . $o);
196                         }
197
198                 }
199         }
200
201         /**
202          * Finds an entry of the container by its identifier and returns it.
203          *
204          * @param string $id Identifier of the entry to look for.
205          *
206          * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
207          * @throws ContainerExceptionInterface Error while retrieving the entry.
208          *
209          * @return int Entry.
210          */
211         public function get($id)
212         {
213                 if (!$this->has($id)) {
214                         return 0;
215                 } else {
216                         return $this->performance[$id];
217                 }
218         }
219
220         /**
221          * Returns true if the container can return an entry for the given identifier.
222          * Returns false otherwise.
223          *
224          * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
225          * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
226          *
227          * @param string $id Identifier of the entry to look for.
228          *
229          * @return bool
230          */
231         public function has($id)
232         {
233                 return isset($this->performance[$id]);
234         }
235 }