3 namespace Friendica\Util;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\Configuration;
7 use Psr\Container\ContainerExceptionInterface;
8 use Psr\Container\ContainerInterface;
9 use Psr\Container\NotFoundExceptionInterface;
10 use Psr\Log\LoggerInterface;
13 * A class to store profiling data
14 * It can handle different logging data for specific functions or global performance measures
16 * It stores the data as log entries (@see LoggerInterface)
18 class Profiler implements ContainerInterface
21 * @var array The global performance array
25 * @var array The function specific callstack
29 * @var bool True, if the Profiler is enabled
33 * @var bool True, if the Profiler should measure the whole rendertime including functions
38 * True, if the Profiler should measure the whole rendertime including functions
42 public function isRendertime()
44 return $this->rendertime;
48 * Updates the enabling of the current profiler
50 * @param Configuration $config
52 public function update(Configuration $config)
54 $this->enabled = $config->get('system', 'profiler');
55 $this->rendertime = $config->get('rendertime', 'callstack');
59 * @param ConfigCache $configCache The configuration cache
61 public function __construct(ConfigCache $configCache)
63 $this->enabled = $configCache->get('system', 'profiler');
64 $this->rendertime = $configCache->get('rendertime', 'callstack');
69 * Saves a timestamp for a value - f.e. a call
70 * Necessary for profiling Friendica
72 * @param int $timestamp the Timestamp
73 * @param string $value A value to profile
74 * @param string $callstack The callstack of the current profiling data
76 public function saveTimestamp($timestamp, $value, $callstack = '')
78 if (!$this->enabled) {
82 $duration = floatval(microtime(true) - $timestamp);
84 if (!isset($this->performance[$value])) {
85 // Prevent ugly E_NOTICE
86 $this->performance[$value] = 0;
89 $this->performance[$value] += (float) $duration;
90 $this->performance['marktime'] += (float) $duration;
92 if (!isset($this->callstack[$value][$callstack])) {
93 // Prevent ugly E_NOTICE
94 $this->callstack[$value][$callstack] = 0;
97 $this->callstack[$value][$callstack] += (float) $duration;
101 * Resets the performance and callstack profiling
103 public function reset()
105 $this->resetPerformance();
106 $this->resetCallstack();
110 * Resets the performance profiling data
112 public function resetPerformance()
114 $this->performance = [];
115 $this->performance['start'] = microtime(true);
116 $this->performance['database'] = 0;
117 $this->performance['database_write'] = 0;
118 $this->performance['cache'] = 0;
119 $this->performance['cache_write'] = 0;
120 $this->performance['network'] = 0;
121 $this->performance['file'] = 0;
122 $this->performance['rendering'] = 0;
123 $this->performance['parser'] = 0;
124 $this->performance['marktime'] = 0;
125 $this->performance['marktime'] = microtime(true);
129 * Resets the callstack profiling data
131 public function resetCallstack()
133 $this->callstack = [];
134 $this->callstack['database'] = [];
135 $this->callstack['database_write'] = [];
136 $this->callstack['cache'] = [];
137 $this->callstack['cache_write'] = [];
138 $this->callstack['network'] = [];
139 $this->callstack['file'] = [];
140 $this->callstack['rendering'] = [];
141 $this->callstack['parser'] = [];
145 * Returns the rendertime string
147 * @return string the rendertime
149 public function getRendertimeString()
153 if (!$this->enabled || !$this->rendertime) {
157 if (isset($this->callstack["database"])) {
158 $output .= "\nDatabase Read:\n";
159 foreach ($this->callstack["database"] as $func => $time) {
160 $time = round($time, 3);
162 $output .= $func . ": " . $time . "\n";
166 if (isset($this->callstack["database_write"])) {
167 $output .= "\nDatabase Write:\n";
168 foreach ($this->callstack["database_write"] as $func => $time) {
169 $time = round($time, 3);
171 $output .= $func . ": " . $time . "\n";
175 if (isset($this->callstack["cache"])) {
176 $output .= "\nCache Read:\n";
177 foreach ($this->callstack["cache"] as $func => $time) {
178 $time = round($time, 3);
180 $output .= $func . ": " . $time . "\n";
184 if (isset($this->callstack["cache_write"])) {
185 $output .= "\nCache Write:\n";
186 foreach ($this->callstack["cache_write"] as $func => $time) {
187 $time = round($time, 3);
189 $output .= $func . ": " . $time . "\n";
193 if (isset($this->callstack["network"])) {
194 $output .= "\nNetwork:\n";
195 foreach ($this->callstack["network"] as $func => $time) {
196 $time = round($time, 3);
198 $output .= $func . ": " . $time . "\n";
207 * Save the current profiling data to a log entry
209 * @param LoggerInterface $logger The logger to save the current log
210 * @param string $message Additional message for the log
212 public function saveLog(LoggerInterface $logger, $message = '')
214 $duration = microtime(true) - $this->get('start');
218 'action' => 'profiling',
219 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
220 'database_write' => round($this->get('database_write'), 3),
221 'cache_read' => round($this->get('cache'), 3),
222 'cache_write' => round($this->get('cache_write'), 3),
223 'network_io' => round($this->get('network'), 2),
224 'file_io' => round($this->get('file'), 2),
225 'other_io' => round($duration - ($this->get('database')
226 + $this->get('cache') + $this->get('cache_write')
227 + $this->get('network') + $this->get('file')), 2),
228 'total' => round($duration, 2)
232 if ($this->isRendertime()) {
233 $output = $this->getRendertimeString();
234 $logger->info($message . ": " . $output, ['action' => 'profiling']);
239 * Finds an entry of the container by its identifier and returns it.
241 * @param string $id Identifier of the entry to look for.
243 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
244 * @throws ContainerExceptionInterface Error while retrieving the entry.
248 public function get($id)
250 if (!$this->has($id)) {
253 return $this->performance[$id];
258 * Returns true if the container can return an entry for the given identifier.
259 * Returns false otherwise.
261 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
262 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
264 * @param string $id Identifier of the entry to look for.
268 public function has($id)
270 return isset($this->performance[$id]);