From 681a5c8c80cc538d29e7e587a32abc9e6c7d8181 Mon Sep 17 00:00:00 2001 From: mattab Date: Mon, 18 Nov 2013 10:39:43 +1300 Subject: 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. --- core/Profiler.php | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) (limited to 'core/Profiler.php') 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 -- cgit v1.2.3