Skip to content

Instantly share code, notes, and snippets.

@vpnwall-services
Last active January 6, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpnwall-services/6159c61af46228f808967fc8e5328034 to your computer and use it in GitHub Desktop.
Save vpnwall-services/6159c61af46228f808967fc8e5328034 to your computer and use it in GitHub Desktop.
[DEBUG PHP MEMORY] Debug PHP Memory #bash #php #debug #xdebug #memory
#!/bin/bash
apt install php7.4-xdebug
echo "xdebug.trace_enable_trigger = 1" >> /etc/php7.4/mods-available/xdebug.ini
echo "xdebug.trace_format = 1" >> /etc/php7.4/mods-available/xdebug.ini
service php7.4-fpm restart
curl -L https://myurl.local/index.php?XDEBUG_TRACE=1
cat /tmp/trace*
#https://lonewolfonline.net/xdebug/

When installed => open ssh tunnel and visit => http://localhost.xhprof/xhprof_html/ (xhprof is localhost only)

Install xhprof and graphviz

apt install -y php7.2-xhprof /var/www/myplatform/

Put localhost xhprof in /etc/hosts

127.0.0.1 localhost.xhprof

Enable xhprof extension

phpenmod -v 7.2 xhprof

Create new vhost (localhost only) for xhgui

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/xhprof
    ServerName localhost.xhprof
  Include conf-available/php7.2-fpm-www.conf
    <Directory /var/www/xhprof>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
</VirtualHost>

Clone xhprof files to new vhost directory

git clone https://github.com/preinheimer/xhprof.git`

Create new database for xhprof

database, user and password, ect...

Inject this data in database

