Profiler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Magento profiler for requests to database
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Model\ResourceModel\Db;
  9. class Profiler extends \Magento\Framework\DB\Profiler
  10. {
  11. /**
  12. * Default connection type for timer name creation
  13. */
  14. const TIMER_PREFIX = 'DB_QUERY';
  15. /**
  16. * Default connection type for timer name creation
  17. */
  18. const DEFAULT_CONNECTION_TYPE = 'database';
  19. /**
  20. * @var array Allowed query types
  21. */
  22. protected $_queryTypes = ['select', 'insert', 'update', 'delete'];
  23. /**
  24. * Form and return timer name
  25. *
  26. * @param string $operation
  27. * @return string
  28. */
  29. protected function _getTimerName($operation)
  30. {
  31. // default name of connection type
  32. $timerName = \Magento\Framework\Model\ResourceModel\Db\Profiler::DEFAULT_CONNECTION_TYPE;
  33. // connection type to database
  34. if (!empty($this->_type)) {
  35. $timerName = $this->_type;
  36. }
  37. // sql operation
  38. $timerName .= '_' . $operation;
  39. // database host
  40. if (!empty($this->_host)) {
  41. $timerName .= '_' . $this->_host;
  42. }
  43. return \Magento\Framework\Model\ResourceModel\Db\Profiler::TIMER_PREFIX . ':' . $timerName;
  44. }
  45. /**
  46. * Parse query type and return
  47. *
  48. * @param string $queryText
  49. * @return string
  50. */
  51. protected function _parseQueryType($queryText)
  52. {
  53. $queryTypeParsed = strtolower(substr(ltrim($queryText), 0, 6));
  54. if (!in_array($queryTypeParsed, $this->_queryTypes)) {
  55. $queryTypeParsed = 'query';
  56. }
  57. return $queryTypeParsed;
  58. }
  59. /**
  60. * Starts a query. Creates a new query profile object (\Zend_Db_Profiler_Query)
  61. *
  62. * @param string $queryText SQL statement
  63. * @param integer $queryType OPTIONAL Type of query, one of the \Zend_Db_Profiler::* constants
  64. * @return integer|null
  65. */
  66. public function queryStart($queryText, $queryType = null)
  67. {
  68. $result = parent::queryStart($queryText, $queryType);
  69. if ($result !== null) {
  70. $queryTypeParsed = $this->_parseQueryType($queryText);
  71. $timerName = $this->_getTimerName($queryTypeParsed);
  72. $tags = [];
  73. // connection type to database
  74. $typePrefix = '';
  75. if ($this->_type) {
  76. $tags['group'] = $this->_type;
  77. $typePrefix = $this->_type . ':';
  78. }
  79. // sql operation
  80. $tags['operation'] = $typePrefix . $queryTypeParsed;
  81. // database host
  82. if ($this->_host) {
  83. $tags['host'] = $this->_host;
  84. }
  85. \Magento\Framework\Profiler::start($timerName, $tags);
  86. }
  87. return $result;
  88. }
  89. /**
  90. * Ends a query. Pass it the handle that was returned by queryStart().
  91. *
  92. * @param int $queryId
  93. * @return string|void
  94. */
  95. public function queryEnd($queryId)
  96. {
  97. $result = parent::queryEnd($queryId);
  98. if ($result != self::IGNORED) {
  99. /** @var \Zend_Db_Profiler_Query $queryProfile */
  100. $queryProfile = $this->_queryProfiles[$queryId];
  101. $queryTypeParsed = $this->_parseQueryType($queryProfile->getQuery());
  102. $timerName = $this->_getTimerName($queryTypeParsed);
  103. \Magento\Framework\Profiler::stop($timerName);
  104. }
  105. return $result;
  106. }
  107. }