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