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
9 
11 {
15  const TIMER_PREFIX = 'DB_QUERY';
16 
20  const DEFAULT_CONNECTION_TYPE = 'database';
21 
25  protected $_queryTypes = ['select', 'insert', 'update', 'delete'];
26 
33  protected function _getTimerName($operation)
34  {
35  // default name of connection type
37 
38  // connection type to database
39  if (!empty($this->_type)) {
40  $timerName = $this->_type;
41  }
42 
43  // sql operation
44  $timerName .= '_' . $operation;
45 
46  // database host
47  if (!empty($this->_host)) {
48  $timerName .= '_' . $this->_host;
49  }
50 
51  return \Magento\Framework\Model\ResourceModel\Db\Profiler::TIMER_PREFIX . ':' . $timerName;
52  }
53 
60  protected function _parseQueryType($queryText)
61  {
62  $queryTypeParsed = strtolower(substr(ltrim($queryText), 0, 6));
63 
64  if (!in_array($queryTypeParsed, $this->_queryTypes)) {
65  $queryTypeParsed = 'query';
66  }
67 
68  return $queryTypeParsed;
69  }
70 
78  public function queryStart($queryText, $queryType = null)
79  {
80  $result = parent::queryStart($queryText, $queryType);
81 
82  if ($result !== null) {
83  $queryTypeParsed = $this->_parseQueryType($queryText);
84  $timerName = $this->_getTimerName($queryTypeParsed);
85 
86  $tags = [];
87 
88  // connection type to database
89  $typePrefix = '';
90  if ($this->_type) {
91  $tags['group'] = $this->_type;
92  $typePrefix = $this->_type . ':';
93  }
94 
95  // sql operation
96  $tags['operation'] = $typePrefix . $queryTypeParsed;
97 
98  // database host
99  if ($this->_host) {
100  $tags['host'] = $this->_host;
101  }
102 
103  \Magento\Framework\Profiler::start($timerName, $tags);
104  }
105 
106  return $result;
107  }
108 
115  public function queryEnd($queryId)
116  {
117  $result = parent::queryEnd($queryId);
118 
119  if ($result != self::IGNORED) {
121  $queryProfile = $this->_queryProfiles[$queryId];
122  $queryTypeParsed = $this->_parseQueryType($queryProfile->getQuery());
123  $timerName = $this->_getTimerName($queryTypeParsed);
124 
125  \Magento\Framework\Profiler::stop($timerName);
126  }
127 
128  return $result;
129  }
130 }
queryStart($queryText, $queryType=null)
Definition: Profiler.php:78