[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/maximebf/debugbar/src/DebugBar/DataCollector/ -> TimeDataCollector.php (source)

   1  <?php
   2  /*
   3   * This file is part of the DebugBar package.
   4   *
   5   * (c) 2013 Maxime Bouroumeau-Fuseau
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  namespace DebugBar\DataCollector;
  12  
  13  use DebugBar\DebugBarException;
  14  
  15  /**
  16   * Collects info about the request duration as well as providing
  17   * a way to log duration of any operations
  18   */
  19  class TimeDataCollector extends DataCollector implements Renderable
  20  {
  21      /**
  22       * @var float
  23       */
  24      protected $requestStartTime;
  25  
  26      /**
  27       * @var float
  28       */
  29      protected $requestEndTime;
  30  
  31      /**
  32       * @var array
  33       */
  34      protected $startedMeasures = array();
  35  
  36      /**
  37       * @var array
  38       */
  39      protected $measures = array();
  40  
  41      /**
  42       * @param float $requestStartTime
  43       */
  44      public function __construct($requestStartTime = null)
  45      {
  46          if ($requestStartTime === null) {
  47              if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
  48                  $requestStartTime = $_SERVER['REQUEST_TIME_FLOAT'];
  49              } else {
  50                  $requestStartTime = microtime(true);
  51              }
  52          }
  53          $this->requestStartTime = (float)$requestStartTime;
  54      }
  55  
  56      /**
  57       * Starts a measure
  58       *
  59       * @param string $name Internal name, used to stop the measure
  60       * @param string|null $label Public name
  61       * @param string|null $collector The source of the collector
  62       */
  63      public function startMeasure($name, $label = null, $collector = null)
  64      {
  65          $start = microtime(true);
  66          $this->startedMeasures[$name] = array(
  67              'label' => $label ?: $name,
  68              'start' => $start,
  69              'collector' => $collector
  70          );
  71      }
  72  
  73      /**
  74       * Check a measure exists
  75       *
  76       * @param string $name
  77       * @return bool
  78       */
  79      public function hasStartedMeasure($name)
  80      {
  81          return isset($this->startedMeasures[$name]);
  82      }
  83  
  84      /**
  85       * Stops a measure
  86       *
  87       * @param string $name
  88       * @param array $params
  89       * @throws DebugBarException
  90       */
  91      public function stopMeasure($name, $params = array())
  92      {
  93          $end = microtime(true);
  94          if (!$this->hasStartedMeasure($name)) {
  95              throw new DebugBarException("Failed stopping measure '$name' because it hasn't been started");
  96          }
  97          $this->addMeasure(
  98              $this->startedMeasures[$name]['label'],
  99              $this->startedMeasures[$name]['start'],
 100              $end,
 101              $params,
 102              $this->startedMeasures[$name]['collector']
 103          );
 104          unset($this->startedMeasures[$name]);
 105      }
 106  
 107      /**
 108       * Adds a measure
 109       *
 110       * @param string $label
 111       * @param float $start
 112       * @param float $end
 113       * @param array $params
 114       * @param string|null $collector
 115       */
 116      public function addMeasure($label, $start, $end, $params = array(), $collector = null)
 117      {
 118          $this->measures[] = array(
 119              'label' => $label,
 120              'start' => $start,
 121              'relative_start' => $start - $this->requestStartTime,
 122              'end' => $end,
 123              'relative_end' => $end - $this->requestEndTime,
 124              'duration' => $end - $start,
 125              'duration_str' => $this->getDataFormatter()->formatDuration($end - $start),
 126              'params' => $params,
 127              'collector' => $collector
 128          );
 129      }
 130  
 131      /**
 132       * Utility function to measure the execution of a Closure
 133       *
 134       * @param string $label
 135       * @param \Closure $closure
 136       * @param string|null $collector
 137       * @return mixed
 138       */
 139      public function measure($label, \Closure $closure, $collector = null)
 140      {
 141          $name = spl_object_hash($closure);
 142          $this->startMeasure($name, $label, $collector);
 143          $result = $closure();
 144          $params = is_array($result) ? $result : array();
 145          $this->stopMeasure($name, $params);
 146          return $result;
 147      }
 148  
 149      /**
 150       * Returns an array of all measures
 151       *
 152       * @return array
 153       */
 154      public function getMeasures()
 155      {
 156          return $this->measures;
 157      }
 158  
 159      /**
 160       * Returns the request start time
 161       *
 162       * @return float
 163       */
 164      public function getRequestStartTime()
 165      {
 166          return $this->requestStartTime;
 167      }
 168  
 169      /**
 170       * Returns the request end time
 171       *
 172       * @return float
 173       */
 174      public function getRequestEndTime()
 175      {
 176          return $this->requestEndTime;
 177      }
 178  
 179      /**
 180       * Returns the duration of a request
 181       *
 182       * @return float
 183       */
 184      public function getRequestDuration()
 185      {
 186          if ($this->requestEndTime !== null) {
 187              return $this->requestEndTime - $this->requestStartTime;
 188          }
 189          return microtime(true) - $this->requestStartTime;
 190      }
 191  
 192      /**
 193       * @return array
 194       * @throws DebugBarException
 195       */
 196      public function collect()
 197      {
 198          $this->requestEndTime = microtime(true);
 199          foreach (array_keys($this->startedMeasures) as $name) {
 200              $this->stopMeasure($name);
 201          }
 202  
 203          usort($this->measures, function($a, $b) {
 204              if ($a['start'] == $b['start']) {
 205                  return 0;
 206              }
 207              return $a['start'] < $b['start'] ? -1 : 1;
 208          });
 209  
 210          return array(
 211              'start' => $this->requestStartTime,
 212              'end' => $this->requestEndTime,
 213              'duration' => $this->getRequestDuration(),
 214              'duration_str' => $this->getDataFormatter()->formatDuration($this->getRequestDuration()),
 215              'measures' => array_values($this->measures)
 216          );
 217      }
 218  
 219      /**
 220       * @return string
 221       */
 222      public function getName()
 223      {
 224          return 'time';
 225      }
 226  
 227      /**
 228       * @return array
 229       */
 230      public function getWidgets()
 231      {
 232          return array(
 233              "time" => array(
 234                  "icon" => "clock-o",
 235                  "tooltip" => "Request Duration",
 236                  "map" => "time.duration_str",
 237                  "default" => "'0ms'"
 238              ),
 239              "timeline" => array(
 240                  "icon" => "tasks",
 241                  "widget" => "PhpDebugBar.Widgets.TimelineWidget",
 242                  "map" => "time",
 243                  "default" => "{}"
 244              )
 245          );
 246      }
 247  }


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