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 * @param bool $enabled True, if the Profiler is enabled
37 * @param bool $renderTime True, if the Profiler should measure the whole rendertime including functions
39 public function __construct($enabled = false, $renderTime = false)
41 $this->enabled = $enabled;
42 $this->rendertime = $renderTime;
47 * Saves a timestamp for a value - f.e. a call
48 * Necessary for profiling Friendica
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
54 public function saveTimestamp($timestamp, $value, $callstack = '')
56 if (!$this->enabled) {
60 $duration = (float) (microtime(true) - $timestamp);
62 if (!isset($this->performance[$value])) {
63 // Prevent ugly E_NOTICE
64 $this->performance[$value] = 0;
67 $this->performance[$value] += (float) $duration;
68 $this->performance['marktime'] += (float) $duration;
70 if (!isset($this->callstack[$value][$callstack])) {
71 // Prevent ugly E_NOTICE
72 $this->callstack[$value][$callstack] = 0;
75 $this->callstack[$value][$callstack] += (float) $duration;
79 * Resets the performance and callstack profiling
81 public function reset()
83 $this->resetPerformance();
84 $this->resetCallstack();
88 * Resets the performance profiling data
90 public function resetPerformance()
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);
107 * Resets the callstack profiling data
109 public function resetCallstack()
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'] = [];
123 * Save the current profiling data to a log entry
125 * @param LoggerInterface $logger The logger to save the current log
126 * @param string $message Additional message for the log
128 public function saveLog(LoggerInterface $logger, $message = '')
130 // Write down the performance values into the log
131 if (!$this->enabled) {
134 $duration = microtime(true) - $this->get('start');
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)
152 if (!$this->rendertime) {
157 if (isset($this->callstack["database"])) {
158 $o .= "\nDatabase Read:\n";
159 foreach ($this->callstack["database"] as $func => $time) {
160 $time = round($time, 3);
162 $o .= $func . ": " . $time . "\n";
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);
171 $o .= $func . ": " . $time . "\n";
175 if (isset($this->callstack["cache"])) {
176 $o .= "\nCache Read:\n";
177 foreach ($this->callstack["cache"] as $func => $time) {
178 $time = round($time, 3);
180 $o .= $func . ": " . $time . "\n";
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);
189 $o .= $func . ": " . $time . "\n";
193 if (isset($this->callstack["network"])) {
194 $o .= "\nNetwork:\n";
195 foreach ($this->callstack["network"] as $func => $time) {
196 $time = round($time, 3);
198 $o .= $func . ": " . $time . "\n";
202 $logger->info($message . ": " . $o, ['action' => 'profiling']);
206 * Finds an entry of the container by its identifier and returns it.
208 * @param string $id Identifier of the entry to look for.
210 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
211 * @throws ContainerExceptionInterface Error while retrieving the entry.
215 public function get($id)
217 if (!$this->has($id)) {
220 return $this->performance[$id];
225 * Returns true if the container can return an entry for the given identifier.
226 * Returns false otherwise.
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`.
231 * @param string $id Identifier of the entry to look for.
235 public function has($id)
237 return isset($this->performance[$id]);