X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FProfiler.php;h=2d3da3a9c0b99f762a29f9b9bb9e7288a87ab337;hb=1df19d3553efc02c76ed17cdc617312f19b21bce;hp=7bf30a2775d95cbecdba49e75a3d8aca4c77fd64;hpb=5e6e1a80250a9b03a0689bbda92a6a66140cc669;p=friendica.git diff --git a/src/Util/Profiler.php b/src/Util/Profiler.php index 7bf30a2775..2d3da3a9c0 100644 --- a/src/Util/Profiler.php +++ b/src/Util/Profiler.php @@ -2,7 +2,6 @@ namespace Friendica\Util; -use Friendica\Core; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -34,22 +33,14 @@ class Profiler implements ContainerInterface private $rendertime; /** - * @var LoggerInterface The profiler logger - */ - private $logger; - - /** - * @param LoggerInterface $logger The profiler logger * @param bool $enabled True, if the Profiler is enabled * @param bool $renderTime True, if the Profiler should measure the whole rendertime including functions */ - public function __construct(LoggerInterface $logger, $enabled = false, $renderTime = false) + public function __construct($enabled = false, $renderTime = false) { $this->enabled = $enabled; $this->rendertime = $renderTime; - $this->logger = $logger; - $this->performance = []; - $this->callstack = []; + $this->reset(); } /** @@ -58,8 +49,9 @@ class Profiler implements ContainerInterface * * @param int $timestamp the Timestamp * @param string $value A value to profile + * @param string $callstack The callstack of the current profiling data */ - public function saveTimestamp($timestamp, $value) + public function saveTimestamp($timestamp, $value, $callstack = '') { if (!$this->enabled) { return; @@ -75,8 +67,6 @@ class Profiler implements ContainerInterface $this->performance[$value] += (float) $duration; $this->performance['marktime'] += (float) $duration; - $callstack = Core\System::callstack(); - if (!isset($this->callstack[$value][$callstack])) { // Prevent ugly E_NOTICE $this->callstack[$value][$callstack] = 0; @@ -87,132 +77,129 @@ class Profiler implements ContainerInterface /** * Resets the performance and callstack profiling - * - * @param bool $performance If true, reset the performance (Default true) - * @param bool $callstack If true, reset the callstack (Default true) */ - public function reset($performance = true, $callstack = true) + public function reset() { - if ($performance) { - $this->performance = []; - $this->performance['start'] = microtime(true); - $this->performance['database'] = 0; - $this->performance['database_write'] = 0; - $this->performance['cache'] = 0; - $this->performance['cache_write'] = 0; - $this->performance['network'] = 0; - $this->performance['file'] = 0; - $this->performance['rendering'] = 0; - $this->performance['parser'] = 0; - $this->performance['marktime'] = 0; - $this->performance['markstart'] = microtime(true); - } + $this->resetPerformance(); + $this->resetCallstack(); + } - if ($callstack) { - $this->callstack['database'] = []; - $this->callstack['database_write'] = []; - $this->callstack['cache'] = []; - $this->callstack['cache_write'] = []; - $this->callstack['network'] = []; - $this->callstack['file'] = []; - $this->callstack['rendering'] = []; - $this->callstack['parser'] = []; - } + /** + * Resets the performance profiling data + */ + public function resetPerformance() + { + $this->performance = []; + $this->performance['start'] = microtime(true); + $this->performance['database'] = 0; + $this->performance['database_write'] = 0; + $this->performance['cache'] = 0; + $this->performance['cache_write'] = 0; + $this->performance['network'] = 0; + $this->performance['file'] = 0; + $this->performance['rendering'] = 0; + $this->performance['parser'] = 0; + $this->performance['marktime'] = 0; + $this->performance['marktime'] = microtime(true); + } + + /** + * Resets the callstack profiling data + */ + public function resetCallstack() + { + $this->callstack = []; + $this->callstack['database'] = []; + $this->callstack['database_write'] = []; + $this->callstack['cache'] = []; + $this->callstack['cache_write'] = []; + $this->callstack['network'] = []; + $this->callstack['file'] = []; + $this->callstack['rendering'] = []; + $this->callstack['parser'] = []; } /** * Save the current profiling data to a log entry * - * @param string $message Additional message for the log + * @param LoggerInterface $logger The logger to save the current log + * @param string $message Additional message for the log */ - public function saveLog($message) + public function saveLog(LoggerInterface $logger, $message = '') { // Write down the performance values into the log - if ($this->enabled) { - $duration = microtime(true)-$this->get('start'); - $this->logger->info( - $message, - [ - 'module' => 'api', - 'action' => 'call', - 'database_read' => round($this->get('database') - $this->get('database_write'), 3), - 'database_write' => round($this->get('database_write'), 3), - 'cache_read' => round($this->get('cache'), 3), - 'cache_write' => round($this->get('cache_write'), 3), - 'network_io' => round($this->get('network'), 2), - 'file_io' => round($this->get('file'), 2), - 'other_io' => round($duration - ($this->get('database') - + $this->get('cache') + $this->get('cache_write') - + $this->get('network') + $this->get('file')), 2), - 'total' => round($duration, 2) - ] - ); - - $o = ''; - if ($this->rendertime) { - if (isset($this->callstack["database"])) { - $o .= "\nDatabase Read:\n"; - foreach ($this->callstack["database"] as $func => $time) { - $time = round($time, 3); - if ($time > 0) { - $o .= $func.": ".$time."\n"; - } - } + if (!$this->enabled) { + return; + } + $duration = microtime(true) - $this->get('start'); + $logger->info( + $message, + [ + 'action' => 'profiling', + 'database_read' => round($this->get('database') - $this->get('database_write'), 3), + 'database_write' => round($this->get('database_write'), 3), + 'cache_read' => round($this->get('cache'), 3), + 'cache_write' => round($this->get('cache_write'), 3), + 'network_io' => round($this->get('network'), 2), + 'file_io' => round($this->get('file'), 2), + 'other_io' => round($duration - ($this->get('database') + + $this->get('cache') + $this->get('cache_write') + + $this->get('network') + $this->get('file')), 2), + 'total' => round($duration, 2) + ] + ); + + if (!$this->rendertime) { + return; + } + + $o = ''; + if (isset($this->callstack["database"])) { + $o .= "\nDatabase Read:\n"; + foreach ($this->callstack["database"] as $func => $time) { + $time = round($time, 3); + if ($time > 0) { + $o .= $func . ": " . $time . "\n"; } - if (isset($this->callstack["database_write"])) { - $o .= "\nDatabase Write:\n"; - foreach ($this->callstack["database_write"] as $func => $time) { - $time = round($time, 3); - if ($time > 0) { - $o .= $func.": ".$time."\n"; - } - } + } + } + if (isset($this->callstack["database_write"])) { + $o .= "\nDatabase Write:\n"; + foreach ($this->callstack["database_write"] as $func => $time) { + $time = round($time, 3); + if ($time > 0) { + $o .= $func . ": " . $time . "\n"; } - if (isset($this->callstack["dache"])) { - $o .= "\nCache Read:\n"; - foreach ($this->callstack["dache"] as $func => $time) { - $time = round($time, 3); - if ($time > 0) { - $o .= $func.": ".$time."\n"; - } - } + } + } + if (isset($this->callstack["cache"])) { + $o .= "\nCache Read:\n"; + foreach ($this->callstack["cache"] as $func => $time) { + $time = round($time, 3); + if ($time > 0) { + $o .= $func . ": " . $time . "\n"; } - if (isset($this->callstack["dache_write"])) { - $o .= "\nCache Write:\n"; - foreach ($this->callstack["dache_write"] as $func => $time) { - $time = round($time, 3); - if ($time > 0) { - $o .= $func.": ".$time."\n"; - } - } + } + } + if (isset($this->callstack["cache_write"])) { + $o .= "\nCache Write:\n"; + foreach ($this->callstack["cache_write"] as $func => $time) { + $time = round($time, 3); + if ($time > 0) { + $o .= $func . ": " . $time . "\n"; } - if (isset($this->callstack["network"])) { - $o .= "\nNetwork:\n"; - foreach ($this->callstack["network"] as $func => $time) { - $time = round($time, 3); - if ($time > 0) { - $o .= $func.": ".$time."\n"; - } - } + } + } + if (isset($this->callstack["network"])) { + $o .= "\nNetwork:\n"; + foreach ($this->callstack["network"] as $func => $time) { + $time = round($time, 3); + if ($time > 0) { + $o .= $func . ": " . $time . "\n"; } } - - $this->logger->info( - $message . ": " . sprintf( - "DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o, - number_format($this->get('database') - $this->get('database_write'), 2), - number_format($this->get('database_write'), 2), - number_format($this->get('cache'), 2), - number_format($this->get('cache_write'), 2), - number_format($this->get('network'), 2), - number_format($this->get('file'), 2), - number_format($duration - ($this->get('database') - + $this->get('cache') + $this->get('cache_write') - + $this->get('network') + $this->get('file')), 2), - number_format($duration, 2) - ) - ); } + $logger->info($message . ": " . $o, ['action' => 'profiling']); } /**