diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-07-09 20:32:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-09 20:32:55 +0200 |
commit | ddb9ef8287f7ab9be9a017f749a3684eb85d7e51 (patch) | |
tree | daffccb7694fa64d996c3f6f1a8cabdf916dafe1 | |
parent | 6d8a7a147c2d9cab3771f7dd592cbdbf1680408a (diff) | |
parent | 3aeb78539988299567835003a0d4cb7df630654d (diff) | |
download | nextcloud-server-ddb9ef8287f7ab9be9a017f749a3684eb85d7e51.tar.gz nextcloud-server-ddb9ef8287f7ab9be9a017f749a3684eb85d7e51.zip |
Merge pull request #46004 from nextcloud/perf/noid/log-slow-capabilities
perf(capabilities): Log capabilities providers that are slow
-rw-r--r-- | lib/private/CapabilitiesManager.php | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/private/CapabilitiesManager.php b/lib/private/CapabilitiesManager.php index bb84ebb65e8..d7bf25f078a 100644 --- a/lib/private/CapabilitiesManager.php +++ b/lib/private/CapabilitiesManager.php @@ -15,6 +15,12 @@ use OCP\Capabilities\IPublicCapability; use Psr\Log\LoggerInterface; class CapabilitiesManager { + /** + * Anything above 0.1s to load the capabilities of an app qualifies for bad code + * and should be cached within the app. + */ + public const ACCEPTABLE_LOADING_TIME = 0.1; + /** @var \Closure[] */ private $capabilities = []; @@ -51,7 +57,27 @@ class CapabilitiesManager { // that we would otherwise inject to every page load continue; } + $startTime = microtime(true); $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); + $endTime = microtime(true); + $timeSpent = $endTime - $startTime; + if ($timeSpent > self::ACCEPTABLE_LOADING_TIME) { + $logLevel = match (true) { + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 16 => \OCP\ILogger::FATAL, + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 8 => \OCP\ILogger::ERROR, + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 4 => \OCP\ILogger::WARN, + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 2 => \OCP\ILogger::INFO, + default => \OCP\ILogger::DEBUG, + }; + $this->logger->log( + $logLevel, + 'Capabilities of {className} took {duration} seconds to generate.', + [ + 'className' => get_class($c), + 'duration' => round($timeSpent, 2), + ] + ); + } } } else { throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |