[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Profiler/ -> Profiler.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Profiler;
  11  
  12  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Utility class to assist in the process of benchmarking the execution
  18   * of sections of code to understand where time is being spent.
  19   *
  20   * @since  1.7.0
  21   */
  22  class Profiler
  23  {
  24      /**
  25       * @var    integer  The start time.
  26       * @since  3.0.0
  27       */
  28      protected $start = 0;
  29  
  30      /**
  31       * @var    string  The prefix to use in the output
  32       * @since  3.0.0
  33       */
  34      protected $prefix = '';
  35  
  36      /**
  37       * @var    array  The buffer of profiling messages.
  38       * @since  3.0.0
  39       */
  40      protected $buffer = null;
  41  
  42      /**
  43       * @var    array  The profiling messages.
  44       * @since  3.0.0
  45       */
  46      protected $marks = null;
  47  
  48      /**
  49       * @var    float  The previous time marker
  50       * @since  3.0.0
  51       */
  52      protected $previousTime = 0.0;
  53  
  54      /**
  55       * @var    float  The previous memory marker
  56       * @since  3.0.0
  57       */
  58      protected $previousMem = 0.0;
  59  
  60      /**
  61       * @var    array  JProfiler instances container.
  62       * @since  1.7.3
  63       */
  64      protected static $instances = array();
  65  
  66      /**
  67       * Constructor
  68       *
  69       * @param   string  $prefix  Prefix for mark messages
  70       *
  71       * @since   1.7.0
  72       */
  73      public function __construct($prefix = '')
  74      {
  75          $this->start = microtime(1);
  76          $this->prefix = $prefix;
  77          $this->marks = array();
  78          $this->buffer = array();
  79      }
  80  
  81      /**
  82       * Returns the global Profiler object, only creating it
  83       * if it doesn't already exist.
  84       *
  85       * @param   string  $prefix  Prefix used to distinguish profiler objects.
  86       *
  87       * @return  Profiler  The Profiler object.
  88       *
  89       * @since   1.7.0
  90       */
  91      public static function getInstance($prefix = '')
  92      {
  93          if (empty(self::$instances[$prefix])) {
  94              self::$instances[$prefix] = new static($prefix);
  95          }
  96  
  97          return self::$instances[$prefix];
  98      }
  99  
 100      /**
 101       * Output a time mark
 102       *
 103       * @param   string  $label  A label for the time mark
 104       *
 105       * @return  string
 106       *
 107       * @since   1.7.0
 108       */
 109      public function mark($label)
 110      {
 111          $current = microtime(1) - $this->start;
 112          $currentMem = memory_get_usage() / 1048576;
 113  
 114          $m = (object) array(
 115              'prefix' => $this->prefix,
 116              'time' => ($current - $this->previousTime) * 1000,
 117              'totalTime' => ($current * 1000),
 118              'memory' => $currentMem - $this->previousMem,
 119              'totalMemory' => $currentMem,
 120              'label' => $label,
 121          );
 122          $this->marks[] = $m;
 123  
 124          $mark = sprintf(
 125              '%s %.3f seconds (%.3f); %0.2f MB (%0.3f) - %s',
 126              $m->prefix,
 127              $m->totalTime / 1000,
 128              $m->time / 1000,
 129              $m->totalMemory,
 130              $m->memory,
 131              $m->label
 132          );
 133          $this->buffer[] = $mark;
 134  
 135          $this->previousTime = $current;
 136          $this->previousMem = $currentMem;
 137  
 138          return $mark;
 139      }
 140  
 141      /**
 142       * Get all profiler marks.
 143       *
 144       * Returns an array of all marks created since the Profiler object
 145       * was instantiated.  Marks are objects as per {@link JProfiler::mark()}.
 146       *
 147       * @return  array  Array of profiler marks
 148       *
 149       * @since   1.7.0
 150       */
 151      public function getMarks()
 152      {
 153          return $this->marks;
 154      }
 155  
 156      /**
 157       * Get all profiler mark buffers.
 158       *
 159       * Returns an array of all mark buffers created since the Profiler object
 160       * was instantiated.  Marks are strings as per {@link Profiler::mark()}.
 161       *
 162       * @return  array  Array of profiler marks
 163       *
 164       * @since   1.7.0
 165       */
 166      public function getBuffer()
 167      {
 168          return $this->buffer;
 169      }
 170  
 171      /**
 172       * Sets the start time.
 173       *
 174       * @param   double  $startTime  Unix timestamp in microseconds for setting the Profiler start time.
 175       * @param   int     $startMem   Memory amount in bytes for setting the Profiler start memory.
 176       *
 177       * @return  $this   For chaining
 178       *
 179       * @since   3.0.0
 180       */
 181      public function setStart($startTime = 0.0, $startMem = 0)
 182      {
 183          $this->start       = (double) $startTime;
 184          $this->previousMem = (int) $startMem / 1048576;
 185  
 186          return $this;
 187      }
 188  }


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