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
269
270
271
272
273
274
275
276
277
278
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/
namespace Piwik;
/**
* Class Profiler helps with measuring memory, and profiling the database.
* To enable set in your config.ini.php
* [Debug]
* enable_sql_profiler = 1
*
* [log]
* log_writers[] = file
* log_level=debug
*
* @package Piwik
*/
class Profiler
{
/**
* Returns memory usage
*
* @return string
*/
public static function getMemoryUsage()
{
$memory = false;
if (function_exists('xdebug_memory_usage')) {
$memory = xdebug_memory_usage();
} elseif (function_exists('memory_get_usage')) {
$memory = memory_get_usage();
}
if ($memory === false) {
return "Memory usage function not found.";
}
$usage = number_format(round($memory / 1024 / 1024, 2), 2);
return "$usage Mb";
}
/**
* Outputs SQL Profiling reports from Zend
*
* @throws \Exception
*/
public static function displayDbProfileReport()
{
$profiler = Db::get()->getProfiler();
if (!$profiler->getEnabled()) {
throw new \Exception("To display the profiler you should enable enable_sql_profiler on your config/config.ini.php file");
}
$infoIndexedByQuery = array();
foreach ($profiler->getQueryProfiles() as $query) {
if (isset($infoIndexedByQuery[$query->getQuery()])) {
$existing = $infoIndexedByQuery[$query->getQuery()];
} else {
$existing = array('count' => 0, 'sumTimeMs' => 0);
}
$new = array('count' => $existing['count'] + 1,
'sumTimeMs' => $existing['count'] + $query->getElapsedSecs() * 1000);
$infoIndexedByQuery[$query->getQuery()] = $new;
}
uasort($infoIndexedByQuery, 'self::sortTimeDesc');
$str = '<hr /><strong>SQL Profiler</strong><hr /><strong>Summary</strong><br/>';
$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();
}
}
$str .= 'Executed ' . $queryCount . ' queries in ' . round($totalTime, 3) . ' seconds';
$str .= '(Average query length: ' . round($totalTime / $queryCount, 3) . ' seconds)';
$str .= '<br />Queries per second: ' . round($queryCount / $totalTime, 1);
$str .= '<br />Longest query length: ' . round($longestTime, 3) . " seconds (<code>$longestQuery</code>)";
Log::debug($str);
self::getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery);
}
private static function maxSumMsFirst($a, $b)
{
return $a['sum_time_ms'] < $b['sum_time_ms'];
}
static private function sortTimeDesc($a, $b)
{
return $a['sumTimeMs'] < $b['sumTimeMs'];
}
/**
* Print profiling report for the tracker
*
* @param \Piwik\Db $db Tracker database object (or null)
*/
public static function displayDbTrackerProfile($db = null)
{
if (is_null($db)) {
$db = Tracker::getDatabase();
}
$tableName = Common::prefixTable('log_profiling');
$all = $db->fetchAll('SELECT * FROM ' . $tableName);
if ($all === false) {
return;
}
uasort($all, 'self::maxSumMsFirst');
$infoIndexedByQuery = array();
foreach ($all as $infoQuery) {
$query = $infoQuery['query'];
$count = $infoQuery['count'];
$sum_time_ms = $infoQuery['sum_time_ms'];
$infoIndexedByQuery[$query] = array('count' => $count, 'sumTimeMs' => $sum_time_ms);
}
self::getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery);
}
/**
* Print number of queries and elapsed time
*/
public static function printQueryCount()
{
$totalTime = self::getDbElapsedSecs();
$queryCount = Profiler::getQueryCount();
Log::debug(sprintf("Total queries = %d (total sql time = %.2fs)", $queryCount, $totalTime));
}
/**
* Get total elapsed time (in seconds)
*
* @return int elapsed time
*/
public static function getDbElapsedSecs()
{
$profiler = Db::get()->getProfiler();
return $profiler->getTotalElapsedSecs();
}
/**
* Get total number of queries
*
* @return int number of queries
*/
public static function getQueryCount()
{
$profiler = Db::get()->getProfiler();
return $profiler->getTotalNumQueries();
}
/**
* Log a breakdown by query
*
* @param array $infoIndexedByQuery
*/
static private function getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery)
{
$output = '<hr /><strong>Breakdown by query</strong><br/>';
foreach ($infoIndexedByQuery as $query => $queryInfo) {
$timeMs = round($queryInfo['sumTimeMs'], 1);
$count = $queryInfo['count'];
$avgTimeString = '';
if ($count > 1) {
$avgTimeMs = $timeMs / $count;
$avgTimeString = " (average = <b>" . round($avgTimeMs, 1) . "ms</b>)";
}
$query = preg_replace('/([\t\n\r ]+)/', ' ', $query);
$output .= "Executed <b>$count</b> time" . ($count == 1 ? '' : 's') . " in <b>" . $timeMs . "ms</b> $avgTimeString <pre>\t$query</pre>";
}
Log::debug($output);
}
/**
* Initializes Profiling via XHProf.
* See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md
*/
public static function setupProfilerXHProf($mainRun = false)
{
if(!empty($GLOBALS['PIWIK_TRACKER_MODE'])) {
// do not profile Tracker
return;
}
$path = PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_runs.php';
if(!file_exists($path)) {
return;
}
require_once $path;
require_once PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php';
if(!function_exists('xhprof_error')) {
function xhprof_error($out) {
echo substr($out, 0, 300) . '...';
}
}
$currentGitBranch = SettingsPiwik::getCurrentGitBranch();
$profilerNamespace = "piwik";
if($currentGitBranch != 'master') {
$profilerNamespace .= "." . $currentGitBranch;
}
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
if($mainRun) {
self::setProfilingRunIds(array());
}
register_shutdown_function(function () use($profilerNamespace, $mainRun) {
$xhprofData = xhprof_disable();
$xhprofRuns = new \XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace);
$runs = self::getProfilingRunIds();
$runs[] = $runId;
// $weights = array_fill(0, count($runs), 1);
// $aggregate = xhprof_aggregate_runs($xhprofRuns, $runs, $weights, $profilerNamespace);
// $runId = $xhprofRuns->save_run($aggregate, $profilerNamespace);
if($mainRun) {
$runIds = implode(',', $runs);
$out = "\n\nHere is the profiler URL aggregating all runs triggered from this process: ";
$baseUrl = "http://" . @$_SERVER['HTTP_HOST'] . "/" . @$_SERVER['REQUEST_URI'];
$baseUrlStored = SettingsPiwik::getPiwikUrl();
if(strlen($baseUrlStored) > strlen($baseUrl)) {
$baseUrl = $baseUrlStored;
}
$baseUrl = "\n\n" . $baseUrl
."tests/lib/xhprof-0.9.4/xhprof_html/?source=$profilerNamespace&run=";
$out .= $baseUrl . "$runIds\n\n";
$out .= "Main run profile:";
$out .= $baseUrl . "$runId\n\n";
echo ($out);
} else {
self::setProfilingRunIds($runs);
}
});
}
private static function setProfilingRunIds($ids)
{
file_put_contents( self::getPathToXHProfRunIds(), json_encode($ids) );
@chmod(self::getPathToXHProfRunIds(), 0777);
}
private static function getProfilingRunIds()
{
$runIds = file_get_contents( self::getPathToXHProfRunIds() );
$array = json_decode($runIds, $assoc = true);
if(!is_array($array)) {
$array = array();
}
return $array;
}
/**
* @return string
*/
private static function getPathToXHProfRunIds()
{
return PIWIK_INCLUDE_PATH . '/tmp/cache/tests-xhprof-runs';
}
}
|