Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormattab <matthieu.aubry@gmail.com>2013-11-18 01:39:43 +0400
committermattab <matthieu.aubry@gmail.com>2013-11-18 01:39:43 +0400
commit681a5c8c80cc538d29e7e587a32abc9e6c7d8181 (patch)
treeb4b4b529f476de139cb019b7fcf02c5a41bc359f /core/Profiler.php
parent070035fe4fd0c370445f5c56919aff180935cd28 (diff)
Improving XHProf support. At the end of a request with XHProf enabled, it will output the link to see the profiling info. New --xhprof option to archive.php, and enabling XHProf when &xhprof=1 is found in the request.
Diffstat (limited to 'core/Profiler.php')
-rw-r--r--core/Profiler.php94
1 files changed, 94 insertions, 0 deletions
diff --git a/core/Profiler.php b/core/Profiler.php
index bce7da3d33..7a0f2b0d39 100644
--- a/core/Profiler.php
+++ b/core/Profiler.php
@@ -182,4 +182,98 @@ class Profiler
}
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;
+
+ $currentGitBranch = self::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";
+ Log::info($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;
+ }
+
+ private static function getCurrentGitBranch()
+ {
+ $firstLineOfGitHead = file(PIWIK_INCLUDE_PATH . '/.git/HEAD');
+ $firstLineOfGitHead = $firstLineOfGitHead[0];
+ $parts = explode("/", $firstLineOfGitHead);
+ $currentGitBranch = trim($parts[2]);
+ return $currentGitBranch;
+ }
+
+ /**
+ * @return string
+ */
+ private static function getPathToXHProfRunIds()
+ {
+ return PIWIK_INCLUDE_PATH . '/tmp/cache/tests-xhprof-runs';
+ }
} \ No newline at end of file