]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Profiler.php
Merge pull request #7168 from annando/suggest-receive
[friendica.git] / src / Util / Profiler.php
index 7bf30a2775d95cbecdba49e75a3d8aca4c77fd64..fe72efce40beec2e623598e3c6efd35c050d3f54 100644 (file)
@@ -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,36 @@ class Profiler implements ContainerInterface
        private $rendertime;
 
        /**
-        * @var LoggerInterface The profiler logger
+        * True, if the Profiler should measure the whole rendertime including functions
+        *
+        * @return bool
+        */
+       public function isRendertime()
+       {
+               return $this->rendertime;
+       }
+
+       /**
+        * Updates the enabling of the current profiler
+        *
+        * @param bool $enabled
+        * @param bool $renderTime
         */
-       private $logger;
+       public function update($enabled = false, $renderTime = false)
+       {
+               $this->enabled = $enabled;
+               $this->rendertime = $renderTime;
+       }
 
        /**
-        * @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 +71,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 +89,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,131 +99,139 @@ 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);
        }
 
        /**
-        * Save the current profiling data to a log entry
+        * 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'] = [];
+       }
+
+       /**
+        * Returns the rendertime string
         *
-        * @param string $message Additional message for the log
+        * @return string the rendertime
         */
-       public function saveLog($message)
+       public function getRendertimeString()
        {
-               // 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";
-                                               }
-                                       }
+               $output = '';
+
+               if (!$this->enabled || !$this->rendertime) {
+                       return $output;
+               }
+
+               if (isset($this->callstack["database"])) {
+                       $output .= "\nDatabase Read:\n";
+                       foreach ($this->callstack["database"] as $func => $time) {
+                               $time = round($time, 3);
+                               if ($time > 0) {
+                                       $output .= $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"])) {
+                       $output .= "\nDatabase Write:\n";
+                       foreach ($this->callstack["database_write"] as $func => $time) {
+                               $time = round($time, 3);
+                               if ($time > 0) {
+                                       $output .= $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"])) {
+                       $output .= "\nCache Read:\n";
+                       foreach ($this->callstack["cache"] as $func => $time) {
+                               $time = round($time, 3);
+                               if ($time > 0) {
+                                       $output .= $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"])) {
+                       $output .= "\nCache Write:\n";
+                       foreach ($this->callstack["cache_write"] as $func => $time) {
+                               $time = round($time, 3);
+                               if ($time > 0) {
+                                       $output .= $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"])) {
+                       $output .= "\nNetwork:\n";
+                       foreach ($this->callstack["network"] as $func => $time) {
+                               $time = round($time, 3);
+                               if ($time > 0) {
+                                       $output .= $func . ": " . $time . "\n";
                                }
                        }
+               }
+
+               return $output;
+       }
+
+       /**
+        * Save the current profiling data to a log entry
+        *
+        * @param LoggerInterface $logger  The logger to save the current log
+        * @param string          $message Additional message for the log
+        */
+       public function saveLog(LoggerInterface $logger, $message = '')
+       {
+               $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)
+                       ]
+               );
 
-                       $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)
-                               )
-                       );
+               if ($this->isRendertime()) {
+                       $output = $this->getRendertimeString();
+                       $logger->info($message . ": " . $output, ['action' => 'profiling']);
                }
        }