ddev-contrib icon indicating copy to clipboard operation
ddev-contrib copied to clipboard

Added xhgui service.

Open penyaskito opened this issue 4 years ago • 12 comments

The New Solution/Problem/Issue/Bug:

xhprof+XHGui is useful for profiling applications.

How this PR Solves The Problem:

With the code provided + following the instructions, it's easy to include xhprof in your application for profiling an application. It provides the instructions for installing xhprof, provides a container for xhprof app itself and for the required xhprof mongodb application who stores the data. Provided instructions for Drupal 8+, WordPress, Bedrock and general php apps.

Manual Testing Instructions:

Follow the README.md (https://github.com/penyaskito/ddev-contrib/tree/xhgui-service/docker-compose-services/xhgui) with a new app, e.g. for WordPress:

wget https://wordpress.org/latest.tar.gz
tar -xvf latest.tar.gz
mv wordpress xhguitest
cd xhguitest
ddev config
vim .ddev/config.yaml
*** EDIT config as instructed in the README.md ***
cp -r ~/Projects/ddev-contrib/docker-compose-services/xhgui/xhgui-mongo .ddev
cp -r ~/Projects/ddev-contrib/docker-compose-services/xhgui/xhgui .ddev
cp ~/Projects/ddev-contrib/docker-compose-services/xhgui/docker-compose.xhgui.yml .ddev
cp ~/Projects/ddev-contrib/docker-compose-services/xhgui/examples/* .
wget https://github.com/perftools/php-profiler/archive/refs/tags/0.18.0.tar.gz
tar -xvf 0.18.0.tar.gz
vim wp-config-ddev.php
*** EDIT config as instructed in the README.md ***
ddev start
vim wp-config-ddev.php
*** EDIT config as instructed in the README.md ***
*** Open https://xhguitest.ddev.site in your browser. *** 
*** Open http://xhguitest.ddev.site:8282 in your browser. ***

Related Issue Link(s):

None known.

penyaskito avatar May 01 '21 16:05 penyaskito

I think this is a great addition for ddev-contrib. I followed the recipe and it works! For core ddev I think we need something more streamlined (e.g. no mongo and no other deps), but this certainly is nicely usable.

LionsAd avatar May 28 '21 20:05 LionsAd

After https://github.com/drud/ddev/pull/2983 goes in (preferably after it's in a release) we can revisit this since there's a lot added there now.

rfay avatar Jun 16 '21 17:06 rfay

What do you think about this now @penyaskito ?

rfay avatar Aug 14 '21 00:08 rfay

Thanks for all the effort.

For anyone who wants to use this with xhprof of ddev v1.17.7.

ddev ssh
vi /usr/local/bin/xhprof_prepend.php 

comment out these lines

    xhprof_enable();
    register_shutdown_function('xhprof_completion');

and save.

Otherwise the shutdown function xhprof_completion will first call xhprof_disable() and afterwards the profiler for xhgui which would get null as return.

Commenting out the above lines will disable the logging for mysite.ddev.site/xhprof. And the change will be flushed after restarting ddev (there is probably a more consistent solution, but I'm not so used to ddev yet).

glingener avatar Aug 31 '21 19:08 glingener

In current prereleases of v1.18, the xhprof_prepend.php is actually in .ddev/xhprof/xhprof_prepend.php, so you can remove the #ddev-generated from that file and change it to your heart's delight. It will be there even after a restart. Anyway, I would love it if you'd try out latest release which is a bit different from what you've been experimenting with.

rfay avatar Aug 31 '21 20:08 rfay

Thank your @rfay. I've installed v1.18 and it works great.

I followed this guide (without the need of adding webimage_extra_packages: [php7.4-xhprof] to the .ddev/config.yaml).

Than I adjusted the .ddev/xhprof/xhprof_prepend.php

  • Removed the #ddev-generated to keep my changes
  • And I put the xhprof enable logic into a function
    function xhprof_log() {
      if (extension_loaded('xhprof') && strpos($uri, '/xhprof') === false) {
        // If this is too much information, just use xhprof_enable(), which shows CPU only
        xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
        register_shutdown_function('xhprof_completion');
      }
    }
    

And adjusted the logic, provided by this guide, to run xhgui from

require_once __DIR__ . '/php-profiler-0.18.0/autoload.php';
require_once __DIR__ . '/xhgui.collector.php';

to

function xhgui_log() {
	if ( extension_loaded('xhprof') ) {
		require_once __DIR__ . '/php-profiler-0.18.0/autoload.php';
		require_once __DIR__ . '/xhgui.collector.php';
	}
}

// DECIDE BETWEEN ("ddev xhprof on" is required for both, otherwise nothing happens.)
// xhprof_log(); // https://my.ddev.site/xhprof/
xhgui_log(); 	 // http://my.ddev.site:8282/

in wp-config-ddev.php (using Wordpress).

That allows me to switch between both ways quickly and I can keep xhgui_log() present there as it does not run before running ddev xhprof on.

There's probably a nicer solution by adding some custom command ddev xhgui on but it's fine for me this way.

glingener avatar Sep 01 '21 11:09 glingener

Sorry I didn't get to this yet @rfay. I'll look at this ASAP and will take @glingener suggestions into account. Ideally we should be able to read the already generated reports with xhprof from xhgui, so no changes should be necessary. Modern xhgui only supports mongo as data storage, so might need another container for importing the files there via crontab or something like that.

penyaskito avatar Sep 01 '21 12:09 penyaskito

@glingener you won't want to be altering wp-config-ddev.php I don't think - I imagine you'll want to find a place to make changes before or after that, as that file is #ddev-generated, right? Congrats on getting what you like running.

It's possible to add more into the default xhprof_prepend.php as well - as you see there's already an example there to add a link to the bottom of page. Probably works in WP.

rfay avatar Sep 01 '21 14:09 rfay

It's #ddev-generated, but so far I saw no reason not removing that for the wp-config-ddev.php.

But I like your approach better and moved all the files for xhgui (incl. the profiler) to the .ddev dir and just load it via the xhprof_prepend.php as you said. Now my wordpress root is clean again. Thank you.

glingener avatar Sep 01 '21 18:09 glingener

I'm fine with going forward with this if anybody has the energy to maintain it, but I think the existing ddev xhprof command without extra config probably took the wind out of it. Closing now, but happy to reopen and pursue it.

rfay avatar Oct 25 '21 22:10 rfay

I think it might be time to resurrect this.

rfay avatar Mar 29 '23 12:03 rfay

I noticed this has been coming up alot recently. I started an outline of the an addon but have yet to connect the service to a (Drupal) framework.

https://github.com/tyler36/ddev-xhgui

tyler36 avatar Mar 29 '23 23:03 tyler36