|
| 1 | +<?php |
| 2 | + |
| 3 | +// #ddev-generated |
| 4 | + |
| 5 | +// Bugreggator throws an error with only xhprof_enable(XHPROF_FLAGS_MEMORY) (DDEV default). |
| 6 | +// This file uses xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY) to make it work. |
| 7 | +// See https://github.com/buggregator/server/issues/230 |
| 8 | + |
| 9 | +// If you want to take over and customize this file, remove the line above |
| 10 | +// And check this file in. |
| 11 | + |
| 12 | +// This file is used by `ddev xhprof on` and determines the behavior |
| 13 | +// of xhprof when it is enabled. It is mounted into the ddev-webserver container |
| 14 | +// as /usr/local/bin/xhprof/xhprof_prepend.php |
| 15 | + |
| 16 | +// It can be customized for particular sites or for particular CMS versions. |
| 17 | +// Some suggestions and examples are provided below. |
| 18 | + |
| 19 | +$uri = "none"; |
| 20 | +if (!empty($_SERVER) && array_key_exists('REQUEST_URI', $_SERVER)) { |
| 21 | + $uri = $_SERVER['REQUEST_URI']; |
| 22 | +} |
| 23 | + |
| 24 | +// Enable xhprof profiling if we're not on an xhprof page |
| 25 | +if (extension_loaded('xhprof') && strpos($uri, '/xhprof') === false) { |
| 26 | + // If this is too much information, just use xhprof_enable(), which shows CPU only |
| 27 | + xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); |
| 28 | + register_shutdown_function('xhprof_completion'); |
| 29 | +} |
| 30 | + |
| 31 | +// Write to the xhprof_html output and latest on completion |
| 32 | +function xhprof_completion() |
| 33 | +{ |
| 34 | + $xhprof_link_dir = "/var/xhprof/xhprof_html/latest/"; |
| 35 | + |
| 36 | + $xhprof_data = xhprof_disable(); |
| 37 | + $appNamespace = "ddev"; |
| 38 | + include_once '/var/xhprof/xhprof_lib/utils/xhprof_lib.php'; |
| 39 | + include_once '/var/xhprof/xhprof_lib/utils/xhprof_runs.php'; |
| 40 | + |
| 41 | + $xhprof_runs = new XHProfRuns_Default(); |
| 42 | + $run_id = $xhprof_runs->save_run($xhprof_data, $appNamespace); |
| 43 | + |
| 44 | + // Uncomment to append profile link to the page (and remove the ddev generated first line) |
| 45 | + // append_profile_link($run_id, $appNamespace); |
| 46 | +} |
| 47 | + |
| 48 | +// If invoked, this will append a profile link to the output HTML |
| 49 | +// This works on some CMSs, like Drupal 7. It does not work on Drupal8/9 |
| 50 | +// and can have unwanted side-effects on TYPO3 |
| 51 | +function append_profile_link($run_id, $appNamespace) |
| 52 | +{ |
| 53 | + $base_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]/xhprof/"; |
| 54 | + |
| 55 | + $profiler_url = sprintf('%sindex.php?run=%s&source=%s', $base_link, $run_id, $appNamespace); |
| 56 | + echo '<div id="xhprof"><a href="' . $profiler_url . '" target="_blank">xhprof profiler output</a></div>'; |
| 57 | +} |
0 commit comments