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