Skip to content

Commit ddb9ef8

Browse files
authored
Merge pull request #46004 from nextcloud/perf/noid/log-slow-capabilities
perf(capabilities): Log capabilities providers that are slow
2 parents 6d8a7a1 + 3aeb785 commit ddb9ef8

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

lib/private/CapabilitiesManager.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
use Psr\Log\LoggerInterface;
1616

1717
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+
1824
/** @var \Closure[] */
1925
private $capabilities = [];
2026

@@ -51,7 +57,27 @@ public function getCapabilities(bool $public = false, bool $initialState = false
5157
// that we would otherwise inject to every page load
5258
continue;
5359
}
60+
$startTime = microtime(true);
5461
$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+
}
5581
}
5682
} else {
5783
throw new \InvalidArgumentException('The given Capability (' . get_class($c) . ') does not implement the ICapability interface');

0 commit comments

Comments
 (0)