3 namespace Friendica\Util;
5 use Psr\Container\ContainerExceptionInterface;
6 use Psr\Container\ContainerInterface;
7 use Psr\Container\NotFoundExceptionInterface;
8 use Psr\Log\LoggerInterface;
11 * A class to store profiling data
12 * It can handle different logging data for specific functions or global performance measures
14 * It stores the data as log entries (@see LoggerInterface )
16 class Profiler implements ContainerInterface
19 * @var array The global performance array
23 * @var array The function specific callstack
27 * @var bool True, if the Profiler is enabled
31 * @var bool True, if the Profiler should measure the whole rendertime including functions
36 * @var LoggerInterface The profiler logger
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
45 public function __construct(LoggerInterface $logger, $enabled = false, $renderTime = false)
47 $this->enabled = $enabled;
48 $this->rendertime = $renderTime;
49 $this->logger = $logger;
54 * Saves a timestamp for a value - f.e. a call
55 * Necessary for profiling Friendica
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
61 public function saveTimestamp($timestamp, $value, $callstack = '')
63 if (!$this->enabled) {
67 $duration = (float) (microtime(true) - $timestamp);
69 if (!isset($this->performance[$value])) {
70 // Prevent ugly E_NOTICE
71 $this->performance[$value] = 0;
74 $this->performance[$value] += (float) $duration;
75 $this->performance['marktime'] += (float) $duration;
77 if (!isset($this->callstack[$value][$callstack])) {
78 // Prevent ugly E_NOTICE
79 $this->callstack[$value][$callstack] = 0;
82 $this->callstack[$value][$callstack] += (float) $duration;
86 * Resets the performance and callstack profiling
88 public function reset()
90 $this->resetPerformance();
91 $this->resetCallstack();
95 * Resets the performance profiling data
97 public function resetPerformance()
99 $this->performance = [];
100 $this->performance['start'] = microtime(true);
101 $this->performance['database'] = 0;
102 $this->performance['database_write'] = 0;
103 $this->performance['cache'] = 0;
104 $this->performance['cache_write'] = 0;
105 $this->performance['network'] = 0;
106 $this->performance['file'] = 0;
107 $this->performance['rendering'] = 0;
108 $this->performance['parser'] = 0;
109 $this->performance['marktime'] = 0;
110 $this->performance['marktime'] = microtime(true);
114 * Resets the callstack profiling data
116 public function resetCallstack()
118 $this->callstack = [];
119 $this->callstack['database'] = [];
120 $this->callstack['database_write'] = [];
121 $this->callstack['cache'] = [];
122 $this->callstack['cache_write'] = [];
123 $this->callstack['network'] = [];
124 $this->callstack['file'] = [];
125 $this->callstack['rendering'] = [];
126 $this->callstack['parser'] = [];
130 * Save the current profiling data to a log entry
132 * @param string $message Additional message for the log
134 public function saveLog($message = '')
136 // Write down the performance values into the log
137 if (!$this->enabled) {
140 $duration = microtime(true) - $this->get('start');
144 'action' => 'profiling',
145 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
146 'database_write' => round($this->get('database_write'), 3),
147 'cache_read' => round($this->get('cache'), 3),
148 'cache_write' => round($this->get('cache_write'), 3),
149 'network_io' => round($this->get('network'), 2),
150 'file_io' => round($this->get('file'), 2),
151 'other_io' => round($duration - ($this->get('database')
152 + $this->get('cache') + $this->get('cache_write')
153 + $this->get('network') + $this->get('file')), 2),
154 'total' => round($duration, 2)
158 if (!$this->rendertime) {
163 if (isset($this->callstack["database"])) {
164 $o .= "\nDatabase Read:\n";
165 foreach ($this->callstack["database"] as $func => $time) {
166 $time = round($time, 3);
168 $o .= $func . ": " . $time . "\n";
172 if (isset($this->callstack["database_write"])) {
173 $o .= "\nDatabase Write:\n";
174 foreach ($this->callstack["database_write"] as $func => $time) {
175 $time = round($time, 3);
177 $o .= $func . ": " . $time . "\n";
181 if (isset($this->callstack["cache"])) {
182 $o .= "\nCache Read:\n";
183 foreach ($this->callstack["cache"] as $func => $time) {
184 $time = round($time, 3);
186 $o .= $func . ": " . $time . "\n";
190 if (isset($this->callstack["cache_write"])) {
191 $o .= "\nCache Write:\n";
192 foreach ($this->callstack["cache_write"] as $func => $time) {
193 $time = round($time, 3);
195 $o .= $func . ": " . $time . "\n";
199 if (isset($this->callstack["network"])) {
200 $o .= "\nNetwork:\n";
201 foreach ($this->callstack["network"] as $func => $time) {
202 $time = round($time, 3);
204 $o .= $func . ": " . $time . "\n";
208 $this->logger->info($message . ": " . $o, ['action' => 'profiling']);
212 * Finds an entry of the container by its identifier and returns it.
214 * @param string $id Identifier of the entry to look for.
216 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
217 * @throws ContainerExceptionInterface Error while retrieving the entry.
221 public function get($id)
223 if (!$this->has($id)) {
226 return $this->performance[$id];
231 * Returns true if the container can return an entry for the given identifier.
232 * Returns false otherwise.
234 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
235 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
237 * @param string $id Identifier of the entry to look for.
241 public function has($id)
243 return isset($this->performance[$id]);