Skip to content

Commit 0b83078

Browse files
Merge pull request #6 from stasadev/20241028_stasadev_xhprof_cpu
Add XHPROF_FLAGS_CPU for xhprof, cleanup for `ddev get`
2 parents be663a9 + c5e8bda commit 0b83078

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

install.yaml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
name: ddev-buggregator
22

33
project_files:
4-
- docker-compose.buggregator.yaml
4+
- docker-compose.buggregator.yaml
5+
- xhprof/xhprof_prepend.php
6+
7+
# needed for xhprof/xhprof_prepend.php
8+
ddev_version_constraint: '>= v1.23.2'

tests/test.bats

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ setup() {
44
export TESTDIR=~/tmp/test-buggregator
55
mkdir -p $TESTDIR
66
export PROJNAME=test-buggregator
7-
export DDEV_NON_INTERACTIVE=true
7+
export DDEV_NONINTERACTIVE=true
88
ddev delete -Oy ${PROJNAME} >/dev/null 2>&1 || true
99
cd "${TESTDIR}"
1010
ddev config --project-name=${PROJNAME}
@@ -13,8 +13,7 @@ setup() {
1313

1414
health_checks() {
1515
# Do something useful here that verifies the add-on
16-
# ddev exec "curl -s elasticsearch:9200" | grep "${PROJNAME}-elasticsearch"
17-
ddev exec "curl -s https://localhost:443/"
16+
ddev exec "curl -s http://buggregator:8000/"
1817
}
1918

2019
teardown() {
@@ -27,17 +26,18 @@ teardown() {
2726
@test "install from directory" {
2827
set -eu -o pipefail
2928
cd ${TESTDIR}
30-
echo "# ddev get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
31-
ddev get ${DIR}
29+
echo "# ddev add-on get ${DIR} with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
30+
ddev add-on get ${DIR}
3231
ddev restart
3332
health_checks
3433
}
3534

35+
# bats test_tags=release
3636
@test "install from release" {
3737
set -eu -o pipefail
3838
cd ${TESTDIR} || ( printf "unable to cd to ${TESTDIR}\n" && exit 1 )
39-
echo "# ddev get iljapolanskis/ddev-buggregator with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
40-
ddev get iljapolanskis/ddev-buggregator
39+
echo "# ddev add-on get iljapolanskis/ddev-buggregator with project ${PROJNAME} in ${TESTDIR} ($(pwd))" >&3
40+
ddev add-on get iljapolanskis/ddev-buggregator
4141
ddev restart >/dev/null
4242
health_checks
4343
}

xhprof/xhprof_prepend.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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&amp;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

Comments
 (0)