update Piwik to version 2.16 (fixes #91)

This commit is contained in:
oliver 2016-04-10 18:55:57 +02:00
commit d885a4baa9
5833 changed files with 418860 additions and 226988 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Piwik - Open source web analytics
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
@ -8,6 +8,8 @@
*/
namespace Piwik;
use Exception;
use XHProfRuns_Default;
/**
* Class Profiler helps with measuring memory, and profiling the database.
@ -22,6 +24,13 @@ namespace Piwik;
*/
class Profiler
{
/**
* Whether xhprof has been setup or not.
*
* @var bool
*/
private static $isXhprofSetup = false;
/**
* Returns memory usage
*
@ -52,7 +61,8 @@ class Profiler
$profiler = Db::get()->getProfiler();
if (!$profiler->getEnabled()) {
throw new \Exception("To display the profiler you should enable enable_sql_profiler on your config/config.ini.php file");
// To display the profiler you should enable enable_sql_profiler on your config/config.ini.php file
return;
}
$infoIndexedByQuery = array();
@ -93,7 +103,7 @@ class Profiler
return $a['sum_time_ms'] < $b['sum_time_ms'];
}
static private function sortTimeDesc($a, $b)
private static function sortTimeDesc($a, $b)
{
return $a['sumTimeMs'] < $b['sumTimeMs'];
}
@ -133,7 +143,9 @@ class Profiler
{
$totalTime = self::getDbElapsedSecs();
$queryCount = Profiler::getQueryCount();
Log::debug(sprintf("Total queries = %d (total sql time = %.2fs)", $queryCount, $totalTime));
if ($queryCount > 0) {
Log::debug(sprintf("Total queries = %d (total sql time = %.2fs)", $queryCount, $totalTime));
}
}
/**
@ -163,7 +175,7 @@ class Profiler
*
* @param array $infoIndexedByQuery
*/
static private function getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery)
private static function getSqlProfilingQueryBreakdownOutput($infoIndexedByQuery)
{
$output = '<hr /><strong>Breakdown by query</strong><br/>';
foreach ($infoIndexedByQuery as $query => $queryInfo) {
@ -184,97 +196,139 @@ class Profiler
* Initializes Profiling via XHProf.
* See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md
*/
public static function setupProfilerXHProf($mainRun = false)
public static function setupProfilerXHProf($mainRun = false, $setupDuringTracking = false)
{
if(SettingsServer::isTrackerApiRequest()) {
if (!$setupDuringTracking
&& SettingsServer::isTrackerApiRequest()
) {
// 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)) {
if (self::$isXhprofSetup) {
return;
}
if(!function_exists('xhprof_enable')) {
return;
if (!function_exists('xhprof_enable')) {
$xhProfPath = PIWIK_INCLUDE_PATH . '/vendor/facebook/xhprof/extension/modules/xhprof.so';
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.");
}
if(!is_writable(ini_get("xhprof.output_dir"))) {
throw new \Exception("The profiler output dir '" .ini_get("xhprof.output_dir"). "' should exist and be writable.");
$outputDir = ini_get("xhprof.output_dir");
if (empty($outputDir)) {
throw new Exception("The profiler output dir is not set. Add 'xhprof.output_dir=...' to your php.ini.");
}
if (!is_writable($outputDir)) {
throw new Exception("The profiler output dir '" . ini_get("xhprof.output_dir") . "' should exist and be writable.");
}
require_once $path;
require_once PIWIK_INCLUDE_PATH . '/tests/lib/xhprof-0.9.4/xhprof_lib/utils/xhprof_lib.php';
if(!function_exists('xhprof_error')) {
function xhprof_error($out) {
if (!function_exists('xhprof_error')) {
function xhprof_error($out)
{
echo substr($out, 0, 300) . '...';
}
}
$currentGitBranch = SettingsPiwik::getCurrentGitBranch();
$profilerNamespace = "piwik";
if($currentGitBranch != 'master') {
$profilerNamespace .= "." . $currentGitBranch;
if ($currentGitBranch != 'master') {
$profilerNamespace .= "-" . $currentGitBranch;
}
if ($mainRun) {
self::setProfilingRunIds(array());
}
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
if($mainRun) {
self::setProfilingRunIds(array());
}
register_shutdown_function(function () use($profilerNamespace, $mainRun) {
register_shutdown_function(function () use ($profilerNamespace, $mainRun) {
$xhprofData = xhprof_disable();
$xhprofRuns = new \XHProfRuns_Default();
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, $profilerNamespace);
if(empty($runId)) {
if (empty($runId)) {
die('could not write profiler run');
}
$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);
$runs = Profiler::getProfilingRunIds();
array_unshift($runs, $runId);
if ($mainRun) {
Profiler::aggregateXhprofRuns($runs, $profilerNamespace, $saveTo = $runId);
$baseUrlStored = SettingsPiwik::getPiwikUrl();
$out = "\n\n";
$baseUrl = "http://" . @$_SERVER['HTTP_HOST'] . "/" . @$_SERVER['REQUEST_URI'];
$baseUrlStored = SettingsPiwik::getPiwikUrl();
if(strlen($baseUrlStored) > strlen($baseUrl)) {
if (strlen($baseUrlStored) > strlen($baseUrl)) {
$baseUrl = $baseUrlStored;
}
$baseUrl = "\n" . $baseUrl
."tests/lib/xhprof-0.9.4/xhprof_html/?source=$profilerNamespace&run=";
$baseUrl = $baseUrlStored . "vendor/facebook/xhprof/xhprof_html/?source=$profilerNamespace&run=$runId";
$out .= "Profiler report is available at:";
$out .= $baseUrl . $runId;
if($runId != $runIds) {
$out .= "\n\nProfiler Report aggregating all runs triggered from this process: ";
$out .= $baseUrl . $runIds;
}
$out .= "Profiler report is available at:\n";
$out .= "<a href='$baseUrl'>$baseUrl</a>";
$out .= "\n\n";
echo ($out);
if (Development::isEnabled()) {
$out .= "WARNING: Development mode is enabled. Many runtime optimizations are not applied in development mode. ";
$out .= "Unless you intend to profile Piwik in development mode, your profile may not be accurate.";
$out .= "\n\n";
}
echo $out;
} else {
self::setProfilingRunIds($runs);
Profiler::setProfilingRunIds($runs);
}
});
self::$isXhprofSetup = true;
}
private static function setProfilingRunIds($ids)
/**
* Aggregates xhprof runs w/o normalizing (xhprof_aggregate_runs will always average data which
* does not fit Piwik's use case).
*/
public static function aggregateXhprofRuns($runIds, $profilerNamespace, $saveToRunId)
{
file_put_contents( self::getPathToXHProfRunIds(), json_encode($ids) );
$xhprofRuns = new XHProfRuns_Default();
$aggregatedData = array();
foreach ($runIds as $runId) {
$xhprofRunData = $xhprofRuns->get_run($runId, $profilerNamespace, $description);
foreach ($xhprofRunData as $key => $data) {
if (empty($aggregatedData[$key])) {
$aggregatedData[$key] = $data;
} else {
// don't aggregate main() metrics since only the super run has the correct metrics for the entire run
if ($key == "main()") {
continue;
}
$aggregatedData[$key]["ct"] += $data["ct"]; // call count
$aggregatedData[$key]["wt"] += $data["wt"]; // incl. wall time
$aggregatedData[$key]["cpu"] += $data["cpu"]; // cpu time
$aggregatedData[$key]["mu"] += $data["mu"]; // memory usage
$aggregatedData[$key]["pmu"] = max($aggregatedData[$key]["pmu"], $data["pmu"]); // peak mem usage
}
}
}
$xhprofRuns->save_run($aggregatedData, $profilerNamespace, $saveToRunId);
}
public static function setProfilingRunIds($ids)
{
file_put_contents(self::getPathToXHProfRunIds(), json_encode($ids));
@chmod(self::getPathToXHProfRunIds(), 0777);
}
private static function getProfilingRunIds()
public static function getProfilingRunIds()
{
$runIds = file_get_contents( self::getPathToXHProfRunIds() );
$runIds = file_get_contents(self::getPathToXHProfRunIds());
$array = json_decode($runIds, $assoc = true);
if(!is_array($array)) {
if (!is_array($array)) {
$array = array();
}
return $array;