]> git.mxchange.org Git - friendica.git/commitdiff
Refactored DependencyFactory for Profiler
authorPhilipp Holzer <admin@philipp.info>
Sun, 17 Feb 2019 20:12:12 +0000 (21:12 +0100)
committerPhilipp Holzer <admin@philipp.info>
Sun, 17 Feb 2019 20:12:12 +0000 (21:12 +0100)
12 files changed:
bin/daemon.php
include/api.php
src/App.php
src/Factory/DBFactory.php
src/Factory/DependencyFactory.php
src/Factory/LoggerFactory.php
src/Factory/ProfilerFactory.php
src/Util/Profiler.php
tests/include/ApiTest.php
tests/src/Database/DBATest.php
tests/src/Database/DBStructureTest.php
tests/src/Util/ProfilerTest.php

index 257896cfacd7d6a6b6f3ec23f92d7a7d156e5972..047bf71be77b4ac31dbea889098e5dd52dce8b5b 100755 (executable)
@@ -144,7 +144,7 @@ if (!$foreground) {
        file_put_contents($pidfile, $pid);
 
        // We lose the database connection upon forking
-       Factory\DBFactory::init($configCache, $_SERVER);
+       Factory\DBFactory::init($a->getConfigCache(), $a->getProfiler(), $_SERVER);
 }
 
 Config::set('system', 'worker_daemon_mode', true);
index 6bd08d01d0cb6bc43e246216098eacefd7aeb598..13ff75bdb50a81f1f602ea70e445ccae9b05b6c6 100644 (file)
@@ -326,7 +326,7 @@ function api_call(App $a)
 
                                Logger::info(API_LOG_PREFIX . 'username {username}', ['module' => 'api', 'action' => 'call', 'username' => $a->user['username'], 'duration' => round($duration, 2)]);
 
-                               $a->getProfiler()->saveLog(API_LOG_PREFIX . 'performance');
+                               $a->getProfiler()->saveLog($a->getLogger(), API_LOG_PREFIX . 'performance');
 
                                if (false === $return) {
                                        /*
index 3189b2da7b44949e7215d6b1f67d848e0c1f774f..8a49f060ba16ac8a31de5fa9904ac03d44d43063 100644 (file)
@@ -12,7 +12,6 @@ use Friendica\Core\Config\Cache\ConfigCacheLoader;
 use Friendica\Core\Config\Cache\IConfigCache;
 use Friendica\Core\Config\Configuration;
 use Friendica\Database\DBA;
-use Friendica\Factory\ConfigFactory;
 use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Util\Profiler;
 use Psr\Log\LoggerInterface;
@@ -114,6 +113,11 @@ class App
         */
        private $config;
 
+       /**
+        * @var LoggerInterface The logger
+        */
+       private $logger;
+
        /**
         * @var Profiler The profiler of this app
         */
@@ -139,6 +143,16 @@ class App
                return $this->basePath;
        }
 
