Skip to content

Commit b68be79

Browse files
committed
more routing performance instrumentation
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 7aa7868 commit b68be79

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/private/AppFramework/App.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
use OC\AppFramework\DependencyInjection\DIContainer;
3535
use OC\AppFramework\Http\Dispatcher;
3636
use OC\AppFramework\Http\Request;
37-
use OC\Diagnostics\EventLogger;
3837
use OCP\Profiler\IProfiler;
3938
use OC\Profiler\RoutingDataCollector;
4039
use OCP\AppFramework\QueryException;
@@ -43,7 +42,6 @@
4342
use OCP\AppFramework\Http\IOutput;
4443
use OCP\Diagnostics\IEventLogger;
4544
use OCP\HintException;
46-
use OCP\IConfig;
4745
use OCP\IRequest;
4846

4947
/**
@@ -120,14 +118,16 @@ public static function getAppIdForClass(string $className, string $topNamespace
120118
public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) {
121119
/** @var IProfiler $profiler */
122120
$profiler = $container->get(IProfiler::class);
123-
$config = $container->get(IConfig::class);
121+
$eventLogger = $container->get(IEventLogger::class);
124122
// Disable profiler on the profiler UI
125123
$profiler->setEnabled($profiler->isEnabled() && !is_null($urlParams) && isset($urlParams['_route']) && !str_starts_with($urlParams['_route'], 'profiler.'));
126124
if ($profiler->isEnabled()) {
127125
\OC::$server->get(IEventLogger::class)->activate();
128126
$profiler->add(new RoutingDataCollector($container['AppName'], $controllerName, $methodName));
129127
}
130128

129+
$eventLogger->start('app:controller:params', 'Gather controller parameters');
130+
131131
if (!is_null($urlParams)) {
132132
/** @var Request $request */
133133
$request = $container->get(IRequest::class);
@@ -139,6 +139,10 @@ public static function main(string $controllerName, string $methodName, DIContai
139139
}
140140
$appName = $container['AppName'];
141141

142+
$eventLogger->end('app:controller:params');
143+
144+
$eventLogger->start('app:controller:load', 'Load app controller');
145+
142146
// first try $controllerName then go for \OCA\AppName\Controller\$controllerName
143147
try {
144148
$controller = $container->get($controllerName);
@@ -158,10 +162,18 @@ public static function main(string $controllerName, string $methodName, DIContai
158162
$controller = $container->query($controllerName);
159163
}
160164

165+
$eventLogger->end('app:controller:load');
166+
167+
$eventLogger->start('app:controller:dispatcher', 'Initialize dispatcher and pre-middleware');
168+
161169
// initialize the dispatcher and run all the middleware before the controller
162170
/** @var Dispatcher $dispatcher */
163171
$dispatcher = $container['Dispatcher'];
164172

173+
$eventLogger->end('app:controller:dispatcher');
174+
175+
$eventLogger->start('app:controller:run', 'Run app controller');
176+
165177
[
166178
$httpHeaders,
167179
$responseHeaders,
@@ -170,11 +182,11 @@ public static function main(string $controllerName, string $methodName, DIContai
170182
$response
171183
] = $dispatcher->dispatch($controller, $methodName);
172184

185+
$eventLogger->end('app:controller:run');
186+
173187
$io = $container[IOutput::class];
174188

175189
if ($profiler->isEnabled()) {
176-
/** @var EventLogger $eventLogger */
177-
$eventLogger = $container->get(IEventLogger::class);
178190
$eventLogger->end('runtime');
179191
$profile = $profiler->collect($container->get(IRequest::class), $response);
180192
$profiler->saveProfile($profile);

lib/private/Route/Router.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
use OC\AppFramework\Routing\RouteParser;
3636
use OCP\AppFramework\App;
37+
use OCP\Diagnostics\IEventLogger;
3738
use OCP\Route\IRouter;
3839
use OCP\Util;
3940
use Psr\Log\LoggerInterface;
@@ -64,6 +65,7 @@ class Router implements IRouter {
6465
protected LoggerInterface $logger;
6566
/** @var RequestContext */
6667
protected $context;
68+
private IEventLogger $eventLogger;
6769

6870
public function __construct(LoggerInterface $logger) {
6971
$this->logger = $logger;
@@ -82,6 +84,7 @@ public function __construct(LoggerInterface $logger) {
8284
$this->context = new RequestContext($baseUrl, $method, $host, $schema);
8385
// TODO cache
8486
$this->root = $this->getCollection('root');
87+
$this->eventLogger = \OC::$server->get(IEventLogger::class);
8588
}
8689

8790
/**
@@ -134,7 +137,7 @@ public function loadRoutes($app = null) {
134137
$routingFiles = [];
135138
}
136139
}
137-
\OC::$server->getEventLogger()->start('loadroutes' . $requestedApp, 'Loading Routes');
140+
$this->eventLogger->start('route:load:' . $requestedApp, 'Loading Routes for ' . $requestedApp);
138141
foreach ($routingFiles as $app => $file) {
139142
if (!isset($this->loadedApps[$app])) {
140143
if (!\OC_App::isAppLoaded($app)) {
@@ -170,7 +173,7 @@ public function loadRoutes($app = null) {
170173
$collection->addPrefix('/ocs');
171174
$this->root->addCollection($collection);
172175
}
173-
\OC::$server->getEventLogger()->end('loadroutes' . $requestedApp);
176+
$this->eventLogger->end('route:load:' . $requestedApp);
174177
}
175178

176179
/**
@@ -231,6 +234,7 @@ public function create($name,
231234
* @return array
232235
*/
233236
public function findMatchingRoute(string $url): array {
237+
$this->eventLogger->start('route:match', 'Match route');
234238
if (substr($url, 0, 6) === '/apps/') {
235239
// empty string / 'apps' / $app / rest of the route
236240
[, , $app,] = explode('/', $url, 4);
@@ -276,6 +280,7 @@ public function findMatchingRoute(string $url): array {
276280
}
277281
}
278282

283+
$this->eventLogger->end('route:match');
279284
return $parameters;
280285
}
281286

@@ -289,7 +294,7 @@ public function findMatchingRoute(string $url): array {
289294
public function match($url) {
290295
$parameters = $this->findMatchingRoute($url);
291296

292-
\OC::$server->getEventLogger()->start('run_route', 'Run route');
297+
$this->eventLogger->start('route:run', 'Run route');
293298
if (isset($parameters['caller'])) {
294299
$caller = $parameters['caller'];
295300
unset($parameters['caller']);
@@ -303,13 +308,15 @@ public function match($url) {
303308
}
304309
unset($parameters['action']);
305310
unset($parameters['caller']);
311+
$this->eventLogger->start('route:run:call', 'Run callable route');
306312
call_user_func($action, $parameters);
313+
$this->eventLogger->end('route:run:call');
307314
} elseif (isset($parameters['file'])) {
308315
include $parameters['file'];
309316
} else {
310317
throw new \Exception('no action available');
311318
}
312-
\OC::$server->getEventLogger()->end('run_route');
319+
$this->eventLogger->end('route:run');
313320
}
314321

315322
/**

0 commit comments

Comments
 (0)