3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Core\System;
27 use Psr\Container\ContainerExceptionInterface;
28 use Psr\Container\ContainerInterface;
29 use Psr\Container\NotFoundExceptionInterface;
30 use Psr\Log\LoggerInterface;
33 * A class to store profiling data
34 * It can handle different logging data for specific functions or global performance measures
36 * It stores the data as log entries (@see LoggerInterface)
38 class Profiler implements ContainerInterface
41 * @var array The global performance array
45 * @var array The function specific callstack
49 * @var bool True, if the Profiler is enabled
53 * @var bool True, if the Profiler should measure the whole rendertime including functions
58 * True, if the Profiler should measure the whole rendertime including functions
62 public function isRendertime()
64 return $this->rendertime;
68 * Updates the enabling of the current profiler
70 * @param IConfig $config
72 public function update(IConfig $config)
74 $this->enabled = $config->get('system', 'profiler');
75 $this->rendertime = $config->get('rendertime', 'callstack');
79 * @param Cache $configCache The configuration cache
81 public function __construct(Cache $configCache)
83 $this->enabled = $configCache->get('system', 'profiler');
84 $this->rendertime = $configCache->get('rendertime', 'callstack');
89 * Saves a timestamp for a value - f.e. a call
90 * Necessary for profiling Friendica
92 * @param int $timestamp the Timestamp
93 * @param string $value A value to profile
94 * @param string $callstack A callstack string, generated if absent
96 public function saveTimestamp($timestamp, $value, $callstack = '')
98 if (!$this->enabled) {
102 $callstack = $callstack ?: System::callstack(4, 1);
104 $duration = floatval(microtime(true) - $timestamp);
106 if (!isset($this->performance[$value])) {
107 // Prevent ugly E_NOTICE
108 $this->performance[$value] = 0;
111 $this->performance[$value] += (float) $duration;
112 $this->performance['marktime'] += (float) $duration;
114 if (!isset($this->callstack[$value][$callstack])) {
115 // Prevent ugly E_NOTICE
116 $this->callstack[$value][$callstack] = 0;
119 $this->callstack[$value][$callstack] += (float) $duration;
123 * Resets the performance and callstack profiling
125 public function reset()
127 $this->resetPerformance();
128 $this->resetCallstack();
132 * Resets the performance profiling data
134 public function resetPerformance()
136 $this->performance = [];
137 $this->performance['start'] = microtime(true);
138 $this->performance['ready'] = 0;
139 $this->performance['database'] = 0;
140 $this->performance['database_write'] = 0;
141 $this->performance['cache'] = 0;
142 $this->performance['cache_write'] = 0;
143 $this->performance['network'] = 0;
144 $this->performance['file'] = 0;
145 $this->performance['rendering'] = 0;
146 $this->performance['parser'] = 0;
147 $this->performance['marktime'] = 0;
148 $this->performance['marktime'] = microtime(true);
149 $this->performance['classcreate'] = 0;
150 $this->performance['classinit'] = 0;
151 $this->performance['init'] = 0;
152 $this->performance['content'] = 0;
156 * Resets the callstack profiling data
158 public function resetCallstack()
160 $this->callstack = [];
161 $this->callstack['database'] = [];
162 $this->callstack['database_write'] = [];
163 $this->callstack['cache'] = [];
164 $this->callstack['cache_write'] = [];
165 $this->callstack['network'] = [];
166 $this->callstack['file'] = [];
167 $this->callstack['rendering'] = [];
168 $this->callstack['parser'] = [];
172 * Returns the rendertime string
173 * @param float $limit Minimal limit for displaying the execution duration
175 * @return string the rendertime
177 public function getRendertimeString(float $limit = 0)
181 if (!$this->enabled || !$this->rendertime) {
185 if (isset($this->callstack["database"])) {
186 $output .= "\nDatabase Read:\n";
187 foreach ($this->callstack["database"] as $func => $time) {
188 $time = round($time, 3);
189 if ($time > $limit) {
190 $output .= $func . ": " . $time . "\n";
194 if (isset($this->callstack["database_write"])) {
195 $output .= "\nDatabase Write:\n";
196 foreach ($this->callstack["database_write"] as $func => $time) {
197 $time = round($time, 3);
198 if ($time > $limit) {
199 $output .= $func . ": " . $time . "\n";
203 if (isset($this->callstack["cache"])) {
204 $output .= "\nCache Read:\n";
205 foreach ($this->callstack["cache"] as $func => $time) {
206 $time = round($time, 3);
207 if ($time > $limit) {
208 $output .= $func . ": " . $time . "\n";
212 if (isset($this->callstack["cache_write"])) {
213 $output .= "\nCache Write:\n";
214 foreach ($this->callstack["cache_write"] as $func => $time) {
215 $time = round($time, 3);
216 if ($time > $limit) {
217 $output .= $func . ": " . $time . "\n";
221 if (isset($this->callstack["network"])) {
222 $output .= "\nNetwork:\n";
223 foreach ($this->callstack["network"] as $func => $time) {
224 $time = round($time, 3);
225 if ($time > $limit) {
226 $output .= $func . ": " . $time . "\n";
235 * Save the current profiling data to a log entry
237 * @param LoggerInterface $logger The logger to save the current log
238 * @param string $message Additional message for the log
240 public function saveLog(LoggerInterface $logger, $message = '')
242 $duration = microtime(true) - $this->get('start');
246 'action' => 'profiling',
247 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
248 'database_write' => round($this->get('database_write'), 3),
249 'cache_read' => round($this->get('cache'), 3),
250 'cache_write' => round($this->get('cache_write'), 3),
251 'network_io' => round($this->get('network'), 2),
252 'file_io' => round($this->get('file'), 2),
253 'other_io' => round($duration - ($this->get('database')
254 + $this->get('cache') + $this->get('cache_write')
255 + $this->get('network') + $this->get('file')), 2),
256 'total' => round($duration, 2)
260 if ($this->isRendertime()) {
261 $output = $this->getRendertimeString();
262 $logger->info($message . ": " . $output, ['action' => 'profiling']);
267 * Finds an entry of the container by its identifier and returns it.
269 * @param string $id Identifier of the entry to look for.
271 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
272 * @throws ContainerExceptionInterface Error while retrieving the entry.
276 public function get($id)
278 if (!$this->has($id)) {
281 return $this->performance[$id];
285 public function set($timestamp, $id)
287 $this->performance[$id] = $timestamp;
291 * Returns true if the container can return an entry for the given identifier.
292 * Returns false otherwise.
294 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
295 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
297 * @param string $id Identifier of the entry to look for.
301 public function has($id)
303 return isset($this->performance[$id]);