Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
profiler.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Profiler
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Utility class to assist in the process of benchmarking the execution
14  * of sections of code to understand where time is being spent.
15  *
16  * @package Joomla.Platform
17  * @subpackage Profiler
18  * @since 11.1
19  */
20 class JProfiler
21 {
22  /**
23  * @var integer The start time.
24  * @since 12.1
25  */
26  protected $start = 0;
27 
28  /**
29  * @var string The prefix to use in the output
30  * @since 12.1
31  */
32  protected $prefix = '';
33 
34  /**
35  * @var array The buffer of profiling messages.
36  * @since 12.1
37  */
38  protected $buffer = null;
39 
40  /**
41  * @var array The profiling messages.
42  * @since 12.1
43  */
44  protected $marks = null;
45 
46  /**
47  * @var float The previous time marker
48  * @since 12.1
49  */
50  protected $previousTime = 0.0;
51 
52  /**
53  * @var float The previous memory marker
54  * @since 12.1
55  */
56  protected $previousMem = 0.0;
57 
58  /**
59  * @var array JProfiler instances container.
60  * @since 11.3
61  */
62  protected static $instances = array();
63 
64  /**
65  * Constructor
66  *
67  * @param string $prefix Prefix for mark messages
68  *
69  * @since 11.1
70  */
71  public function __construct($prefix = '')
72  {
73  $this->start = microtime(1);
74  $this->prefix = $prefix;
75  $this->marks = array();
76  $this->buffer = array();
77  }
78 
79  /**
80  * Returns the global Profiler object, only creating it
81  * if it doesn't already exist.
82  *
83  * @param string $prefix Prefix used to distinguish profiler objects.
84  *
85  * @return JProfiler The Profiler object.
86  *
87  * @since 11.1
88  */
89  public static function getInstance($prefix = '')
90  {
91  if (empty(self::$instances[$prefix]))
92  {
93  self::$instances[$prefix] = new JProfiler($prefix);
94  }
95 
96  return self::$instances[$prefix];
97  }
98 
99  /**
100  * Output a time mark
101  *
102  * The mark is returned as text enclosed in <div> tags
103  * with a CSS class of 'profiler'.
104  *
105  * @param string $label A label for the time mark
106  *
107  * @return string Mark enclosed in <div> tags
108  *
109  * @since 11.1
110  */
111  public function mark($label)
112  {
113  $current = microtime(1) - $this->start;
114  $currentMem = memory_get_usage() / 1048576;
115 
116  $m = (object) array(
117  'prefix' => $this->prefix,
118  'time' => ($current > $this->previousTime ? '+' : '-') . (($current - $this->previousTime) * 1000),
119  'totalTime' => ($current * 1000),
120  'memory' => ($currentMem > $this->previousMem ? '+' : '-') . ($currentMem - $this->previousMem),
121  'totalMemory' => $currentMem,
122  'label' => $label
123  );
124  $this->marks[] = $m;
125 
126  $mark = sprintf(
127  '%s %.3f seconds (%.3f); %0.2f MB (%0.3f) - %s',
128  $m->prefix,
129  $m->totalTime / 1000,
130  $m->time / 1000,
131  $m->totalMemory,
132  $m->memory,
133  $m->label
134  );
135  $this->buffer[] = $mark;
136 
137  $this->previousTime = $current;
138  $this->previousMem = $currentMem;
139 
140  return $mark;
141  }
142 
143  /**
144  * Get the current time.
145  *
146  * @return float The current time
147  *
148  * @since 11.1
149  * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use PHP's microtime(1)
150  */
151  public static function getmicrotime()
152  {
153  list ($usec, $sec) = explode(' ', microtime());
154 
155  return ((float) $usec + (float) $sec);
156  }
157 
158  /**
159  * Get information about current memory usage.
160  *
161  * @return integer The memory usage
162  *
163  * @link PHP_MANUAL#memory_get_usage
164  * @since 11.1
165  * @deprecated 12.3 (Platform) & 4.0 (CMS) - Use PHP's native memory_get_usage()
166  */
167  public function getMemory()
168  {
169  return memory_get_usage();
170  }
171 
172  /**
173  * Get all profiler marks.
174  *
175  * Returns an array of all marks created since the Profiler object
176  * was instantiated. Marks are objects as per {@link JProfiler::mark()}.
177  *
178  * @return array Array of profiler marks
179  *
180  * @since 11.1
181  */
182  public function getMarks()
183  {
184  return $this->marks;
185  }
186 
187  /**
188  * Get all profiler mark buffers.
189  *
190  * Returns an array of all mark buffers created since the Profiler object
191  * was instantiated. Marks are strings as per {@link JProfiler::mark()}.
192  *
193  * @return array Array of profiler marks
194  *
195  * @since 11.1
196  */
197  public function getBuffer()
198  {
199  return $this->buffer;
200  }
201 }