PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Profiler.php
matomo / app / core Last commit date
API 6 years ago Access 6 years ago Application 6 years ago Archive 6 years ago ArchiveProcessor 6 years ago Archiver 6 years ago AssetManager 6 years ago Auth 6 years ago Category 6 years ago CliMulti 6 years ago Columns 6 years ago Composer 6 years ago Concurrency 6 years ago Config 6 years ago Container 6 years ago CronArchive 6 years ago DataAccess 5 years ago DataFiles 6 years ago DataTable 6 years ago Db 6 years ago DeviceDetector 5 years ago Email 6 years ago Exception 6 years ago Http 6 years ago Intl 6 years ago Mail 6 years ago Measurable 6 years ago Menu 6 years ago Metrics 6 years ago Notification 6 years ago Period 6 years ago Plugin 6 years ago ProfessionalServices 6 years ago Report 6 years ago ReportRenderer 6 years ago Scheduler 6 years ago Segment 6 years ago Session 6 years ago Settings 6 years ago Tracker 5 years ago Translation 6 years ago UpdateCheck 6 years ago Updater 6 years ago Updates 6 years ago Validators 6 years ago View 6 years ago ViewDataTable 6 years ago Visualization 6 years ago Widget 6 years ago .htaccess 6 years ago Access.php 6 years ago Archive.php 6 years ago ArchiveProcessor.php 6 years ago AssetManager.php 6 years ago Auth.php 6 years ago BaseFactory.php 6 years ago Cache.php 6 years ago CacheId.php 6 years ago CliMulti.php 6 years ago Common.php 6 years ago Config.php 6 years ago Console.php 6 years ago Context.php 6 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 6 years ago DataTable.php 6 years ago Date.php 6 years ago Db.php 6 years ago DbHelper.php 6 years ago Development.php 6 years ago DeviceDetectorFactory.php 6 years ago ErrorHandler.php 6 years ago EventDispatcher.php 6 years ago ExceptionHandler.php 6 years ago FileIntegrity.php 6 years ago Filechecks.php 6 years ago Filesystem.php 6 years ago FrontController.php 6 years ago Http.php 6 years ago IP.php 6 years ago Log.php 6 years ago LogDeleter.php 6 years ago Mail.php 6 years ago Metrics.php 6 years ago MetricsFormatter.php 6 years ago Nonce.php 5 years ago Notification.php 6 years ago NumberFormatter.php 6 years ago Option.php 5 years ago Period.php 6 years ago Piwik.php 6 years ago Plugin.php 6 years ago Profiler.php 6 years ago ProxyHeaders.php 6 years ago ProxyHttp.php 6 years ago QuickForm2.php 6 years ago RankingQuery.php 6 years ago Registry.php 6 years ago ReportRenderer.php 6 years ago ScheduledTask.php 6 years ago Segment.php 6 years ago Sequence.php 6 years ago Session.php 6 years ago SettingsPiwik.php 6 years ago SettingsServer.php 6 years ago Singleton.php 6 years ago Site.php 6 years ago TCPDF.php 6 years ago TaskScheduler.php 6 years ago Theme.php 6 years ago Timer.php 6 years ago Tracker.php 6 years ago Translate.php 6 years ago Twig.php 6 years ago Unzip.php 6 years ago UpdateCheck.php 6 years ago Updater.php 6 years ago Updates.php 6 years ago Url.php 6 years ago UrlHelper.php 6 years ago Version.php 5 years ago View.php 6 years ago bootstrap.php 6 years ago dispatch.php 6 years ago testMinimumPhpVersion.php 6 years ago
Profiler.php
371 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik;
10
11 use Exception;
12 use XHProfRuns_Default;
13
14 /**
15 * Class Profiler helps with measuring memory, and profiling the database.
16 * To enable set in your config.ini.php
17 * [Debug]
18 * enable_sql_profiler = 1
19 *
20 * [log]
21 * log_writers[] = file
22 * log_level=debug
23 *
24 */
25 class Profiler
26 {
27 /**
28 * Whether xhprof has been setup or not.
29 *
30 * @var bool
31 */
32 private static $isXhprofSetup = false;
33
34 /**
35 * Returns memory usage
36 *
37 * @return string
38 */
39 public static function getMemoryUsage()
40 {
41 $memory = false;
42 if (function_exists('xdebug_memory_usage')) {
43 $memory = xdebug_memory_usage();
44 } elseif (function_exists('memory_get_usage')) {
45 $memory = memory_get_usage();
46 }
47 if ($memory === false) {
48 return "Memory usage function not found.";
49 }
50 $usage = number_format(round($memory / 1024 / 1024, 2), 2);
51 return "$usage Mb";
52 }
53
54 /**
55 * Outputs SQL Profiling reports from Zend
56 *
57 * @throws \Exception
58 */
59 public static function displayDbProfileReport()
60 {
61 $profiler = Db::get()->getProfiler();
62
63 if (!$profiler->getEnabled()) {
64 // To display the profiler you should enable enable_sql_profiler on your config/config.ini.php file
65 return;
66 }
67
68 $infoIndexedByQuery = array();
69 foreach ($profiler->getQueryProfiles() as $query) {
70 if (isset($infoIndexedByQuery[$query->getQuery()])) {
71 $existing = $infoIndexedByQuery[$query->getQuery()];
72 } else {
73 $existing = array('count' => 0, 'sumTimeMs' => 0);
74 }
75 $new = array('count' => $existing['count'] + 1,
76 'sumTimeMs' => $existing['count'] + $query->getElapsedSecs() * 1000);
77 $infoIndexedByQuery[$query->getQuery()] = $new;
78 }
79
80 uasort($infoIndexedByQuery, 'self::sortTimeDesc');
81
82 $str = '<hr /><strong>SQL Profiler</strong><hr /><strong>Summary</strong><br/>';
83 $totalTime = $profiler->getTotalElapsedSecs();
84 $queryCount = $profiler->getTotalNumQueries();
85 $longestTime = 0;
86 $longestQuery = null;
87 foreach ($profiler->getQueryProfiles() as $query) {
88 if ($query->getElapsedSecs() > $longestTime) {
89 $longestTime = $query->getElapsedSecs();
90 $longestQuery = $query->getQuery();
91 }
92 }
93 $str .= 'Executed ' . $queryCount . ' queries in ' . round($totalTime, 3) . ' seconds';
94 $str .= '(Average query length: ' . round($totalTime / $queryCount, 3) . ' seconds)';
95 $str .= '<br />Queries per second: ' . round($queryCount / $totalTime, 1);
96 $str .= '<br />Longest query length: ' . round($longestTime, 3) . " seconds (<code>$longestQuery</code>)";
97 Log::debug($str);
98 self::getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery);
99 }
100
101 private static function maxSumMsFirst($a, $b)
102 {
103 return $a['sum_time_ms'] < $b['sum_time_ms'];
104 }
105
106 private static function sortTimeDesc($a, $b)
107 {
108 return $a['sumTimeMs'] < $b['sumTimeMs'];
109 }
110
111 /**
112 * Print profiling report for the tracker
113 *
114 * @param \Piwik\Db $db Tracker database object (or null)
115 */
116 public static function displayDbTrackerProfile($db = null)
117 {
118 if (is_null($db)) {
119 $db = Tracker::getDatabase();
120 }
121 $tableName = Common::prefixTable('log_profiling');
122
123 $all = $db->fetchAll('SELECT * FROM ' . $tableName);
124 if ($all === false) {
125 return;
126 }
127 uasort($all, 'self::maxSumMsFirst');
128
129 $infoIndexedByQuery = array();
130 foreach ($all as $infoQuery) {
131 $query = $infoQuery['query'];
132 $count = $infoQuery['count'];
133 $sum_time_ms = $infoQuery['sum_time_ms'];
134 $infoIndexedByQuery[$query] = array('count' => $count, 'sumTimeMs' => $sum_time_ms);
135 }
136 self::getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery);
137 }
138
139 /**
140 * Print number of queries and elapsed time
141 */
142 public static function printQueryCount()
143 {
144 $totalTime = self::getDbElapsedSecs();
145 $queryCount = Profiler::getQueryCount();
146 if ($queryCount > 0) {
147 Log::debug(sprintf("Total queries = %d (total sql time = %.2fs)", $queryCount, $totalTime));
148 }
149 }
150
151 /**
152 * Get total elapsed time (in seconds)
153 *
154 * @return int elapsed time
155 */
156 public static function getDbElapsedSecs()
157 {
158 $profiler = Db::get()->getProfiler();
159 return $profiler->getTotalElapsedSecs();
160 }
161
162 /**
163 * Get total number of queries
164 *
165 * @return int number of queries
166 */
167 public static function getQueryCount()
168 {
169 $profiler = Db::get()->getProfiler();
170 return $profiler->getTotalNumQueries();
171 }
172
173 /**
174 * Log a breakdown by query
175 *
176 * @param array $infoIndexedByQuery
177 */
178 private static function getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery)
179 {
180 $output = '<hr /><strong>Breakdown by query</strong><br/>';
181 foreach ($infoIndexedByQuery as $query => $queryInfo) {
182 $timeMs = round($queryInfo['sumTimeMs'], 1);
183 $count = $queryInfo['count'];
184 $avgTimeString = '';
185 if ($count > 1) {
186 $avgTimeMs = $timeMs / $count;
187 $avgTimeString = " (average = <b>" . round($avgTimeMs, 1) . "ms</b>)";
188 }
189 $query = preg_replace('/([\t\n\r ]+)/', ' ', $query);
190 $output .= "Executed <b>$count</b> time" . ($count == 1 ? '' : 's') . " in <b>" . $timeMs . "ms</b> $avgTimeString <pre>\t$query</pre>";
191 }
192 Log::debug($output);
193 }
194
195 /**
196 * Initializes Profiling via XHProf.
197 * See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md
198 */
199 public static function setupProfilerXHProf($mainRun = false, $setupDuringTracking = false)
200 {
201 if (!$setupDuringTracking
202 && SettingsServer::isTrackerApiRequest()
203 ) {
204 // do not profile Tracker
205 return;
206 }
207
208 if (self::$isXhprofSetup) {
209 return;
210 }
211
212 $hasXhprof = function_exists('xhprof_enable');
213 $hasTidewaysXhprof = function_exists('tideways_xhprof_enable') || function_exists('tideways_enable');
214
215 if (!$hasXhprof && !$hasTidewaysXhprof) {
216 $xhProfPath = PIWIK_INCLUDE_PATH . '/vendor/facebook/xhprof/extension/modules/xhprof.so';
217 throw new Exception("Cannot find xhprof_enable, make sure to 1) install xhprof: run 'composer install --dev' and build the extension, and 2) add 'extension=$xhProfPath' to your php.ini.");
218 }
219
220 $outputDir = ini_get("xhprof.output_dir");
221 if (!$outputDir && $hasTidewaysXhprof) {
222 $outputDir = sys_get_temp_dir();
223 }
224
225 if (empty($outputDir)) {
226 throw new Exception("The profiler output dir is not set. Add 'xhprof.output_dir=...' to your php.ini.");
227 }
228 if (!is_writable($outputDir)) {
229 throw new Exception("The profiler output dir '" . ini_get("xhprof.output_dir") . "' should exist and be writable.");
230 }
231
232 if (!function_exists('xhprof_error')) {
233 function xhprof_error($out)
234 {
235 echo substr($out, 0, 300) . '...';
236 }
237 }
238
239 $currentGitBranch = SettingsPiwik::getCurrentGitBranch();
240 $profilerNamespace = "piwik";
241 if ($currentGitBranch != 'master') {
242 $profilerNamespace .= "-" . $currentGitBranch;
243 }
244
245 if ($mainRun) {
246 self::setProfilingRunIds(array());
247 }
248
249 if (function_exists('xhprof_enable')) {
250 xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
251 } elseif (function_exists('tideways_enable')) {
252 tideways_enable(TIDEWAYS_FLAGS_MEMORY | TIDEWAYS_FLAGS_CPU);
253 } elseif (function_exists('tideways_xhprof_enable')) {
254 tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);
255 }
256
257 register_shutdown_function(function () use ($profilerNamespace, $mainRun, $outputDir) {
258 if (function_exists('xhprof_disable')) {
259 $xhprofData = xhprof_disable();
260 $xhprofRuns = new XHProfRuns_Default();
261 $runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace);
262 } elseif (function_exists('tideways_xhprof_disable') || function_exists('tideways_disable')) {
263 if (function_exists('tideways_xhprof_disable')) {
264 $xhprofData = tideways_xhprof_disable();
265 } else {
266 $xhprofData = tideways_disable();
267 }
268 $runId = uniqid();
269 file_put_contents(
270 $outputDir . DIRECTORY_SEPARATOR . $runId . '.' . $profilerNamespace . '.xhprof',
271 serialize($xhprofData)
272 );
273 }
274
275 if (empty($runId)) {
276 die('could not write profiler run');
277 }
278
279 $runs = Profiler::getProfilingRunIds();
280 array_unshift($runs, $runId);
281
282 if ($mainRun) {
283 Profiler::aggregateXhprofRuns($runs, $profilerNamespace, $saveTo = $runId);
284
285 $baseUrlStored = SettingsPiwik::getPiwikUrl();
286
287 $out = "\n\n";
288 $baseUrl = "http://" . @$_SERVER['HTTP_HOST'] . "/" . @$_SERVER['REQUEST_URI'];
289 if (strlen($baseUrlStored) > strlen($baseUrl)) {
290 $baseUrl = $baseUrlStored;
291 }
292 $baseUrl = $baseUrlStored . "vendor/facebook/xhprof/xhprof_html/?source=$profilerNamespace&run=$runId";
293
294 $out .= "Profiler report is available at:\n";
295 $out .= "<a href='$baseUrl'>$baseUrl</a>";
296 $out .= "\n\n";
297
298 if (Development::isEnabled()) {
299 $out .= "WARNING: Development mode is enabled. Many runtime optimizations are not applied in development mode. ";
300 $out .= "Unless you intend to profile Matomo in development mode, your profile may not be accurate.";
301 $out .= "\n\n";
302 }
303
304 echo $out;
305 } else {
306 Profiler::setProfilingRunIds($runs);
307 }
308 });
309
310 self::$isXhprofSetup = true;
311 }
312
313 /**
314 * Aggregates xhprof runs w/o normalizing (xhprof_aggregate_runs will always average data which
315 * does not fit Piwik's use case).
316 */
317 public static function aggregateXhprofRuns($runIds, $profilerNamespace, $saveToRunId)
318 {
319 $xhprofRuns = new XHProfRuns_Default();
320
321 $aggregatedData = array();
322
323 foreach ($runIds as $runId) {
324 $xhprofRunData = $xhprofRuns->get_run($runId, $profilerNamespace, $description);
325
326 foreach ($xhprofRunData as $key => $data) {
327 if (empty($aggregatedData[$key])) {
328 $aggregatedData[$key] = $data;
329 } else {
330 // don't aggregate main() metrics since only the super run has the correct metrics for the entire run
331 if ($key == "main()") {
332 continue;
333 }
334
335 $aggregatedData[$key]["ct"] += $data["ct"]; // call count
336 $aggregatedData[$key]["wt"] += $data["wt"]; // incl. wall time
337 $aggregatedData[$key]["cpu"] += $data["cpu"]; // cpu time
338 $aggregatedData[$key]["mu"] += $data["mu"]; // memory usage
339 $aggregatedData[$key]["pmu"] = max($aggregatedData[$key]["pmu"], $data["pmu"]); // peak mem usage
340 }
341 }
342 }
343
344 $xhprofRuns->save_run($aggregatedData, $profilerNamespace, $saveToRunId);
345 }
346
347 public static function setProfilingRunIds($ids)
348 {
349 file_put_contents(self::getPathToXHProfRunIds(), json_encode($ids));
350 @chmod(self::getPathToXHProfRunIds(), 0777);
351 }
352
353 public static function getProfilingRunIds()
354 {
355 $runIds = file_get_contents(self::getPathToXHProfRunIds());
356 $array = json_decode($runIds, $assoc = true);
357 if (!is_array($array)) {
358 $array = array();
359 }
360 return $array;
361 }
362
363 /**
364 * @return string
365 */
366 private static function getPathToXHProfRunIds()
367 {
368 return PIWIK_INCLUDE_PATH . '/tmp/cache/tests-xhprof-runs';
369 }
370 }
371