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