X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FProfiler.php;h=7e04ee563f6afe3744b6ae9116baad09f186b8e1;hb=39c2282c1292af173f854e2a4338c601f9ba58f6;hp=3e1a900ad56ab65dd3632f44f12c61428957b6a9;hpb=befc2af5043a3afde251721c0d27df695db1bb7e;p=friendica.git diff --git a/src/Util/Profiler.php b/src/Util/Profiler.php index 3e1a900ad5..7e04ee563f 100644 --- a/src/Util/Profiler.php +++ b/src/Util/Profiler.php @@ -21,8 +21,8 @@ namespace Friendica\Util; -use Friendica\Core\Config\Cache; -use Friendica\Core\Config\IConfig; +use Friendica\Core\Config\ValueObject\Cache; +use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\System; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; @@ -54,6 +54,8 @@ class Profiler implements ContainerInterface */ private $rendertime; + private $timestamps = []; + /** * True, if the Profiler should measure the whole rendertime including functions * @@ -67,16 +69,16 @@ class Profiler implements ContainerInterface /** * Updates the enabling of the current profiler * - * @param IConfig $config + * @param IManageConfigValues $config */ - public function update(IConfig $config) + public function update(IManageConfigValues $config) { $this->enabled = $config->get('system', 'profiler'); $this->rendertime = $config->get('rendertime', 'callstack'); } /** - * @param Cache $configCache The configuration cache + * @param \Friendica\Core\Config\ValueObject\Cache $configCache The configuration cache */ public function __construct(Cache $configCache) { @@ -85,6 +87,59 @@ class Profiler implements ContainerInterface $this->reset(); } + /** + * Start a profiler recording + * + * @param string $value + * @return void + */ + public function startRecording(string $value) + { + if (!$this->enabled) { + return; + } + + $this->timestamps[] = ['value' => $value, 'stamp' => microtime(true), 'credit' => 0]; + } + + /** + * Stop a profiler recording + * + * @param string $callstack + * @return void + */ + public function stopRecording(string $callstack = '') + { + if (!$this->enabled || empty($this->timestamps)) { + return; + } + + $timestamp = array_pop($this->timestamps); + + $duration = floatval(microtime(true) - $timestamp['stamp'] - $timestamp['credit']); + $value = $timestamp['value']; + + foreach ($this->timestamps as $key => $stamp) { + $this->timestamps[$key]['credit'] += $duration; + } + + $callstack = $callstack ?: System::callstack(4, $value == 'rendering' ? 0 : 1); + + if (!isset($this->performance[$value])) { + $this->performance[$value] = 0; + } + + $this->performance[$value] += (float) $duration; + $this->performance['marktime'] += (float) $duration; + + if (!isset($this->callstack[$value][$callstack])) { + // Prevent ugly E_NOTICE + $this->callstack[$value][$callstack] = 0; + } + + $this->callstack[$value][$callstack] += (float) $duration; + } + /** * Saves a timestamp for a value - f.e. a call * Necessary for profiling Friendica @@ -143,7 +198,7 @@ class Profiler implements ContainerInterface $this->performance['network'] = 0; $this->performance['file'] = 0; $this->performance['rendering'] = 0; - $this->performance['parser'] = 0; + $this->performance['session'] = 0; $this->performance['marktime'] = 0; $this->performance['marktime'] = microtime(true); $this->performance['classcreate'] = 0; @@ -165,7 +220,7 @@ class Profiler implements ContainerInterface $this->callstack['network'] = []; $this->callstack['file'] = []; $this->callstack['rendering'] = []; - $this->callstack['parser'] = []; + $this->callstack['session'] = []; } /** @@ -228,6 +283,16 @@ class Profiler implements ContainerInterface } } + if (isset($this->callstack["rendering"])) { + $output .= "\nRendering:\n"; + foreach ($this->callstack["rendering"] as $func => $time) { + $time = round($time, 3); + if ($time > $limit) { + $output .= $func . ": " . $time . "\n"; + } + } + } + return $output; }