CREATE TABLE `details` 
             ( 
                          `id`    CHAR(17) NOT NULL, 
                          `url`   VARCHAR(255) DEFAULT NULL, 
                          `c_url` VARCHAR(255) DEFAULT NULL, 
                          `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP on 
             UPDATE CURRENT_TIMESTAMP, 
                    `server name` VARCHAR(64) DEFAULT NULL, 
                    `perfdata` MEDIUMBLOB, 
                    `type` TINYINT(4) DEFAULT NULL, 
                    `cookie` BLOB, 
                    `post` BLOB, 
                    `get` BLOB, 
                    `pmu`                    INT(11) UNSIGNED DEFAULT NULL, 
                    `wt`                     INT(11) UNSIGNED DEFAULT NULL, 
                    `cpu`                    INT(11) UNSIGNED DEFAULT NULL, 
                    `server_id`              CHAR(3) NOT NULL DEFAULT 't11', 
                    `aggregatecalls_include` VARCHAR(255) DEFAULT NULL, 
                    PRIMARY KEY (`id`), 
                    KEY `url` (`url`), 
                    KEY `c_url` (`c_url`), 
                    KEY `cpu` (`cpu`), 
                    KEY `wt` (`wt`), 
                    KEY `pmu` (`pmu`), 
                    KEY `timestamp` (`timestamp`) 
             ) 
             engine=myisam DEFAULT charset=utf8;

Put mysql database details in xhprof config.php (/var/www/xhprof/xhprof_lib/config.php)

<?php
$_xhprof = array();

// Change these:
$_xhprof['dbtype'] = 'mysql'; // Only relevant for PDO
$_xhprof['dbhost'] = 'xxx';
$_xhprof['dbuser'] = 'xhprof';
$_xhprof['dbpass'] = 'xxx';
$_xhprof['dbname'] = 'xhprof';
$_xhprof['dbadapter'] = 'Mysqli';
$_xhprof['servername'] = 'wpb';
$_xhprof['namespace'] = 'wpb';
$_xhprof['url'] = 'https://xxxxxx/xhprof/xhprof_html';
$_xhprof['getparam'] = "_profile";
/*
 * MySQL/MySQLi/PDO ONLY
 * Switch to JSON for better performance and support for larger profiler data sets.
 * WARNING: Will break with existing profile data, you will need to TRUNCATE the profile data table.
 */
$_xhprof['serializer'] = 'php';

//Uncomment one of these, platform dependent. You may need to tune for your specific environment, but they're worth a try

//These are good for Windows
/*
$_xhprof['dot_binary']  = 'C:\\Programme\\Graphviz\\bin\\dot.exe';
$_xhprof['dot_tempdir'] = 'C:\\WINDOWS\\Temp';
$_xhprof['dot_errfile'] = 'C:\\WINDOWS\\Temp\\xh_dot.err';
*/

//These are good for linux and its derivatives.

$_xhprof['dot_binary']  = '/usr/bin/dot';
$_xhprof['dot_tempdir'] = '/tmp';
$_xhprof['dot_errfile'] = '/tmp/xh_dot.err';

$ignoreURLs = array();
// Do not track URIs containing xhprof
$ignoreURLs[] = "/xhprof/";

$ignoreDomains = array();

$exceptionURLs = array();

$ignoreDomains = array();

$exceptionURLs = array();

$exceptionPostURLs = array();
$exceptionPostURLs[] = "login";

$_xhprof['display'] = false;
$_xhprof['doprofile'] = false;

//Control IPs allow you to specify which IPs will be permitted to control when profiling is on or off within your application, and view the results via the UI.

$controlIPs = false; //Disables access controls completely.

//$controlIPs = array();
//$controlIPs[] = "127.0.0.1";   // localhost, you'll want to add your own ip here
//$controlIPs[] = "::1";         // localhost IP v6

//$otherURLS = array();

// ignore builtin functions and call_user_func* during profiling
//$ignoredFunctions = array('call_user_func', 'call_user_func_array', 'socket_select');

//Default weight - can be overidden by an Apache environment variable 'xhprof_weight' for domain-specific values
$weight = 100;

if($domain_weight = getenv('xhprof_weight')) {
	$weight = $domain_weight;
}

unset($domain_weight);

  /**
  * The goal of this function is to accept the URL for a resource, and return a "simplified" version
  * thereof. Similar URLs should become identical. Consider:
  * http://example.org/stories.php?id=2323
  * http://example.org/stories.php?id=2324
  * Under most setups these two URLs, while unique, will have an identical execution path, thus it's
  * worthwhile to consider them as identical. The script will store both the original URL and the
  * Simplified URL for display and comparison purposes. A good simplified URL would be:
  * http://example.org/stories.php?id=
  * 
  * @param string $url The URL to be simplified
  * @return string The simplified URL 
  */
  function _urlSimilartor($url)
  {
      //This is an example 
      $url = preg_replace("!\d{4}!", "", $url);
      
      // For domain-specific configuration, you can use Apache setEnv xhprof_urlSimilartor_include [some_php_file]
      if($similartorinclude = getenv('xhprof_urlSimilartor_include')) {
      	require_once($similartorinclude);
      }
      
      $url = preg_replace("![?&]_profile=\d!", "", $url);
      return $url;
  }
  
  function _aggregateCalls($calls, $rules = null)
  {
    $rules = array(
        'Loading' => 'load::',
        'mysql' => 'mysql_'
        );

    // For domain-specific configuration, you can use Apache setEnv xhprof_aggregateCalls_include [some_php_file]
  	if(isset($run_details['aggregateCalls_include']) && strlen($run_details['aggregateCalls_include']) > 1)
		{
    	require_once($run_details['aggregateCalls_include']);
		}        
        
    $addIns = array();
    foreach($calls as $index => $call)
    {
        foreach($rules as $rule => $search)
        {
            if (strpos($call['fn'], $search) !== false)
            {
                if (isset($addIns[$search]))
                {
                    unset($call['fn']);
                    foreach($call as $k => $v)
                    {
                        $addIns[$search][$k] += $v;
                    }
                }else
                {
                    $call['fn'] = $rule;
                    $addIns[$search] = $call;
                }
                unset($calls[$index]);  //Remove it from the listing
                break;  //We don't need to run any more rules on this
            }else
            {
                //echo "nomatch for $search in {$call['fn']}<br />\n";
            }
        }
    }
    return array_merge($addIns, $calls);
  }

Enable xhprof

Append this to /etc/php/7.2/fpm/php.ini and restart fpm auto_prepend_file="/var/www/xhprof/external/header.php";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment