PageRenderTime 71ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/Profiler.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 185 lines | 130 code | 22 blank | 33 comment | 16 complexity | d434c08a6dd3ef8eb168e3a3d46f057f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Profiler
  23. * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Varien_Profiler
  27. {
  28. /**
  29. * Timers for code profiling
  30. *
  31. * @var array
  32. */
  33. static private $_timers = array();
  34. static private $_enabled = false;
  35. static private $_memory_get_usage = false;
  36. public static function enable()
  37. {
  38. self::$_enabled = true;
  39. self::$_memory_get_usage = function_exists('memory_get_usage');
  40. }
  41. public static function disable()
  42. {
  43. self::$_enabled = false;
  44. }
  45. public static function reset($timerName)
  46. {
  47. self::$_timers[$timerName] = array(
  48. 'start'=>false,
  49. 'count'=>0,
  50. 'sum'=>0,
  51. 'realmem'=>0,
  52. 'emalloc'=>0,
  53. );
  54. }
  55. public static function resume($timerName)
  56. {
  57. if (!self::$_enabled) {
  58. return;
  59. }
  60. if (empty(self::$_timers[$timerName])) {
  61. self::reset($timerName);
  62. }
  63. if (self::$_memory_get_usage) {
  64. self::$_timers[$timerName]['realmem_start'] = memory_get_usage(true);
  65. self::$_timers[$timerName]['emalloc_start'] = memory_get_usage();
  66. }
  67. self::$_timers[$timerName]['start'] = microtime(true);
  68. self::$_timers[$timerName]['count'] ++;
  69. }
  70. public static function start($timerName)
  71. {
  72. self::resume($timerName);
  73. }
  74. public static function pause($timerName)
  75. {
  76. if (!self::$_enabled) {
  77. return;
  78. }
  79. $time = microtime(true); // Get current time as quick as possible to make more accurate calculations
  80. if (empty(self::$_timers[$timerName])) {
  81. self::reset($timerName);
  82. }
  83. if (false!==self::$_timers[$timerName]['start']) {
  84. self::$_timers[$timerName]['sum'] += $time-self::$_timers[$timerName]['start'];
  85. self::$_timers[$timerName]['start'] = false;
  86. if (self::$_memory_get_usage) {
  87. self::$_timers[$timerName]['realmem'] += memory_get_usage(true)-self::$_timers[$timerName]['realmem_start'];
  88. self::$_timers[$timerName]['emalloc'] += memory_get_usage()-self::$_timers[$timerName]['emalloc_start'];
  89. }
  90. }
  91. }
  92. public static function stop($timerName)
  93. {
  94. self::pause($timerName);
  95. }
  96. public static function fetch($timerName, $key='sum')
  97. {
  98. if (empty(self::$_timers[$timerName])) {
  99. return false;
  100. } elseif (empty($key)) {
  101. return self::$_timers[$timerName];
  102. }
  103. switch ($key) {
  104. case 'sum':
  105. $sum = self::$_timers[$timerName]['sum'];
  106. if (self::$_timers[$timerName]['start']!==false) {
  107. $sum += microtime(true)-self::$_timers[$timerName]['start'];
  108. }
  109. return $sum;
  110. case 'count':
  111. $count = self::$_timers[$timerName]['count'];
  112. return $count;
  113. case 'realmem':
  114. if (!isset(self::$_timers[$timerName]['realmem'])) {
  115. self::$_timers[$timerName]['realmem'] = -1;
  116. }
  117. return self::$_timers[$timerName]['realmem'];
  118. case 'emalloc':
  119. if (!isset(self::$_timers[$timerName]['emalloc'])) {
  120. self::$_timers[$timerName]['emalloc'] = -1;
  121. }
  122. return self::$_timers[$timerName]['emalloc'];
  123. default:
  124. if (!empty(self::$_timers[$timerName][$key])) {
  125. return self::$_timers[$timerName][$key];
  126. }
  127. }
  128. return false;
  129. }
  130. public static function getTimers()
  131. {
  132. return self::$_timers;
  133. }
  134. /**
  135. * Output SQl Zend_Db_Profiler
  136. *
  137. */
  138. public static function getSqlProfiler($res) {
  139. if(!$res){
  140. return '';
  141. }
  142. $out = '';
  143. $profiler = $res->getProfiler();
  144. if($profiler->getEnabled()) {
  145. $totalTime = $profiler->getTotalElapsedSecs();
  146. $queryCount = $profiler->getTotalNumQueries();
  147. $longestTime = 0;
  148. $longestQuery = null;
  149. foreach ($profiler->getQueryProfiles() as $query) {
  150. if ($query->getElapsedSecs() > $longestTime) {
  151. $longestTime = $query->getElapsedSecs();
  152. $longestQuery = $query->getQuery();
  153. }
  154. }
  155. $out .= 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "<br>";
  156. $out .= 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "<br>";
  157. $out .= 'Queries per second: ' . $queryCount / $totalTime . "<br>";
  158. $out .= 'Longest query length: ' . $longestTime . "<br>";
  159. $out .= 'Longest query: <br>' . $longestQuery . "<hr>";
  160. }
  161. return $out;
  162. }
  163. }