Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Profiler.php
Go to the documentation of this file.
1 <?php
32 {
33 
37  const CONNECT = 1;
38 
42  const QUERY = 2;
43 
47  const INSERT = 4;
48 
53  const UPDATE = 8;
54 
59  const DELETE = 16;
60 
64  const SELECT = 32;
65 
69  const TRANSACTION = 64;
70 
74  const STORED = 'stored';
75 
79  const IGNORED = 'ignored';
80 
86  protected $_queryProfiles = array();
87 
94  protected $_enabled = false;
95 
104  protected $_filterElapsedSecs = null;
105 
115  protected $_filterTypes = null;
116 
124  public function __construct($enabled = false)
125  {
126  $this->setEnabled($enabled);
127  }
128 
136  public function setEnabled($enable)
137  {
138  $this->_enabled = (boolean) $enable;
139 
140  return $this;
141  }
142 
149  public function getEnabled()
150  {
151  return $this->_enabled;
152  }
153 
163  public function setFilterElapsedSecs($minimumSeconds = null)
164  {
165  if (null === $minimumSeconds) {
166  $this->_filterElapsedSecs = null;
167  } else {
168  $this->_filterElapsedSecs = (integer) $minimumSeconds;
169  }
170 
171  return $this;
172  }
173 
180  public function getFilterElapsedSecs()
181  {
183  }
184 
194  public function setFilterQueryType($queryTypes = null)
195  {
196  $this->_filterTypes = $queryTypes;
197 
198  return $this;
199  }
200 
208  public function getFilterQueryType()
209  {
210  return $this->_filterTypes;
211  }
212 
220  public function clear()
221  {
222  $this->_queryProfiles = array();
223 
224  return $this;
225  }
226 
234  {
235  $this->_queryProfiles[] = clone $query;
236 
237  end($this->_queryProfiles);
238 
239  return key($this->_queryProfiles);
240  }
241 
253  public function queryStart($queryText, $queryType = null)
254  {
255  if (!$this->_enabled) {
256  return null;
257  }
258 
259  // make sure we have a query type
260  if (null === $queryType) {
261  switch (strtolower(substr(ltrim($queryText), 0, 6))) {
262  case 'insert':
263  $queryType = self::INSERT;
264  break;
265  case 'update':
266  $queryType = self::UPDATE;
267  break;
268  case 'delete':
269  $queryType = self::DELETE;
270  break;
271  case 'select':
272  $queryType = self::SELECT;
273  break;
274  default:
275  $queryType = self::QUERY;
276  break;
277  }
278  }
279 
283  #require_once 'Zend/Db/Profiler/Query.php';
284  $this->_queryProfiles[] = new Zend_Db_Profiler_Query($queryText, $queryType);
285 
286  end($this->_queryProfiles);
287 
288  return key($this->_queryProfiles);
289  }
290 
299  public function queryEnd($queryId)
300  {
301  // Don't do anything if the Zend_Db_Profiler is not enabled.
302  if (!$this->_enabled) {
303  return self::IGNORED;
304  }
305 
306  // Check for a valid query handle.
307  if (!isset($this->_queryProfiles[$queryId])) {
311  #require_once 'Zend/Db/Profiler/Exception.php';
312  throw new Zend_Db_Profiler_Exception("Profiler has no query with handle '$queryId'.");
313  }
314 
315  $qp = $this->_queryProfiles[$queryId];
316 
317  // Ensure that the query profile has not already ended
318  if ($qp->hasEnded()) {
322  #require_once 'Zend/Db/Profiler/Exception.php';
323  throw new Zend_Db_Profiler_Exception("Query with profiler handle '$queryId' has already ended.");
324  }
325 
326  // End the query profile so that the elapsed time can be calculated.
327  $qp->end();
328 
333  if (null !== $this->_filterElapsedSecs && $qp->getElapsedSecs() < $this->_filterElapsedSecs) {
334  unset($this->_queryProfiles[$queryId]);
335  return self::IGNORED;
336  }
337 
342  if (null !== $this->_filterTypes && !($qp->getQueryType() & $this->_filterTypes)) {
343  unset($this->_queryProfiles[$queryId]);
344  return self::IGNORED;
345  }
346 
347  return self::STORED;
348  }
349 
358  public function getQueryProfile($queryId)
359  {
360  if (!array_key_exists($queryId, $this->_queryProfiles)) {
364  #require_once 'Zend/Db/Profiler/Exception.php';
365  throw new Zend_Db_Profiler_Exception("Query handle '$queryId' not found in profiler log.");
366  }
367 
368  return $this->_queryProfiles[$queryId];
369  }
370 
383  public function getQueryProfiles($queryType = null, $showUnfinished = false)
384  {
385  $queryProfiles = array();
386  foreach ($this->_queryProfiles as $key => $qp) {
387  if ($queryType === null) {
388  $condition = true;
389  } else {
390  $condition = ($qp->getQueryType() & $queryType);
391  }
392 
393  if (($qp->hasEnded() || $showUnfinished) && $condition) {
394  $queryProfiles[$key] = $qp;
395  }
396  }
397 
398  if (empty($queryProfiles)) {
399  $queryProfiles = false;
400  }
401 
402  return $queryProfiles;
403  }
404 
414  public function getTotalElapsedSecs($queryType = null)
415  {
416  $elapsedSecs = 0;
417  foreach ($this->_queryProfiles as $key => $qp) {
418  if (null === $queryType) {
419  $condition = true;
420  } else {
421  $condition = ($qp->getQueryType() & $queryType);
422  }
423  if (($qp->hasEnded()) && $condition) {
424  $elapsedSecs += $qp->getElapsedSecs();
425  }
426  }
427  return $elapsedSecs;
428  }
429 
438  public function getTotalNumQueries($queryType = null)
439  {
440  if (null === $queryType) {
441  return count($this->_queryProfiles);
442  }
443 
444  $numQueries = 0;
445  foreach ($this->_queryProfiles as $qp) {
446  if ($qp->hasEnded() && ($qp->getQueryType() & $queryType)) {
447  $numQueries++;
448  }
449  }
450 
451  return $numQueries;
452  }
453 
461  public function getLastQueryProfile()
462  {
463  if (empty($this->_queryProfiles)) {
464  return false;
465  }
466 
467  end($this->_queryProfiles);
468 
469  return current($this->_queryProfiles);
470  }
471 
472 }
473 
setFilterElapsedSecs($minimumSeconds=null)
Definition: Profiler.php:163
setEnabled($enable)
Definition: Profiler.php:136
getQueryProfiles($queryType=null, $showUnfinished=false)
Definition: Profiler.php:383
getTotalNumQueries($queryType=null)
Definition: Profiler.php:438
getTotalElapsedSecs($queryType=null)
Definition: Profiler.php:414
__construct($enabled=false)
Definition: Profiler.php:124
getQueryProfile($queryId)
Definition: Profiler.php:358
queryStart($queryText, $queryType=null)
Definition: Profiler.php:253
const TRANSACTION
Definition: Profiler.php:69
queryClone(Zend_Db_Profiler_Query $query)
Definition: Profiler.php:233
queryEnd($queryId)
Definition: Profiler.php:299
setFilterQueryType($queryTypes=null)
Definition: Profiler.php:194