+       /**
+        * The Logger of this app
+        *
+        * @return LoggerInterface
+        */
+       public function getLogger()
+       {
+               return $this->logger;
+       }
+
        /**
         * The profiler of this app
         *
@@ -192,7 +206,7 @@ class App
         * @brief App constructor.
         *
         * @param Configuration    $config    The Configuration
-        * @param LoggerInterface  $logger    Logger of this application
+        * @param LoggerInterface  $logger    The current app logger
         * @param Profiler         $profiler  The profiler of this application
         * @param bool             $isBackend Whether it is used for backend or frontend (Default true=backend)
         *
@@ -200,8 +214,8 @@ class App
         */
        public function __construct(Configuration $config, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
        {
-               $this->config   = $config;
                $this->logger   = $logger;
+               $this->config   = $config;
                $this->profiler = $profiler;
                $this->basePath = $this->config->get('system', 'basepath');
 
index abfe6f99d0ed5c18f02ad1f59a209f91889aca97..c1a7965013e84a14dff775c365a7d11e2f3a9a85 100644 (file)
@@ -4,18 +4,20 @@ namespace Friendica\Factory;
 
 use Friendica\Core\Config\Cache;
 use Friendica\Database;
+use Friendica\Util\Profiler;
 
 class DBFactory
 {
        /**
         * Initialize the DBA connection
         *
-        * @param Cache\ConfigCache $configCache The configuration cache
+        * @param Cache\IConfigCache $configCache The configuration cache
+        * @param Profiler          $profiler    The profiler
         * @param array             $server      The $_SERVER variables
         *
         * @throws \Exception if connection went bad
         */
-       public static function init(Cache\ConfigCache $configCache, array $server)
+       public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
        {
                if (Database\DBA::connected()) {
                        return;
@@ -46,7 +48,7 @@ class DBFactory
                        $db_data = $server['MYSQL_DATABASE'];
                }
 
-               if (Database\DBA::connect($configCache, $db_host, $db_user, $db_pass, $db_data, $charset)) {
+               if (Database\DBA::connect($configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
                        // Loads DB_UPDATE_VERSION constant
                        Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
                }
index 041202e6bfde7eedad4dbe9ffba1d0edbbeff41e..acbf4bfaf7a0ced05776e455404baba58439d194 100644 (file)
@@ -25,12 +25,13 @@ class DependencyFactory
                $basedir = BasePath::create($directory, $_SERVER);
                $configLoader = new Cache\ConfigCacheLoader($basedir);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
-               Factory\DBFactory::init($configCache, $_SERVER);
+               $profiler = Factory\ProfilerFactory::create($configCache);
+               Factory\DBFactory::init($configCache, $profiler, $_SERVER);
                $config = Factory\ConfigFactory::createConfig($configCache);
                // needed to call PConfig::init()
                Factory\ConfigFactory::createPConfig($configCache);
-               Factory\LoggerFactory::create($channel, $config);
+               $logger = Factory\LoggerFactory::create($channel, $config);
 
-               return new App($config, $isBackend);
+               return new App($config, $logger, $profiler, $isBackend);
        }
 }
index bbe3b0a4b31acb116141079f513c25f628a0b959..77a09637c46d9dc3dffc23330f543de9ffe31e23 100644 (file)
@@ -42,6 +42,8 @@ class LoggerFactory
                if ($debugging) {
                        $loglevel = self::mapLegacyConfigDebugLevel((string)$level);
                        static::addStreamHandler($logger, $stream, $loglevel);
+               } else {
+                       static::addVoidHandler($logger);
                }
 
                Logger::init($logger);
@@ -153,4 +155,11 @@ class LoggerFactory
                        throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
                }
        }
+
+       public static function addVoidHandler($logger)
+       {
+               if ($logger instanceof Monolog\Logger) {
+                       $logger->pushHandler(new Monolog\Handler\NullHandler());
+               }
+       }
 }
index 7a63c7440cca689a8b31e14a08abb77dcdad03c4..26a156639e6bb05d79d8976f0862418cef2183be 100644 (file)
@@ -2,24 +2,25 @@
 
 namespace Friendica\Factory;
 
-use Friendica\Core\Config\ConfigCache;
+use Friendica\Core\Config\Cache\IConfigCache;
 use Friendica\Util\Profiler;
-use Psr\Log\LoggerInterface;
 
 class ProfilerFactory
 {
        /**
         * Creates a Profiler for the current execution
         *
-        * @param LoggerInterface $logger      The logger for saving the profiling data
-        * @param ConfigCache     $configCache The configuration cache
+        * @param IConfigCache     $configCache The configuration cache
         *
         * @return Profiler
         */
-       public static function create(LoggerInterface $logger, ConfigCache $configCache)
+       public static function create(IConfigCache $configCache)
        {
-               $enabled = $configCache->get('system', 'profiler', false);
-               $renderTime = $configCache->get('rendertime', 'callstack', false);
-               return new Profiler($logger, $enabled, $renderTime);
+               $enabled = $configCache->get('system', 'profiler');
+               $enabled = isset($enabled) && $enabled !== '!<unset>!';
+               $renderTime = $configCache->get('rendertime', 'callstack');
+               $renderTime = isset($renderTime) && $renderTime !== '!<unset>!';
+
+               return new Profiler($enabled, $renderTime);
        }
 }
index a3f889380eeca5bd15c6347dc7717fcc249f2639..2d3da3a9c0b99f762a29f9b9bb9e7288a87ab337 100644 (file)
@@ -33,20 +33,13 @@ 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->reset();
        }
 
