Improved performance profiler

This commit is contained in:
Michael 2020-12-09 22:10:27 +00:00
parent 9c0342b907
commit 5fb83d0632
5 changed files with 33 additions and 7 deletions

View File

@ -21,6 +21,8 @@
use Dice\Dice; use Dice\Dice;
$start_time = microtime(true);
if (!file_exists(__DIR__ . '/vendor/autoload.php')) { if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
die('Vendor path not found. Please execute "bin/composer.phar --no-dev install" on the command line in the web root.'); die('Vendor path not found. Please execute "bin/composer.phar --no-dev install" on the command line in the web root.');
} }
@ -39,5 +41,6 @@ $a->runFrontend(
$dice->create(\Friendica\App\Router::class), $dice->create(\Friendica\App\Router::class),
$dice->create(\Friendica\Core\PConfig\IPConfig::class), $dice->create(\Friendica\Core\PConfig\IPConfig::class),
$dice->create(\Friendica\Security\Authentication::class), $dice->create(\Friendica\Security\Authentication::class),
$dice->create(\Friendica\App\Page::class) $dice->create(\Friendica\App\Page::class),
$start_time
); );

View File

@ -415,8 +415,14 @@ class App
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
* @throws \ImagickException * @throws \ImagickException
*/ */
public function runFrontend(App\Module $module, App\Router $router, IPConfig $pconfig, Authentication $auth, App\Page $page) public function runFrontend(App\Module $module, App\Router $router, IPConfig $pconfig, Authentication $auth, App\Page $page, int $start_time = 0)
{ {
if ($start_time != 0) {
$this->profiler->set($start_time, 'start');
}
$this->profiler->set(microtime(true), 'classinit');
$moduleName = $module->getName(); $moduleName = $module->getName();
try { try {
@ -551,12 +557,12 @@ class App
$module = $module->determineClass($this->args, $router, $this->config); $module = $module->determineClass($this->args, $router, $this->config);
// Let the module run it's internal process (init, get, post, ...) // Let the module run it's internal process (init, get, post, ...)
$module->run($this->l10n, $this->baseURL, $this->logger, $_SERVER, $_POST); $module->run($this->l10n, $this->baseURL, $this->logger, $this->profiler, $_SERVER, $_POST);
} catch (HTTPException $e) { } catch (HTTPException $e) {
ModuleHTTPException::rawContent($e); ModuleHTTPException::rawContent($e);
} }
$page->run($this, $this->baseURL, $this->mode, $module, $this->l10n, $this->config, $pconfig); $page->run($this, $this->baseURL, $this->mode, $module, $this->l10n, $this->profiler, $this->config, $pconfig);
} }
/** /**

View File

@ -30,6 +30,7 @@ use Friendica\Module\HTTPException\MethodNotAllowed;
use Friendica\Module\HTTPException\PageNotFound; use Friendica\Module\HTTPException\PageNotFound;
use Friendica\Network\HTTPException\MethodNotAllowedException; use Friendica\Network\HTTPException\MethodNotAllowedException;
use Friendica\Network\HTTPException\NotFoundException; use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
/** /**
@ -234,7 +235,7 @@ class Module
* *
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public function run(Core\L10n $l10n, App\BaseURL $baseUrl, LoggerInterface $logger, array $server, array $post) public function run(Core\L10n $l10n, App\BaseURL $baseUrl, LoggerInterface $logger, Profiler $profiler, array $server, array $post)
{ {
if ($this->printNotAllowedAddon) { if ($this->printNotAllowedAddon) {
notice($l10n->t("You must be logged in to use addons. ")); notice($l10n->t("You must be logged in to use addons. "));
@ -266,10 +267,15 @@ class Module
$placeholder = ''; $placeholder = '';
$profiler->set(microtime(true), 'ready');
$timestamp = microtime(true);
Core\Hook::callAll($this->module . '_mod_init', $placeholder); Core\Hook::callAll($this->module . '_mod_init', $placeholder);
call_user_func([$this->module_class, 'init'], $this->module_parameters); call_user_func([$this->module_class, 'init'], $this->module_parameters);
$profiler->set(microtime(true) - $timestamp, 'init');
if ($server['REQUEST_METHOD'] === 'POST') { if ($server['REQUEST_METHOD'] === 'POST') {
Core\Hook::callAll($this->module . '_mod_post', $post); Core\Hook::callAll($this->module . '_mod_post', $post);
call_user_func([$this->module_class, 'post'], $this->module_parameters); call_user_func([$this->module_class, 'post'], $this->module_parameters);

View File

@ -36,6 +36,7 @@ use Friendica\Module\Special\HTTPException as ModuleHTTPException;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Strings; use Friendica\Util\Strings;
use Friendica\Util\Profiler;
/** /**
* Contains the page specific environment variables for the current Page * Contains the page specific environment variables for the current Page
@ -375,7 +376,7 @@ class Page implements ArrayAccess
* *
* @throws HTTPException\InternalServerErrorException * @throws HTTPException\InternalServerErrorException
*/ */
public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, IConfig $config, IPConfig $pconfig) public function run(App $app, BaseURL $baseURL, Mode $mode, Module $module, L10n $l10n, Profiler $profiler, IConfig $config, IPConfig $pconfig)
{ {
$moduleName = $module->getName(); $moduleName = $module->getName();
@ -384,7 +385,9 @@ class Page implements ArrayAccess
* *
* Sets the $Page->page['content'] variable * Sets the $Page->page['content'] variable
*/ */
$timestamp = microtime(true);
$this->initContent($module, $mode); $this->initContent($module, $mode);
$profiler->set(microtime(true) - $timestamp, 'content');
// Load current theme info after module has been initialized as theme could have been set in module // Load current theme info after module has been initialized as theme could have been set in module
$currentTheme = $app->getCurrentTheme(); $currentTheme = $app->getCurrentTheme();

View File

@ -134,7 +134,7 @@ class Profiler implements ContainerInterface
public function resetPerformance() public function resetPerformance()
{ {
$this->performance = []; $this->performance = [];
$this->performance['start'] = microtime(true); $this->performance['start'] = $this->performance['ready'] = microtime(true);
$this->performance['database'] = 0; $this->performance['database'] = 0;
$this->performance['database_write'] = 0; $this->performance['database_write'] = 0;
$this->performance['cache'] = 0; $this->performance['cache'] = 0;
@ -145,6 +145,9 @@ class Profiler implements ContainerInterface
$this->performance['parser'] = 0; $this->performance['parser'] = 0;
$this->performance['marktime'] = 0; $this->performance['marktime'] = 0;
$this->performance['marktime'] = microtime(true); $this->performance['marktime'] = microtime(true);
$this->performance['frontend'] = 0;
$this->performance['init'] = 0;
$this->performance['content'] = 0;
} }
/** /**
@ -277,6 +280,11 @@ class Profiler implements ContainerInterface
} }
} }
public function set($timestamp, $id)
{
$this->performance[$id] = $timestamp;
}
/** /**
* Returns true if the container can return an entry for the given identifier. * Returns true if the container can return an entry for the given identifier.
* Returns false otherwise. * Returns false otherwise.