]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/Profiler.php
Merge pull request #10257 from annando/apcontact-no-normalize
[friendica.git] / src / Util / Profiler.php
index e745a8bbdf001e528cb5735a411698b8b43e0436..3e1a900ad56ab65dd3632f44f12c61428957b6a9 100644 (file)
@@ -1,9 +1,29 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Util;
 
-use Friendica\Core\Config\Cache\ConfigCache;
-use Friendica\Core\Config\Configuration;
+use Friendica\Core\Config\Cache;
+use Friendica\Core\Config\IConfig;
+use Friendica\Core\System;
 use Psr\Container\ContainerExceptionInterface;
 use Psr\Container\ContainerInterface;
 use Psr\Container\NotFoundExceptionInterface;
@@ -47,18 +67,18 @@ class Profiler implements ContainerInterface
        /**
         * Updates the enabling of the current profiler
         *
-        * @param Configuration $config
+        * @param IConfig $config
         */
-       public function update(Configuration $config)
+       public function update(IConfig $config)
        {
                $this->enabled = $config->get('system', 'profiler');
                $this->rendertime = $config->get('rendertime', 'callstack');
        }
 
        /**
-        * @param ConfigCache $configCache The configuration cache
+        * @param Cache $configCache The configuration cache
         */
-       public function __construct(ConfigCache $configCache)
+       public function __construct(Cache $configCache)
        {
                $this->enabled = $configCache->get('system', 'profiler');
                $this->rendertime = $configCache->get('rendertime', 'callstack');
@@ -69,9 +89,9 @@ class Profiler implements ContainerInterface
         * Saves a timestamp for a value - f.e. a call
         * Necessary for profiling Friendica
         *
-        * @param int $timestamp the Timestamp
-        * @param string $value A value to profile
-        * @param string $callstack The callstack of the current profiling data
+        * @param int    $timestamp the Timestamp
+        * @param string $value     A value to profile
+        * @param string $callstack A callstack string, generated if absent
         */
        public function saveTimestamp($timestamp, $value, $callstack = '')
        {
@@ -79,6 +99,8 @@ class Profiler implements ContainerInterface
                        return;
                }
 
+               $callstack = $callstack ?: System::callstack(4, 1);
+
                $duration = floatval(microtime(true) - $timestamp);
 
                if (!isset($this->performance[$value])) {
@@ -113,6 +135,7 @@ class Profiler implements ContainerInterface
        {
                $this->performance = [];
                $this->performance['start'] = microtime(true);
+               $this->performance['ready'] = 0;
                $this->performance['database'] = 0;
                $this->performance['database_write'] = 0;
                $this->performance['cache'] = 0;
@@ -123,6 +146,10 @@ class Profiler implements ContainerInterface
                $this->performance['parser'] = 0;
                $this->performance['marktime'] = 0;
                $this->performance['marktime'] = microtime(true);
+               $this->performance['classcreate'] = 0;
+               $this->performance['classinit'] = 0;
+               $this->performance['init'] = 0;
+               $this->performance['content'] = 0;
        }
 
        /**
@@ -143,10 +170,11 @@ class Profiler implements ContainerInterface
 
        /**
         * Returns the rendertime string
+        * @param float $limit Minimal limit for displaying the execution duration
         *
         * @return string the rendertime
         */
-       public function getRendertimeString()
+       public function getRendertimeString(float $limit = 0)
        {
                $output = '';
 
@@ -158,7 +186,7 @@ class Profiler implements ContainerInterface
                        $output .= "\nDatabase Read:\n";
                        foreach ($this->callstack["database"] as $func => $time) {
                                $time = round($time, 3);
-                               if ($time > 0) {
+                               if ($time > $limit) {
                                        $output .= $func . ": " . $time . "\n";
                                }
                        }
@@ -167,7 +195,7 @@ class Profiler implements ContainerInterface
                        $output .= "\nDatabase Write:\n";
                        foreach ($this->callstack["database_write"] as $func => $time) {
                                $time = round($time, 3);
-                               if ($time > 0) {
+                               if ($time > $limit) {
                                        $output .= $func . ": " . $time . "\n";
                                }
                        }
@@ -176,7 +204,7 @@ class Profiler implements ContainerInterface
                        $output .= "\nCache Read:\n";
                        foreach ($this->callstack["cache"] as $func => $time) {
                                $time = round($time, 3);
-                               if ($time > 0) {
+                               if ($time > $limit) {
                                        $output .= $func . ": " . $time . "\n";
                                }
                        }
@@ -185,7 +213,7 @@ class Profiler implements ContainerInterface
                        $output .= "\nCache Write:\n";
                        foreach ($this->callstack["cache_write"] as $func => $time) {
                                $time = round($time, 3);
-                               if ($time > 0) {
+                               if ($time > $limit) {
                                        $output .= $func . ": " . $time . "\n";
                                }
                        }
@@ -194,7 +222,7 @@ class Profiler implements ContainerInterface
                        $output .= "\nNetwork:\n";
                        foreach ($this->callstack["network"] as $func => $time) {
                                $time = round($time, 3);
-                               if ($time > 0) {
+                               if ($time > $limit) {
                                        $output .= $func . ": " . $time . "\n";
                                }
                        }
@@ -254,6 +282,11 @@ class Profiler implements ContainerInterface
                }
        }
 
+       public function set($timestamp, $id)
+       {
+               $this->performance[$id] = $timestamp;
+       }
+
        /**
         * Returns true if the container can return an entry for the given identifier.
         * Returns false otherwise.