[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/debug/src/DataCollector/ -> ProfileCollector.php (source)

   1  <?php
   2  
   3  /**
   4   * This file is part of the DebugBar package.
   5   *
   6   * @copyright  (c) 2013 Maxime Bouroumeau-Fuseau
   7   * @license    For the full copyright and license information, please view the LICENSE
   8   *             file that was distributed with this source code.
   9   */
  10  
  11  namespace Joomla\Plugin\System\Debug\DataCollector;
  12  
  13  use DebugBar\DebugBarException;
  14  use Joomla\CMS\Profiler\Profiler;
  15  use Joomla\Plugin\System\Debug\AbstractDataCollector;
  16  use Joomla\Registry\Registry;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Collects info about the request duration as well as providing
  24   * a way to log duration of any operations
  25   *
  26   * @since  version
  27   */
  28  class ProfileCollector extends AbstractDataCollector
  29  {
  30      /**
  31       * Request start time.
  32       *
  33       * @var   float
  34       * @since 4.0.0
  35       */
  36      protected $requestStartTime;
  37  
  38      /**
  39       * Request end time.
  40       *
  41       * @var   float
  42       * @since 4.0.0
  43       */
  44      protected $requestEndTime;
  45  
  46      /**
  47       * Started measures.
  48       *
  49       * @var array
  50       * @since  4.0.0
  51       */
  52      protected $startedMeasures = [];
  53  
  54      /**
  55       * Measures.
  56       *
  57       * @var array
  58       * @since  4.0.0
  59       */
  60      protected $measures = [];
  61  
  62      /**
  63       * Constructor.
  64       *
  65       * @param   Registry  $params  Parameters.
  66       *
  67       * @since 4.0.0
  68       */
  69      public function __construct(Registry $params)
  70      {
  71          if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  72              $this->requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'];
  73          } else {
  74              $this->requestStartTime = microtime(true);
  75          }
  76  
  77          parent::__construct($params);
  78      }
  79  
  80      /**
  81       * Starts a measure.
  82       *
  83       * @param   string       $name       Internal name, used to stop the measure
  84       * @param   string|null  $label      Public name
  85       * @param   string|null  $collector  The source of the collector
  86       *
  87       * @since  4.0.0
  88       * @return void
  89       */
  90      public function startMeasure($name, $label = null, $collector = null)
  91      {
  92          $start = microtime(true);
  93  
  94          $this->startedMeasures[$name] = [
  95              'label'     => $label ?: $name,
  96              'start'     => $start,
  97              'collector' => $collector,
  98          ];
  99      }
 100  
 101      /**
 102       * Check a measure exists
 103       *
 104       * @param   string  $name  Group name.
 105       *
 106       * @since  4.0.0
 107       * @return bool
 108       */
 109      public function hasStartedMeasure($name): bool
 110      {
 111          return isset($this->startedMeasures[$name]);
 112      }
 113  
 114      /**
 115       * Stops a measure.
 116       *
 117       * @param   string  $name    Measurement name.
 118       * @param   array   $params  Parameters
 119       *
 120       * @since  4.0.0
 121       * @throws DebugBarException
 122       * @return void
 123       */
 124      public function stopMeasure($name, array $params = [])
 125      {
 126          $end = microtime(true);
 127  
 128          if (!$this->hasStartedMeasure($name)) {
 129              throw new DebugBarException("Failed stopping measure '$name' because it hasn't been started");
 130          }
 131  
 132          $this->addMeasure(
 133              $this->startedMeasures[$name]['label'],
 134              $this->startedMeasures[$name]['start'],
 135              $end,
 136              $params,
 137              $this->startedMeasures[$name]['collector']
 138          );
 139  
 140          unset($this->startedMeasures[$name]);
 141      }
 142  
 143      /**
 144       * Adds a measure
 145       *
 146       * @param   string       $label      A label.
 147       * @param   float        $start      Start of request.
 148       * @param   float        $end        End of request.
 149       * @param   array        $params     Parameters.
 150       * @param   string|null  $collector  A collector.
 151       *
 152       * @since  4.0.0
 153       * @return void
 154       */
 155      public function addMeasure($label, $start, $end, array $params = [], $collector = null)
 156      {
 157          $this->measures[] = [
 158              'label'          => $label,
 159              'start'          => $start,
 160              'relative_start' => $start - $this->requestStartTime,
 161              'end'            => $end,
 162              'relative_end'   => $end - $this->requestEndTime,
 163              'duration'       => $end - $start,
 164              'duration_str'   => $this->getDataFormatter()->formatDuration($end - $start),
 165              'params'         => $params,
 166              'collector'      => $collector,
 167          ];
 168      }
 169  
 170      /**
 171       * Utility function to measure the execution of a Closure
 172       *
 173       * @param   string       $label      A label.
 174       * @param   \Closure     $closure    A closure.
 175       * @param   string|null  $collector  A collector.
 176       *
 177       * @since  4.0.0
 178       * @return void
 179       */
 180      public function measure($label, \Closure $closure, $collector = null)
 181      {
 182          $name = spl_object_hash($closure);
 183          $this->startMeasure($name, $label, $collector);
 184          $result = $closure();
 185          $params = \is_array($result) ? $result : [];
 186          $this->stopMeasure($name, $params);
 187      }
 188  
 189      /**
 190       * Returns an array of all measures
 191       *
 192       * @since  4.0.0
 193       * @return array
 194       */
 195      public function getMeasures(): array
 196      {
 197          return $this->measures;
 198      }
 199  
 200      /**
 201       * Returns the request start time
 202       *
 203       * @since  4.0.0
 204       * @return float
 205       */
 206      public function getRequestStartTime(): float
 207      {
 208          return $this->requestStartTime;
 209      }
 210  
 211      /**
 212       * Returns the request end time
 213       *
 214       * @since  4.0.0
 215       * @return float
 216       */
 217      public function getRequestEndTime(): float
 218      {
 219          return $this->requestEndTime;
 220      }
 221  
 222      /**
 223       * Returns the duration of a request
 224       *
 225       * @since  4.0.0
 226       * @return float
 227       */
 228      public function getRequestDuration(): float
 229      {
 230          if ($this->requestEndTime !== null) {
 231              return $this->requestEndTime - $this->requestStartTime;
 232          }
 233  
 234          return microtime(true) - $this->requestStartTime;
 235      }
 236  
 237      /**
 238       * Called by the DebugBar when data needs to be collected
 239       *
 240       * @since  4.0.0
 241       * @return array Collected data
 242       */
 243      public function collect(): array
 244      {
 245          $this->requestEndTime = microtime(true);
 246  
 247          $start = $this->requestStartTime;
 248  
 249          $marks = Profiler::getInstance('Application')->getMarks();
 250  
 251          foreach ($marks as $mark) {
 252              $mem = $this->getDataFormatter()->formatBytes(abs($mark->memory) * 1048576);
 253              $label = $mark->label . " ($mem)";
 254              $end = $start + $mark->time / 1000;
 255              $this->addMeasure($label, $start, $end);
 256              $start = $end;
 257          }
 258  
 259          foreach (array_keys($this->startedMeasures) as $name) {
 260              $this->stopMeasure($name);
 261          }
 262  
 263          usort(
 264              $this->measures,
 265              function ($a, $b) {
 266                  if ($a['start'] === $b['start']) {
 267                      return 0;
 268                  }
 269  
 270                  return $a['start'] < $b['start'] ? -1 : 1;
 271              }
 272          );
 273  
 274          return [
 275              'start'        => $this->requestStartTime,
 276              'end'          => $this->requestEndTime,
 277              'duration'     => $this->getRequestDuration(),
 278              'duration_str' => $this->getDataFormatter()->formatDuration($this->getRequestDuration()),
 279              'measures'     => array_values($this->measures),
 280              'rawMarks'     => $marks,
 281          ];
 282      }
 283  
 284      /**
 285       * Returns the unique name of the collector
 286       *
 287       * @since  4.0.0
 288       * @return string
 289       */
 290      public function getName(): string
 291      {
 292          return 'profile';
 293      }
 294  
 295      /**
 296       * Returns a hash where keys are control names and their values
 297       * an array of options as defined in {@see \DebugBar\JavascriptRenderer::addControl()}
 298       *
 299       * @since  4.0.0
 300       * @return array
 301       */
 302      public function getWidgets(): array
 303      {
 304          return [
 305              'profileTime' => [
 306                  'icon'    => 'clock-o',
 307                  'tooltip' => 'Request Duration',
 308                  'map'     => 'profile.duration_str',
 309                  'default' => "'0ms'",
 310              ],
 311              'profile'     => [
 312                  'icon'    => 'clock-o',
 313                  'widget'  => 'PhpDebugBar.Widgets.TimelineWidget',
 314                  'map'     => 'profile',
 315                  'default' => '{}',
 316              ],
 317          ];
 318      }
 319  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer