Skip to content

Commit

Permalink
Tideways compat (preinheimer#98)
Browse files Browse the repository at this point in the history
Added Tideways (xhprof fork) support
  • Loading branch information
tperalta82 authored and Alexander Obuhovich committed Oct 12, 2016
1 parent 6abb0b0 commit 507344a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
8 changes: 4 additions & 4 deletions external/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
define('XHPROF_LIB_ROOT', dirname(dirname(__FILE__)) . '/xhprof_lib');
}

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$isAjax = true;
}

if (extension_loaded('xhprof') && $_xhprof['doprofile'] === true) {
if ($_xhprof['ext_name'] && $_xhprof['doprofile'] === true) {
$profiler_namespace = $_xhprof['namespace']; // namespace for your application
$xhprof_data = xhprof_disable();
$xhprof_data = call_user_func($_xhprof['ext_name'].'_disable');
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $profiler_namespace, null, $_xhprof);
if ($_xhprof['display'] === true && PHP_SAPI != 'cli' && !isset($isAjax))
{
// url to the XHProf UI libraries (change the host name and path)
$profiler_url = sprintf($_xhprof['url'].'/index.php?run=%s&source=%s', $run_id, $profiler_namespace);
echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
echo '<a href="'. $profiler_url .'" target="_blank">Profiler output</a>';
}
}
56 changes: 44 additions & 12 deletions external/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@

include(dirname(__FILE__) . '/../xhprof_lib/config.php');

function getExtensionName()
{
if (extension_loaded('tideways'))
{
return 'tideways';
}elseif(extension_loaded('xhprof')) {
return 'xhprof';
}
return false;
}
$_xhprof['ext_name'] = getExtensionName();
if($_xhprof['ext_name'])
{
$flagsCpu = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_CPU');
$flagsMemory = constant(strtoupper($_xhprof['ext_name']).'_FLAGS_MEMORY');
$envVarName = strtoupper($_xhprof['ext_name']).'_PROFILE';
}


//I'm Magic :)
class visibilitator
{
Expand Down Expand Up @@ -34,21 +53,30 @@ public static function __callstatic($name, $arguments)
// Only users from authorized IP addresses may control Profiling
if ($controlIPs === false || in_array($_SERVER['REMOTE_ADDR'], $controlIPs) || PHP_SAPI == 'cli')
{
if (isset($_GET['_profile']))
/* Backwards Compatibility getparam check*/
if (!isset($_xhprof['getparam']))
{
$_xhprof['getparam'] = '_profile';
}

if (isset($_GET[$_xhprof['getparam']]))
{
//Give them a cookie to hold status, and redirect back to the same page
setcookie('_profile', $_GET['_profile']);
$newURI = str_replace(array('_profile=1','_profile=0'), '', $_SERVER['REQUEST_URI']);
setcookie('_profile', $_GET[$_xhprof['getparam']]);
$newURI = str_replace(array($_xhprof['getparam'].'=1',$_xhprof['getparam'].'=0'), '', $_SERVER['REQUEST_URI']);
header("Location: $newURI");
exit;
}

if (isset($_COOKIE['_profile']) && $_COOKIE['_profile'] || PHP_SAPI == 'cli' && ((isset($_SERVER['XHPROF_PROFILE']) && $_SERVER['XHPROF_PROFILE']) || (isset($_ENV['XHPROF_PROFILE']) && $_ENV['XHPROF_PROFILE'])))

if (isset($_COOKIE['_profile']) && $_COOKIE['_profile']
|| PHP_SAPI == 'cli' && ( (isset($_SERVER[$envVarName]) && $_SERVER[$envVarName])
|| (isset($_ENV[$envVarName]) && $_ENV[$envVarName])))
{
$_xhprof['display'] = true;
$_xhprof['doprofile'] = true;
$_xhprof['type'] = 1;
}
unset($envVarName);
}


Expand Down Expand Up @@ -112,20 +140,24 @@ public static function __callstatic($name, $arguments)
unset($domain);

//Display warning if extension not available
if (extension_loaded('xhprof') && $_xhprof['doprofile'] === true) {
if ($_xhprof['ext_name'] && $_xhprof['doprofile'] === true) {
include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_lib.php';
include_once dirname(__FILE__) . '/../xhprof_lib/utils/xhprof_runs.php';
if (isset($ignoredFunctions) && is_array($ignoredFunctions) && !empty($ignoredFunctions)) {
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY, array('ignored_functions' => $ignoredFunctions));
if (isset($ignoredFunctions) && is_array($ignoredFunctions) && !empty($ignoredFunctions)) {
call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory, array('ignored_functions' => $ignoredFunctions));
} else {
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
call_user_func($_xhprof['ext_name'].'_enable', $flagsCpu + $flagsMemory);
}
}elseif(!extension_loaded('xhprof') && $_xhprof['display'] === true)
unset($flagsCpu);
unset($flagsMemory);

}elseif(false === $_xhprof['ext_name'] && $_xhprof['display'] === true)
{
$message = 'Warning! Unable to profile run, xhprof extension not loaded';
$message = 'Warning! Unable to profile run, tideways or xhprof extension not loaded';
trigger_error($message, E_USER_WARNING);
}

unset($flagsCpu);
unset($flagsMemory);
function xhprof_shutdown_function() {
global $_xhprof;
require dirname(__FILE__).'/footer.php';
Expand Down
2 changes: 2 additions & 0 deletions xhprof_lib/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
$_xhprof['servername'] = 'myserver';
$_xhprof['namespace'] = 'myapp';
$_xhprof['url'] = 'http://url/to/xhprof/xhprof_html';
$_xhprof['getparam'] = "_profile";

/*
* MySQL/MySQLi/PDO ONLY
* Switch to JSON for better performance and support for larger profiler data sets.
Expand Down

0 comments on commit 507344a

Please sign in to comment.