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 * Returns the rendertime string
125 * @return string the rendertime
127 public function getRendertimeString()
131 if (!$this->enabled || !$this->rendertime) {
135 if (isset($this->callstack["database"])) {
136 $output .= "\nDatabase Read:\n";
137 foreach ($this->callstack["database"] as $func => $time) {
138 $time = round($time, 3);
140 $output .= $func . ": " . $time . "\n";
144 if (isset($this->callstack["database_write"])) {
145 $output .= "\nDatabase Write:\n";
146 foreach ($this->callstack["database_write"] as $func => $time) {
147 $time = round($time, 3);
149 $output .= $func . ": " . $time . "\n";
153 if (isset($this->callstack["cache"])) {
154 $output .= "\nCache Read:\n";
155 foreach ($this->callstack["cache"] as $func => $time) {
156 $time = round($time, 3);
158 $output .= $func . ": " . $time . "\n";
162 if (isset($this->callstack["cache_write"])) {
163 $output .= "\nCache Write:\n";
164 foreach ($this->callstack["cache_write"] as $func => $time) {
165 $time = round($time, 3);
167 $output .= $func . ": " . $time . "\n";
171 if (isset($this->callstack["network"])) {
172 $output .= "\nNetwork:\n";
173 foreach ($this->callstack["network"] as $func => $time) {
174 $time = round($time, 3);
176 $output .= $func . ": " . $time . "\n";
185 * Save the current profiling data to a log entry
187 * @param LoggerInterface $logger The logger to save the current log
188 * @param string $message Additional message for the log
190 public function saveLog(LoggerInterface $logger, $message = '')
192 $duration = microtime(true) - $this->get('start');
196 'action' => 'profiling',
197 'database_read' => round($this->get('database') - $this->get('database_write'), 3),
198 'database_write' => round($this->get('database_write'), 3),
199 'cache_read' => round($this->get('cache'), 3),
200 'cache_write' => round($this->get('cache_write'), 3),
201 'network_io' => round($this->get('network'), 2),
202 'file_io' => round($this->get('file'), 2),
203 'other_io' => round($duration - ($this->get('database')
204 + $this->get('cache') + $this->get('cache_write')
205 + $this->get('network') + $this->get('file')), 2),
206 'total' => round($duration, 2)
210 $output = $this->getRendertimeString();
211 $logger->info($message . ": " . $output, ['action' => 'profiling']);
215 * Finds an entry of the container by its identifier and returns it.
217 * @param string $id Identifier of the entry to look for.
219 * @throws NotFoundExceptionInterface No entry was found for **this** identifier.
220 * @throws ContainerExceptionInterface Error while retrieving the entry.
224 public function get($id)
226 if (!$this->has($id)) {
229 return $this->performance[$id];
234 * Returns true if the container can return an entry for the given identifier.
235 * Returns false otherwise.
237 * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
238 * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
240 * @param string $id Identifier of the entry to look for.
244 public function has($id)
246 return isset($this->performance[$id]);