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 Psr\Container\ContainerExceptionInterface;
27 use Psr\Container\ContainerInterface;
28 use Psr\Container\NotFoundExceptionInterface;
29 use Psr\Log\LoggerInterface;
32 * A class to store profiling data
33 * It can handle different logging data for specific functions or global performance measures
35 * It stores the data as log entries (@see LoggerInterface)
37 class Profiler implements ContainerInterface
40 * @var array The global performance array
44 * @var array The function specific callstack
48 * @var bool True, if the Profiler is enabled
52 * @var bool True, if the Profiler should measure the whole rendertime including functions
57 * True, if the Profiler should measure the whole rendertime including functions
61 public function isRendertime()
63 return $this->rendertime;
67 * Updates the enabling of the current profiler
69 * @param IConfig $config
71 public function update(IConfig $config)
73 $this->enabled = $config->get('system', 'profiler');
74 $this->rendertime = $config->get('rendertime', 'callstack');
78 * @param Cache $configCache The configuration cache
80 public function __construct(Cache $configCache)
82 $this->enabled = $configCache->get('system', 'profiler');
83 $this->rendertime = $configCache->get('rendertime', 'callstack');
88 * Saves a timestamp for a value - f.e. a call
89 * Necessary for profiling Friendica
91 * @param int $timestamp the Timestamp
92 * @param string $value A value to profile
93 * @param string $callstack The callstack of the current profiling data
95 public function saveTimestamp($timestamp, $value, $callstack = '')
97 if (!$this->enabled) {
101 $duration = floatval(microtime(true) - $timestamp);
103 if (!isset($this->performance[$value])) {
104 // Prevent ugly E_NOTICE
105 $this->performance[$value] = 0;
108 $this->performance[$value] += (float) $duration;
109 $this->performance['marktime'] += (float) $duration;
111 if (!isset($this->callstack[$value][$callstack])) {
112 // Prevent ugly E_NOTICE
113 $this->callstack[$value][$callstack] = 0;
116 $this->callstack[$value][$callstack] += (float) $duration;
120 * Resets the performance and callstack profiling
122 public function reset()
124 $this->resetPerformance();
125 $this->resetCallstack();
129 * Resets the performance profiling data
131 public function resetPerformance()
133 $this->performance = [];
134 $this->performance['start'] = microtime(true);
135 $this->performance['database'] = 0;
136 $this->performance['database_write'] = 0;
137 $this->performance['cache'] = 0;
138 $this->performance['cache_write'] = 0;
139 $this->performance['network'] = 0;
140 $this->performance['file'] = 0;
141 $this->performance['rendering'] = 0;
142 $this->performance['parser'] = 0;
143 $this->performance['marktime'] = 0;
144 $this->performance['marktime'] = microtime(true);
148 * Resets the callstack profiling data
150 public function resetCallstack()
152 $this->callstack = [];
153 $this->callstack['database'] = [];
154 $this->callstack['database_write'] = [];
155 $this->callstack['cache'] = [];
156 $this->callstack['cache_write'] = [];
157 $this->callstack['network'] = [];
158 $this->callstack['file'] = [];
159 $this->callstack['rendering'] = [];
160 $this->callstack['parser'] = [];
164 * Returns the rendertime string
166 * @return string the rendertime
168 public function getRendertimeString()
172 if (!$this->enabled || !$this->rendertime) {
176 if (isset($this->callstack["database"])) {
177 $output .= "\nDatabase Read:\n";
178 foreach ($this->callstack["database"] as $func => $time) {
179 $time = round($time, 3);
181 $output .= $func . ": " . $time . "\n";
185 if (isset($this->callstack["database_write"])) {
186 $output .= "\nDatabase Write:\n";
187 foreach ($this->callstack["database_write"] as $func => $time) {
188 $time = round($time, 3);
190 $output .= $func . ": " . $time . "\n";
194 if (isset($this->callstack["cache"])) {
195 $output .= "\nCache Read:\n";
196 foreach ($this->callstack["cache"] as $func => $time) {
197 $time = round($time, 3);
199 $output .= $func . ": " . $time . "\n";
203 if (isset($this->callstack["cache_write"])) {
204 $output .= "\nCache Write:\n";
205 foreach ($this->callstack["cache_write"] as $func => $time) {
206 $time = round($time, 3);
208 $output .= $func . ": " . $time . "\n";
212 if (isset($this->callstack["network"])) {
213 $output .= "\nNetwork:\n";
214 foreach ($this->callstack["network"] as $func => $time) {
215 $time = round($time, 3);
217 $output .= $func . ": " . $time . "\n";
226 * Save the current profiling data to a log entry
228 * @param LoggerInterface $logger The logger to save the current log
229 * @param string $message Additional message for the log
231 public function saveLog(LoggerInterface $logger, $message = '')
233 $duration = microtime(true) - $this->get('start');
237 'action' => 'profiling',
238 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
239 'database_write' => round($this->get('database_write'), 3),
240 'cache_read' => round($this->get('cache'), 3),
241 'cache_write' => round($this->get('cache_write'), 3),
242 'network_io' => round($this->get('network'), 2),
243 'file_io' => round($this->get('file'), 2),
244 'other_io' => round($duration - ($this->get('database')
245 + $this->get('cache') + $this->get('cache_write')
246 + $this->get('network') + $this->get('file')), 2),
247 'total' => round($duration, 2)
251 if ($this->isRendertime()) {
252 $output = $this->getRendertimeString();
253 $logger->info($message . ": " . $output, ['action' => 'profiling']);
258 * Finds an entry of the container by its identifier and returns it.
260 * @param string $id Identifier of the entry to look for.
262 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
263 * @throws ContainerExceptionInterface Error while retrieving the entry.
267 public function get($id)
269 if (!$this->has($id)) {
272 return $this->performance[$id];
277 * Returns true if the container can return an entry for the given identifier.
278 * Returns false otherwise.
280 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
281 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
283 * @param string $id Identifier of the entry to look for.
287 public function has($id)
289 return isset($this->performance[$id]);