]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Profiler.php
Merge pull request #6641 from nupplaphil/config_followup
[friendica.git] / src / Util / Profiler.php
index f65bede152ae3cb149ade2eef99e192abdf2aac4..2d3da3a9c0b99f762a29f9b9bb9e7288a87ab337 100644 (file)
@@ -33,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();
        }
 
        /**
@@ -85,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']);
        }
 
        /**