aboutsummaryrefslogtreecommitdiffstats
path: root/includes/profiler/ProfilerXhprof.php
diff options
context:
space:
mode:
authorOri Livneh <ori@wikimedia.org>2016-05-12 11:10:39 -0700
committerBryanDavis <bdavis@wikimedia.org>2016-05-12 22:23:05 +0000
commit20abb707ba23e0649a48a3ab0153c765e21b4ccf (patch)
treef726d8d3f16a4aeed8045ff98ccb083b9a10afec /includes/profiler/ProfilerXhprof.php
parent279dd4156c6195be16fe497980d73cd2e5c95884 (diff)
downloadmediawikicore-20abb707ba23e0649a48a3ab0153c765e21b4ccf.tar.gz
mediawikicore-20abb707ba23e0649a48a3ab0153c765e21b4ccf.zip
Decouple Xhprof profiling from profiling data processing
The motivation for this patch came from trying to use xhprof to profile the unit tests. I was able to profile specific test suites, but if I tried to profile a complete PHPUnit run, I ended up with empty profiling data. My initial suspicion was that this was due to some Xhprof buffer getting exhausted. The actual reason ended up being much simpler: the XhprofTest suite indirectly called xhprof_enable() / xhprof_disable(), which stopped xhprof and cleared out the data, so that when I was calling xhprof_disable() at the end of the run, there was no profiling data to return, because xhprof was not running. For the most part the XhprofTest was already doing the right thing by trying to avoid having side-effects or relying on xhprof. Wherever possible, test fixture profiling data was used in lieu of actually running xhprof. But this was not totally successful because the Xhprof class coupled the collection of data to the processing of data. Xhprof::__construct() called xhprof_enable(), so there was no real way around that. I think that the right way to fix that is to decouple profiling from profiling data analysis. Thus I renamed 'Xhprof' to 'XhprofData', and modified the class so that it expects to be fed profiling data rather than going out and collecting it on its own. As a result, it is now possible to profile a full phpunit run with xhprof, and the work that went into writing fixtures for the Xhprof unit tests pays off: the class and the tests no longer have a hard dependency on the xhprof extension, and the tests do not have to be skipped when it is not installed. And the tests are really testing the system under test, rather than the xhprof extension. Finally, I added a new Xhprof class, which really is just an extremely thin wrapper around xhprof_enable() / xhprof_disable(). The only extra functionality it provides is the ability to check whether xhprof is running, via Xhprof::isEnabled(). Calling Xhprof::enable() when it is already enabled will cause an exception to be thrown. This should help us avoid running into situations where two components contend for control of the profiler without realizing it. A unit test tests this behavior. The only part of this change that is not covered by tests is the change to ProfilerXhprof. I tested it manually and it works. Change-Id: Ica96beded68f04083abaf48ef1ae8c726eb60fa6
Diffstat (limited to 'includes/profiler/ProfilerXhprof.php')
-rw-r--r--includes/profiler/ProfilerXhprof.php24
1 files changed, 19 insertions, 5 deletions
diff --git a/includes/profiler/ProfilerXhprof.php b/includes/profiler/ProfilerXhprof.php
index 7c4fde40e8b9..8fc0b778da82 100644
--- a/includes/profiler/ProfilerXhprof.php
+++ b/includes/profiler/ProfilerXhprof.php
@@ -52,9 +52,9 @@
*/
class ProfilerXhprof extends Profiler {
/**
- * @var Xhprof $xhprof
+ * @var XhprofData|null $xhprofData
*/
- protected $xhprof;
+ protected $xhprofData;
/**
* Profiler for explicit, arbitrary, frame labels
@@ -68,10 +68,24 @@ class ProfilerXhprof extends Profiler {
*/
public function __construct( array $params = [] ) {
parent::__construct( $params );
- $this->xhprof = new Xhprof( $params );
+
+ $flags = isset( $params['flags'] ) ? $params['flags'] : 0;
+ $options = isset( $params['exclude'] )
+ ? [ 'ignored_functions' => $params['exclude'] ] : [];
+ Xhprof::enable( $flags, $options );
$this->sprofiler = new SectionProfiler();
}
+ /**
+ * @return XhprofData
+ */
+ public function getXhprofData() {
+ if ( !$this->xhprofData ) {
+ $this->xhprofData = new XhprofData( Xhprof::disable(), $this->params );
+ }
+ return $this->xhprofData;
+ }
+
public function scopedProfileIn( $section ) {
$key = 'section.' . ltrim( $section, '.' );
return $this->sprofiler->scopedProfileIn( $key );
@@ -112,7 +126,7 @@ class ProfilerXhprof extends Profiler {
}
public function getFunctionStats() {
- $metrics = $this->xhprof->getCompleteMetrics();
+ $metrics = $this->getXhprofData()->getCompleteMetrics();
$profile = [];
$main = null; // units in ms
@@ -216,6 +230,6 @@ class ProfilerXhprof extends Profiler {
* @return array
*/
public function getRawData() {
- return $this->xhprof->getRawData();
+ return $this->getXhprofData()->getRawData();
}
}