Skip to content

Commit 7e66557

Browse files
danepowellweitzman
authored andcommitted
Compatibility with new Tideways XHProf extension. (#3707)
* Compatibility with new Tideways XHProf extension. * Code standard cleanup. * Get tmp dir from Drush config. * Added additional documentation about tideways_xhprof.
1 parent 0c59d31 commit 7e66557

File tree

1 file changed

+55
-8
lines changed

1 file changed

+55
-8
lines changed

src/Commands/core/XhprofCommands.php

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Drush\Commands\DrushCommands;
1010

11+
/**
12+
* Class XhprofCommands
13+
* @package Drush\Commands\core
14+
*
15+
* Supports profiling Drush commands using either XHProf or Tideways XHProf.
16+
*
17+
* Note that XHProf is only compatible with PHP 5.6. For PHP 7+, you must use
18+
* the Tideways XHProf fork. The Tideways XHProf extension recently underwent a
19+
* major refactor; Drush is only compatible with the newer version.
20+
* @see https://tideways.com/profiler/blog/releasing-new-tideways-xhprof-extension
21+
*
22+
* @todo Remove support for XHProf extension once PHP 5.6 is EOL.
23+
*/
1124
class XhprofCommands extends DrushCommands
1225
{
1326

@@ -37,10 +50,7 @@ public function xhprofPost($result, CommandData $commandData)
3750
{
3851
if (self::xhprofIsEnabled()) {
3952
$namespace = 'Drush';
40-
$xhprof_data = xhprof_disable();
41-
$xhprof_runs = new \XHProfRuns_Default();
42-
$run_id = $xhprof_runs->save_run($xhprof_data, $namespace);
43-
$namespace = 'Drush';
53+
$run_id = self::xhprofFinishRun($namespace);
4454
$url = Drush::config()->get('xh.link') . '/index.php?run=' . urlencode($run_id) . '&source=' . urlencode($namespace);
4555
$this->logger()->notice(dt('XHProf run saved. View report at !url', ['!url' => $url]));
4656
}
@@ -53,25 +63,32 @@ public function xhprofPost($result, CommandData $commandData)
5363
*/
5464
public function xhprofInitialize(InputInterface $input, AnnotationData $annotationData)
5565
{
56-
if (self::xhprofIsEnabled($input)) {
66+
if (self::xhprofIsEnabled()) {
5767
$config = Drush::config()->get('xh');
5868
$flags = self::xhprofFlags($config);
59-
\xhprof_enable($flags);
69+
self::xhprofEnable($flags);
6070
}
6171
}
6272

6373
public static function xhprofIsEnabled()
6474
{
6575
if (Drush::config()->get('xh.link')) {
66-
if (!extension_loaded('xhprof') && !extension_loaded('tideways')) {
67-
throw new \Exception(dt('You must enable the xhprof or tideways PHP extensions in your CLI PHP in order to profile.'));
76+
if (!extension_loaded('xhprof') && !extension_loaded('tideways_xhprof')) {
77+
if (extension_loaded('tideways')) {
78+
throw new \Exception(dt('You are using an older incompatible version of the tideways extension. Please upgrade to the new tideways_xhprof extension.'));
79+
} else {
80+
throw new \Exception(dt('You must enable the xhprof or tideways_xhprof PHP extensions in your CLI PHP in order to profile.'));
81+
}
6882
}
6983
return true;
7084
}
85+
return false;
7186
}
7287

7388
/**
7489
* Determines flags.
90+
*
91+
* TODO: Make these work for Tideways as well.
7592
*/
7693
public static function xhprofFlags(array $config)
7794
{
@@ -87,4 +104,34 @@ public static function xhprofFlags(array $config)
87104
}
88105
return $flags;
89106
}
107+
108+
/**
109+
* Enable profiling.
110+
*/
111+
public static function xhprofEnable($flags)
112+
{
113+
if (extension_loaded('tideways_xhprof')) {
114+
\tideways_xhprof_enable(TIDEWAYS_XHPROF_FLAGS_MEMORY | TIDEWAYS_XHPROF_FLAGS_CPU);
115+
} else {
116+
\xhprof_enable($flags);
117+
}
118+
}
119+
120+
/**
121+
* Disable profiling and save results.
122+
*/
123+
public function xhprofFinishRun($namespace)
124+
{
125+
if (extension_loaded('tideways_xhprof')) {
126+
$data = \tideways_xhprof_disable();
127+
$dir = $this->getConfig()->tmp();
128+
$run_id = uniqid();
129+
file_put_contents($dir . DIRECTORY_SEPARATOR . $run_id . '.' . $namespace . '.xhprof', serialize($data));
130+
return $run_id;
131+
} else {
132+
$xhprof_data = \xhprof_disable();
133+
$xhprof_runs = new \XHProfRuns_Default();
134+
return $xhprof_runs->save_run($xhprof_data, $namespace);
135+
}
136+
}
90137
}

0 commit comments

Comments
 (0)