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