@@ -129,16 +122,17 @@ class Profiler implements ContainerInterface
        /**
         * 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) {
                        return;
                }
                $duration = microtime(true) - $this->get('start');
-               $this->logger->info(
+               $logger->info(
                        $message,
                        [
                                'action' => 'profiling',
@@ -205,7 +199,7 @@ class Profiler implements ContainerInterface
                                }
                        }
                }
-               $this->logger->info($message . ": " . $o, ['action' => 'profiling']);
+               $logger->info($message . ": " . $o, ['action' => 'profiling']);
        }
 
        /**
index 59096f5e5b1aea44c447cee28bef41eb5c117b17..289b3fcea54f278db4f5b099817d7dd7109b0f0f 100644 (file)
@@ -39,13 +39,12 @@ class ApiTest extends DatabaseTest
                $basedir = BasePath::create(dirname(__DIR__) . '/../');
                $configLoader = new Cache\ConfigCacheLoader($basedir);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
-               Factory\DBFactory::init($configCache, $_SERVER);
+               $profiler = Factory\ProfilerFactory::create($configCache);
+               Factory\DBFactory::init($configCache, $profiler, $_SERVER);
                $config = Factory\ConfigFactory::createConfig($configCache);
                Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config);
-               $profiler = Factory\ProfilerFactory::create($logger, $config);
                $this->app = new App($config, $logger, $profiler, false);
-               $this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
 
                parent::setUp();
 
index a4a715575c13c761fbc0b5dc3fce4285413018c7..e8b9c68b19c3aff0f40e9457c923931ab4bf4e4a 100644 (file)
@@ -16,13 +16,12 @@ class DBATest extends DatabaseTest
                $basedir = BasePath::create(dirname(__DIR__) . '/../../');
                $configLoader = new Cache\ConfigCacheLoader($basedir);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
-               Factory\DBFactory::init($configCache, $_SERVER);
+               $profiler = Factory\ProfilerFactory::create($configCache);
+               Factory\DBFactory::init($configCache, $profiler, $_SERVER);
                $config = Factory\ConfigFactory::createConfig($configCache);
-               $pconfig = Factory\ConfigFactory::createPConfig($configCache);
+               Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config);
-               $profiler = Factory\ProfilerFactory::create($logger, $config);
                $this->app = new App($config, $logger, $profiler, false);
-               $this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
 
                parent::setUp();
 
index 6a5e25e76b4ab335724f2db050a626de7cf44630..325ad4e5e7f18571358e7403473b265c0d437129 100644 (file)
@@ -16,13 +16,12 @@ class DBStructureTest extends DatabaseTest
                $basedir = BasePath::create(dirname(__DIR__) . '/../../');
                $configLoader = new Cache\ConfigCacheLoader($basedir);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
-               Factory\DBFactory::init($configCache, $_SERVER);
+               $profiler = Factory\ProfilerFactory::create($configCache);
+               Factory\DBFactory::init($configCache, $profiler, $_SERVER);
                $config = Factory\ConfigFactory::createConfig($configCache);
-               $pconfig = Factory\ConfigFactory::createPConfig($configCache);
+               Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config);
-               $profiler = Factory\ProfilerFactory::create($logger, $config);
                $this->app = new App($config, $logger, $profiler, false);
-               $this->logOutput = FActory\LoggerFactory::enableTest($this->app->getLogger());
 
                parent::setUp();
        }
index f9febeae8dc915de30c674b1a8bc11912e460955..f242fd43c5ebc6354dc011fd207f0f941638174a 100644 (file)
@@ -26,7 +26,7 @@ class ProfilerTest extends MockedTest
         */
        public function testSetUp()
        {
-               $profiler = new Profiler($this->logger, true, true);
+               $profiler = new Profiler(true, true);
        }
 
        /**
@@ -96,7 +96,7 @@ class ProfilerTest extends MockedTest
         */
        public function testSaveTimestamp($timestamp, $name, array $functions)
        {
-               $profiler = new Profiler($this->logger, true, true);
+               $profiler = new Profiler(true, true);
 
                foreach ($functions as $function) {
                        $profiler->saveTimestamp($timestamp, $name, $function);
@@ -111,7 +111,7 @@ class ProfilerTest extends MockedTest
         */
        public function testReset($timestamp, $name, array $functions)
        {
-               $profiler = new Profiler($this->logger, true, true);
+               $profiler = new Profiler(true, true);
 
                $profiler->saveTimestamp($timestamp, $name);
                $profiler->reset();
@@ -168,7 +168,7 @@ class ProfilerTest extends MockedTest
                        ->shouldReceive('info')
                        ->once();
 
-               $profiler = new Profiler($this->logger, true, true);
+               $profiler = new Profiler(true, true);
 
                foreach ($data as $perf => $items) {
                        foreach ($items['functions'] as $function) {
@@ -176,6 +176,6 @@ class ProfilerTest extends MockedTest
                        }
                }
 
-               $profiler->saveLog('test');
+               $profiler->saveLog($this->logger, 'test');
        }
 }