PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / trunk
Matomo Analytics – Powerful, Privacy-First Insights for WordPress vtrunk
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 month ago Access 3 months ago Application 1 month ago Archive 1 month ago ArchiveProcessor 1 month ago Archiver 2 years ago AssetManager 1 month ago Auth 6 months ago Category 6 months ago Changes 1 month ago CliMulti 1 year ago Columns 1 month ago Concurrency 1 month ago Config 1 month ago Container 1 month ago CronArchive 3 months ago DataAccess 1 month ago DataFiles 2 years ago DataTable 2 weeks ago Db 2 weeks ago DeviceDetector 1 year ago Email 2 years ago Exception 4 months ago Http 4 months ago Intl 3 months ago Log 2 years ago Mail 1 year ago Measurable 6 months ago Menu 1 month ago Metrics 3 months ago Notification 6 months ago Period 1 month ago Plugin 2 weeks ago Policy 1 month ago ProfessionalServices 1 year ago Report 1 year ago ReportRenderer 3 months ago Request 3 months ago Scheduler 1 month ago Segment 1 month ago Session 2 weeks ago Settings 1 month ago Tracker 2 weeks ago Translation 1 month ago Twig 1 year ago UpdateCheck 3 months ago Updater 1 month ago Updates 3 days ago Validators 1 year ago View 1 month ago ViewDataTable 2 weeks ago Visualization 1 year ago Widget 1 month ago .htaccess 2 years ago Access.php 1 month ago Archive.php 1 month ago ArchiveProcessor.php 1 month ago AssetManager.php 1 month ago Auth.php 6 months ago AuthResult.php 6 months ago BaseFactory.php 2 years ago Cache.php 2 years ago CacheId.php 4 months ago CliMulti.php 1 month ago Common.php 2 weeks ago Config.php 1 month ago Console.php 3 months ago Context.php 2 years ago Cookie.php 1 year ago CronArchive.php 1 month ago DI.php 3 months ago DataArray.php 1 month ago DataTable.php 1 month ago Date.php 1 month ago Db.php 1 month ago DbHelper.php 1 month ago Development.php 1 year ago ErrorHandler.php 6 months ago EventDispatcher.php 1 month ago ExceptionHandler.php 4 months ago FileIntegrity.php 1 month ago Filechecks.php 1 year ago Filesystem.php 1 month ago FrontController.php 4 months ago Http.php 1 month ago IP.php 1 year ago Log.php 3 months ago LogDeleter.php 1 year ago Mail.php 1 year ago Metrics.php 1 month ago NoAccessException.php 2 years ago Nonce.php 6 months ago Notification.php 1 month ago NumberFormatter.php 5 months ago Option.php 5 months ago Period.php 1 month ago Piwik.php 1 month ago Plugin.php 1 month ago Process.php 1 month ago Profiler.php 6 months ago ProxyHeaders.php 4 months ago ProxyHttp.php 5 months ago QuickForm2.php 3 months ago RankingQuery.php 1 month ago ReportRenderer.php 1 month ago Request.php 1 month ago Segment.php 1 month ago Sequence.php 6 months ago Session.php 2 weeks ago SettingsPiwik.php 1 month ago SettingsServer.php 1 year ago Singleton.php 2 years ago Site.php 1 month ago SiteContentDetector.php 1 month ago SupportedBrowser.php 2 years ago TCPDF.php 1 year ago Theme.php 1 year ago Timer.php 1 month ago Tracker.php 1 month ago Twig.php 1 month ago Unzip.php 1 year ago UpdateCheck.php 1 month ago Updater.php 1 month ago UpdaterErrorException.php 2 years ago Updates.php 3 months ago Url.php 3 months ago UrlHelper.php 1 month ago Version.php 3 days ago View.php 1 month ago bootstrap.php 1 year ago dispatch.php 2 years ago testMinimumPhpVersion.php 6 months ago
Profiler.php
342 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 // @phpstan-ignore function.inner
214 function xhprof_error($out)
215 {
216 echo substr($out, 0, 300) . '...';
217 }
218 }
219 $currentGitBranch = \Piwik\SettingsPiwik::getCurrentGitBranch();
220 $profilerNamespace = "piwik";
221 if ($currentGitBranch != 'master') {
222 $profilerNamespace .= "-" . $currentGitBranch;
223 }
224 if ($mainRun) {
225 self::setProfilingRunIds(array());
226 }
227 if (function_exists('xhprof_enable')) {
228 xhprof_enable(\XHPROF_FLAGS_CPU + \XHPROF_FLAGS_MEMORY);
229 } elseif (function_exists('tideways_enable')) {
230 tideways_enable(TIDEWAYS_FLAGS_MEMORY | TIDEWAYS_FLAGS_CPU);
231 } elseif (function_exists('tideways_xhprof_enable')) {
232 tideways_xhprof_enable(\TIDEWAYS_XHPROF_FLAGS_MEMORY | \TIDEWAYS_XHPROF_FLAGS_CPU);
233 }
234 register_shutdown_function(function () use($profilerNamespace, $mainRun, $outputDir) {
235 if (function_exists('xhprof_disable')) {
236 $xhprofData = xhprof_disable();
237 $xhprofRuns = new XHProfRuns_Default();
238 $runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace);
239 } elseif (function_exists('tideways_xhprof_disable') || function_exists('tideways_disable')) {
240 if (function_exists('tideways_xhprof_disable')) {
241 $xhprofData = tideways_xhprof_disable();
242 } elseif (function_exists('tideways_disable')) {
243 $xhprofData = tideways_disable();
244 }
245 $runId = uniqid();
246 file_put_contents($outputDir . \DIRECTORY_SEPARATOR . $runId . '.' . $profilerNamespace . '.xhprof', serialize($xhprofData));
247 $meta = array('time' => time(), 'instance' => \Piwik\SettingsPiwik::getPiwikInstanceId());
248 if (!empty($_GET)) {
249 $meta['get'] = $_GET;
250 }
251 if (!empty($_POST)) {
252 $meta['post'] = $_POST;
253 }
254 file_put_contents($outputDir . \DIRECTORY_SEPARATOR . $runId . '.' . $profilerNamespace . '.meta', serialize($meta));
255 }
256 if (empty($runId)) {
257 die('could not write profiler run');
258 }
259 $runs = \Piwik\Profiler::getProfilingRunIds();
260 array_unshift($runs, $runId);
261 if ($mainRun) {
262 \Piwik\Profiler::aggregateXhprofRuns($runs, $profilerNamespace, $saveTo = $runId);
263 $baseUrlStored = \Piwik\SettingsPiwik::getPiwikUrl();
264 $host = \Piwik\Url::getHost();
265 $out = "\n\n";
266 $baseUrl = "http://" . $host . "/" . @$_SERVER['REQUEST_URI'];
267 if (strlen($baseUrlStored) > strlen($baseUrl)) {
268 $baseUrl = $baseUrlStored;
269 }
270 $baseUrl = $baseUrlStored . "vendor/lox/xhprof/xhprof_html/?source={$profilerNamespace}&run={$runId}";
271 $baseUrl = \Piwik\Common::sanitizeInputValue($baseUrl);
272 $out .= "Profiler report is available at:\n";
273 $out .= "<a href='{$baseUrl}'>{$baseUrl}</a>";
274 $out .= "\n\n";
275 if (\Piwik\Development::isEnabled()) {
276 $out .= "WARNING: Development mode is enabled. Many runtime optimizations are not applied in development mode. ";
277 $out .= "Unless you intend to profile Matomo in development mode, your profile may not be accurate.";
278 $out .= "\n\n";
279 }
280 echo $out;
281 } else {
282 \Piwik\Profiler::setProfilingRunIds($runs);
283 }
284 });
285 self::$isXhprofSetup = \true;
286 }
287 /**
288 * Aggregates xhprof runs w/o normalizing (xhprof_aggregate_runs will always average data which
289 * does not fit Piwik's use case).
290 */
291 public static function aggregateXhprofRuns($runIds, $profilerNamespace, $saveToRunId)
292 {
293 $xhprofRuns = new XHProfRuns_Default();
294 $aggregatedData = array();
295 foreach ($runIds as $runId) {
296 $xhprofRunData = $xhprofRuns->get_run($runId, $profilerNamespace, $description);
297 foreach ($xhprofRunData as $key => $data) {
298 if (empty($aggregatedData[$key])) {
299 $aggregatedData[$key] = $data;
300 } else {
301 // don't aggregate main() metrics since only the super run has the correct metrics for the entire run
302 if ($key == "main()") {
303 continue;
304 }
305 $aggregatedData[$key]["ct"] += $data["ct"];
306 // call count
307 $aggregatedData[$key]["wt"] += $data["wt"];
308 // incl. wall time
309 $aggregatedData[$key]["cpu"] += $data["cpu"];
310 // cpu time
311 $aggregatedData[$key]["mu"] += $data["mu"];
312 // memory usage
313 $aggregatedData[$key]["pmu"] = max($aggregatedData[$key]["pmu"], $data["pmu"]);
314 // peak mem usage
315 }
316 }
317 }
318 $xhprofRuns->save_run($aggregatedData, $profilerNamespace, $saveToRunId);
319 }
320 public static function setProfilingRunIds($ids)
321 {
322 file_put_contents(self::getPathToXHProfRunIds(), json_encode($ids));
323 @chmod(self::getPathToXHProfRunIds(), 0777);
324 }
325 public static function getProfilingRunIds()
326 {
327 $runIds = file_get_contents(self::getPathToXHProfRunIds());
328 $array = json_decode($runIds, $assoc = \true);
329 if (!is_array($array)) {
330 $array = array();
331 }
332 return $array;
333 }
334 /**
335 * @return string
336 */
337 private static function getPathToXHProfRunIds()
338 {
339 return PIWIK_INCLUDE_PATH . '/tmp/cache/tests-xhprof-runs';
340 }
341 }
342