|
15 | 15 | use Psr\Log\LoggerInterface; |
16 | 16 |
|
17 | 17 | class CapabilitiesManager { |
| 18 | + /** |
| 19 | + * Anything above 0.1s to load the capabilities of an app qualifies for bad code |
| 20 | + * and should be cached within the app. |
| 21 | + */ |
| 22 | + public const ACCEPTABLE_LOADING_TIME = 0.1; |
| 23 | + |
18 | 24 | /** @var \Closure[] */ |
19 | 25 | private $capabilities = []; |
20 | 26 |
|
@@ -51,7 +57,27 @@ public function getCapabilities(bool $public = false, bool $initialState = false |
51 | 57 | // that we would otherwise inject to every page load |
52 | 58 | continue; |
53 | 59 | } |
| 60 | + $startTime = microtime(true); |
54 | 61 | $capabilities = array_replace_recursive($capabilities, $c->getCapabilities()); |
| 62 | + $endTime = microtime(true); |
| 63 | + $timeSpent = $endTime - $startTime; |
| 64 | + if ($timeSpent > self::ACCEPTABLE_LOADING_TIME) { |
| 65 | + $logLevel = match (true) { |
| 66 | + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 16 => \OCP\ILogger::FATAL, |
| 67 | + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 8 => \OCP\ILogger::ERROR, |
| 68 | + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 4 => \OCP\ILogger::WARN, |
| 69 | + $timeSpent > self::ACCEPTABLE_LOADING_TIME * 2 => \OCP\ILogger::INFO, |
| 70 | + default => \OCP\ILogger::DEBUG, |
| 71 | + }; |
| 72 | + $this->logger->log( |
| 73 | + $logLevel, |
| 74 | + 'Capabilities of {className} took {duration} seconds to generate.', |
| 75 | + [ |
| 76 | + 'className' => get_class($c), |
| 77 | + 'duration' => round($timeSpent, 2), |
| 78 | + ] |
| 79 | + ); |
| 80 | + } |
55 | 81 | } |
56 | 82 | } else { |
57 | 83 | throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface'); |
|
0 commit comments