Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve xhprof availability and revert to just using the classic run list page #3087

Merged
merged 12 commits into from Jul 6, 2021
2 changes: 1 addition & 1 deletion containers/ddev-webserver/Dockerfile
Expand Up @@ -127,7 +127,7 @@ RUN chmod -R ugo+w /usr/sbin /usr/bin /etc/nginx /var/cache/nginx /var/lib/nginx

RUN source /tmp/ddev/vars && curl -sSL https://github.com/drud/mkcert/releases/download/${MKCERT_VERSION}/mkcert-${MKCERT_VERSION}-${MKCERT_ARCH} -o /usr/local/bin/mkcert && chmod 777 /usr/local/bin/mkcert

RUN mkdir -p /var/www/xhprof && curl -o /tmp/xhprof.tgz -sSL https://pecl.php.net/get/xhprof && tar -zxf /tmp/xhprof.tgz --strip-components=1 -C /var/www/xhprof
RUN mkdir -p /var/www/xhprof && curl -o /tmp/xhprof.tgz -sSL https://pecl.php.net/get/xhprof && tar -zxf /tmp/xhprof.tgz --strip-components=1 -C /var/www/xhprof && chmod 777 /var/www/xhprof/xhprof_html

RUN touch /var/log/nginx/error.log /var/log/nginx/access.log /var/log/php-fpm.log && \
chmod 666 /var/log/nginx/error.log /var/log/nginx/access.log /var/log/php-fpm.log
Expand Down
@@ -1,7 +1,8 @@
# Handle xhprof queries if there are any
location ^~ /xhprof {
absolute_redirect off;
alias /var/www/xhprof/xhprof_html;
index index.php;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm.sock;
Expand Down
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
@@ -1,4 +1,3 @@
extension=xhprof.so
xhprof.output_dir=/tmp/xhprof
auto_prepend_file=/usr/local/bin/xhprof_prepend.php
auto_append_file=/usr/local/bin/xhprof_append.php
Expand Up @@ -4,4 +4,6 @@ phpdismod blackfire xdebug
mkdir -p ${XHPROF_OUTPUT_DIR}
phpenmod xhprof
killall -USR2 php-fpm 2>/dev/null || true
echo "Enabled xhprof"
echo "Enabled xhprof.
After each web request you can see all runs, most recent first, at
${DDEV_PRIMARY_URL}/xhprof"

This file was deleted.

@@ -1,4 +1,25 @@
<?php
if (extension_loaded('xhprof')) {

$uri = "none";
if (!empty($_SERVER) && array_key_exists('REQUEST_URI', $_SERVER)) {
$uri = $_SERVER['REQUEST_URI'];
}

// Enable xhprof profiling if we're not on an xhprof page
if (extension_loaded('xhprof') && strpos($uri, '/xhprof') === false) {
xhprof_enable();
register_shutdown_function('xhprof_completion');
}

// Write to the xhprof_html output and latest on completion
function xhprof_completion() {
$xhprof_link_dir = "/var/www/xhprof/xhprof_html/latest/";

$xhprof_data = xhprof_disable();
$appNamespace = "ddev";
include_once '/var/www/xhprof/xhprof_lib/utils/xhprof_lib.php';
include_once '/var/www/xhprof/xhprof_lib/utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, $appNamespace);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want the ability for a dev user to supply their own script here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have been thinking about that, and definitely agree. Can go so very far into the weeds. I've also thought about having the script optionally add the link at the bottom. But mucking with the output breaks all APIs and all command-line scripts and lots of web pages (like TYPO3).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cool to have the shutdown function include a file which was easily editable by the user, perhaps empty by default.

}
@@ -1,6 +1,5 @@
# ddev default (PHP project type) config

#ddev-generated
# If you want to take over this file and customize it, remove the line above
# and ddev will respect it and won't overwrite the file.
# See https://ddev.readthedocs.io/en/stable/users/extend/customization-extendibility/#providing-custom-nginx-configuration
Expand Down Expand Up @@ -88,7 +87,7 @@ server {
# Handle xhprof queries if there are any
location ^~ /xhprof {
alias /var/www/xhprof/xhprof_html;
index index.php;
index index.php index.html;

location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm.sock;
Expand Down
13 changes: 8 additions & 5 deletions docs/users/xhprof-profiling.md
Expand Up @@ -4,11 +4,14 @@ DDEV-Local has built-in support for [xhprof](https://www.php.net/manual/en/book.

### Basic xhprof Usage

* Enable xhprof with `ddev xhprof enable` (or `ddev xhprof` or `ddev xhprof on`) and see status with `ddev xhprof status`
* Use a web browser or other technique to visit a page whose performance you want to study.
* On some CMSs, there will be a link to "xhprof profiler output" so you can study the page. (For example, on Drupal 7 at least with the default theme, it's at the bottom of the page, on TYPO3 v10 it's in the upper left.) On other CMSs the output is suppressed and you'll need to visit `/xhprof/` (for example, `https://project.ddev.site/xhprof/`) and click the first listed link.
* Enable xhprof with `ddev xhprof on` (or `ddev xhprof` or `ddev xhprof enable`) and see status with `ddev xhprof status`
* `ddev xhprof on` will show you the URL you can use to see the xhprof analysis, `https://<projectname>.ddev.site/xhprof` shows recent runs. It's often useful to just have a tab or window open with this URL and refresh it as needed.
* Use a web browser or other technique to visit a page whose performance you want to study. To eliminate first-time cache-building issues, you may want to hit it twice.
* Visit one of the links provided by `ddev xhprof on` and study the results.
* On the profiler output page you can drill down to the function that you want to study, or use the graphical "View Full Callgraph" link. Click the column headers to sort by number of runs and inclusive or exclusive wall time, then drill down into the function you really want to study and do the same.
* Visit `<project_url>/xhprof/` (for example, `https://project.ddev.site/xhprof/`) to see and study all the runs you have captured. (These are erased on `ddev restart`.)
* The runs are erased on `ddev restart`.
* If you are using webserver_type apache-fpm and you have a custom .ddev/apache/apache-site.conf, you'll need to make sure it has the `Alias "/xhprof" "/var/www/xhprof/xhprof_html"` in it that the [provided apache-site.conf](https://github.com/drud/ddev/blob/master/pkg/ddevapp/webserver_config_assets/apache-site-php.conf) has.

For a tutorial on how to study the various xhprof reports, see the section "How to use XHPROF UI" in [A Guide to Profiling with XHPROF](https://inviqa.com/blog/profiling-xhprof). It takes a little time to get your eyes used to the reporting. (You do not need to do any of the installation described in that article, of course.)

Some early versions of xhprof often added a link to the bottom of a web page, but that approach breaks all pages that aren't simple html, including command-line invocations, json or other API results, and even breaks pages that consume json themselves.
2 changes: 1 addition & 1 deletion pkg/version/version.go
Expand Up @@ -40,7 +40,7 @@ var DockerComposeFileFormatVersion = "3.6"
var WebImg = "drud/ddev-webserver"

// WebTag defines the default web image tag for drud dev
var WebTag = "20210628_lang_utf8" // Note that this can be overridden by make
var WebTag = "20210703_improve_xhprof" // Note that this can be overridden by make

// DBImg defines the default db image used for applications.
var DBImg = "drud/ddev-dbserver"
Expand Down