-
Notifications
You must be signed in to change notification settings - Fork 1
/
debugTiming.php
268 lines (224 loc) · 8.14 KB
/
debugTiming.php
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/********************************************************************************
* The contents of this file are subject to the Common Public Attribution License
* Version 1.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.wikiarguments.net/license/. The License is based on the Mozilla
* Public License Version 1.1 but Sections 14 and 15 have been added to cover
* use of software over a computer network and provide for limited attribution
* for the Original Developer. In addition, Exhibit A has been modified to be
* consistent with Exhibit B.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is Wikiarguments. The Original Developer is the Initial
* Developer and is Wikiarguments GbR. All portions of the code written by
* Wikiarguments GbR are Copyright (c) 2012. All Rights Reserved.
* Contributor(s):
* Andreas Wierz (andreas.wierz@gmail.com).
*
* Attribution Information
* Attribution Phrase (not exceeding 10 words): Powered by Wikiarguments
* Attribution URL: http://www.wikiarguments.net
*
* This display should be, at a minimum, the Attribution Phrase displayed in the
* footer of the page and linked to the Attribution URL. The link to the Attribution
* URL must not contain any form of 'nofollow' attribute.
*
* Display of Attribution Information is required in Larger Works which are
* defined in the CPAL as a work which combines Covered Code or portions
* thereof with code not governed by the terms of the CPAL.
*******************************************************************************/
/*
* Debugging class for tracking of timing behavior.
*/
class DebugTiming
{
public function DebugTiming()
{
$this->timers = Array();
$this->start('global');
}
public function start($name)
{
if(@!$this->timers[$name])
{
$this->timers[$name] = new Timer();
}
if($this->timers[$name]->isRunning())
{
return false;
}
$this->timers[$name]->start();
return true;
}
public function stop($name)
{
if(!$this->timers[$name] || !$this->timers[$name]->isRunning())
{
return false;
}
$this->timers[$name]->stop();
}
public function getTimersRaw()
{
return $this->timers;
}
public function getGlobalRuntime()
{
$tm = $this->getTimerByName('global');
return $tm["time"];
}
public function getRuntimeByName($name)
{
$tm = $this->getTimerByName($name);
return $tm["time"];
}
public function getTimerByName($name)
{
if($name == 'global')
{
$this->stop('global');
}
$tm = Array();
$tm["name"] = $name;
$tm["time"] = 0;
$tm["isRunning"] = -1;
if(!$this->timers[$name])
{
return false;
}
$tm["time"] = $this->timers[$name]->getTimeElapsedMs();
$tm["isRunning"] = $this->timers[$name]->isRunning() ? 1 : 0;
if($name == 'global')
{
$this->start('global');
}
return $tm;
}
public function getTimingData()
{
$ret = Array();
$this->stop('global');
foreach($this->timers as $k => $v)
{
$tm = Array();
$tm["name"] = $k;
$tm["time"] = $v->getTimeElapsedMs();
$tm["isRunning"] = $v->isRunning() ? 1 : 0;
array_push($ret, $tm);
}
$this->start('global');
return $ret;
}
public function logPerformance($annotation, $requestObj)
{
global $sLog;
$sLog->logMessage(LOG_TYPE_PERFORMANCE, $this->performanceString($annotation, $requestObj));
$sLog->logMessage(LOG_TYPE_PERFORMANCE_VERBOSE, $this->performanceStringVerbose($annotation, $requestObj));
}
public function performanceString($annotation, $requestObj, $isFinal = true)
{
global $sUser, $sTimer, $sDB, $sMD, $sStatistics;
$slowQuerys = $sDB->getSlowQuerys();
$msg = $annotation."\n";
$msg .= sprintf("%20s %d\n", "userId:", $sUser->getUserId());
$msg .= sprintf("%20s %d\n", "timeTotal:", $sTimer->getGlobalRuntime());
$msg .= sprintf("%20s %d\n", "queryCacheHits:", $sStatistics->queryCacheHits());
$msg .= sprintf("%20s %d\n", "queryCacheMisses:", $sStatistics->queryCacheMisses());
$msg .= sprintf("%20s %d\n", "memcachedQuerys:", $sMD->getNumQuerys());
$msg .= sprintf("%20s %d\n", "memcachedMisses:", $sMD->getNumMisses());
$msg .= sprintf("%20s %d\n", "slowQuerys:", count($slowQuerys));
$msg .= "---------------TIMERS---------------\n";
foreach($sTimer->getTimingData() as $k => $v)
{
$msg .= sprintf("%20s: %10d\n", $v["name"], $v["time"]);
}
$msg .= "---------------REQUEST_OBJECT---------------\n";
$msg .= serialize($requestObj)."\n";
if($isFinal)
{
$msg .= "------------------------------\n";
}
return $msg;
}
public function performanceStringVerbose($annotation, $requestObj, $isFinal = true)
{
global $sDB;
$slowQuerys = $sDB->getSlowQuerys();
$msg = $this->performanceString($annotation, $requestObj, false);
$msg .= "---------------SLOW_QUERYS---------------\n";
foreach($slowQuerys as $k => $q)
{
$msg .= serialize($q)."\n";
}
/*$msg .= "---------------BACKTRACE---------------\n";
$msg .= $this->buildBacktrace();*/
if($isFinal)
{
$msg .= "------------------------------\n";
}
return $msg;
}
public function buildBacktrace()
{
$backtrace = debug_backtrace();
foreach ($backtrace as $bt)
{
$args = '';
foreach ($bt['args'] as $a)
{
if (!empty($args))
{
$args .= ', ';
}
switch (gettype($a))
{
case 'integer':
case 'double':
{
$args .= $a;
}break;
case 'string':
{
$a = mb_substr($a, 0, 64).((strlen($a) > 64) ? '...' : '');
$args .= "\"$a\"";
}break;
case 'array':
{
$args .= 'Array('.count($a).')';
}break;
case 'object':
{
$args .= 'Object('.get_class($a).')';
}break;
case 'resource':
{
$args .= 'Resource('.strstr($a, '#').')';
}break;
case 'boolean':
{
$args .= $a ? 'True' : 'False';
}break;
case 'NULL':
{
$args .= 'Null';
}break;
default:
{
$args .= 'Unknown';
}
}
}
$line = sprintf("%s%s%s(%s)", $bt['class'], $bt['type'], $bt['function'], $args);
$line = str_pad($line, 100, " ", STR_PAD_RIGHT)." | ";
$line .= sprintf("%s:%d\n", $bt['file'], $bt['line']);
$output .= $line;
}
return $output;
}
private $timers;
};
?>