From: Aaron Schulz Date: Sat, 22 Nov 2014 01:13:27 +0000 (-0800) Subject: Added custom frame support to Profiler X-Git-Tag: 1.31.0-rc.0~13179 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=240152117c999df805fbec6ec9c0d4fd1d06b8c4;p=lhc%2Fweb%2Fwiklou.git Added custom frame support to Profiler * Made use of it in the DatabaseBase classes * For the xhprof class, this only works in HHVM for now Change-Id: I95d7cc128d4a770328fbdd2b546972d3fc2e2e8a --- diff --git a/includes/db/Database.php b/includes/db/Database.php index fc2451e508..fc13eebbec 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -962,7 +962,8 @@ abstract class DatabaseBase implements IDatabase { $totalProf = ''; $isMaster = !is_null( $this->getLBInfo( 'master' ) ); - if ( !Profiler::instance()->isStub() ) { + $profiler = Profiler::instance(); + if ( !$profiler->isStub() ) { # generalizeSQL will probably cut down the query to reasonable # logging size most of the time. The substr is really just a sanity check. if ( $isMaster ) { @@ -975,8 +976,8 @@ abstract class DatabaseBase implements IDatabase { # Include query transaction state $queryProf .= $this->mTrxShortId ? " [TRX#{$this->mTrxShortId}]" : ""; - wfProfileIn( $totalProf ); - wfProfileIn( $queryProf ); + $totalProfSection = $profiler->scopedProfileIn( $totalProf ); + $queryProfSection = $profiler->scopedProfileIn( $queryProf ); } if ( $this->debug() ) { @@ -1059,11 +1060,6 @@ abstract class DatabaseBase implements IDatabase { $this->reportQueryError( $this->lastError(), $this->lastErrno(), $sql, $fname, $tempIgnore ); } - if ( !Profiler::instance()->isStub() ) { - wfProfileOut( $queryProf ); - wfProfileOut( $totalProf ); - } - return $this->resultObject( $ret ); } diff --git a/includes/profiler/Profiler.php b/includes/profiler/Profiler.php index 2b3b61675d..9650ff5199 100644 --- a/includes/profiler/Profiler.php +++ b/includes/profiler/Profiler.php @@ -139,6 +139,23 @@ abstract class Profiler { */ abstract public function profileOut( $functionname ); + /** + * Mark the start of a custom profiling frame (e.g. DB queries). + * The frame ends when the result of this method falls out of scope. + * + * @param string $section + * @return ScopedCallback|null + * @since 1.25 + */ + abstract public function scopedProfileIn( $section ); + + /** + * @param ScopedCallback $section + */ + public function scopedProfileOut( ScopedCallback &$section ) { + $section = null; + } + /** * @return TransactionProfiler * @since 1.25 diff --git a/includes/profiler/ProfilerStandard.php b/includes/profiler/ProfilerStandard.php index 15c5cdd04b..ab5e3abfb1 100644 --- a/includes/profiler/ProfilerStandard.php +++ b/includes/profiler/ProfilerStandard.php @@ -227,6 +227,15 @@ class ProfilerStandard extends Profiler { } } + public function scopedProfileIn( $section ) { + $this->profileIn( $section ); + + $that = $this; + return new ScopedCallback( function() use ( $that, $section ) { + $that->profileOut( $section ); + } ); + } + /** * Close opened profiling sections */ diff --git a/includes/profiler/ProfilerStub.php b/includes/profiler/ProfilerStub.php index 510a0a0d95..6fc74ef29a 100644 --- a/includes/profiler/ProfilerStub.php +++ b/includes/profiler/ProfilerStub.php @@ -37,6 +37,10 @@ class ProfilerStub extends Profiler { public function profileOut( $fn ) { } + public function scopedProfileIn( $section ) { + return null; + } + public function getFunctionStats() { } diff --git a/includes/profiler/ProfilerXhprof.php b/includes/profiler/ProfilerXhprof.php index d67806b079..cbd081da82 100644 --- a/includes/profiler/ProfilerXhprof.php +++ b/includes/profiler/ProfilerXhprof.php @@ -122,6 +122,23 @@ class ProfilerXhprof extends Profiler { public function profileOut( $functionname ) { } + public function scopedProfileIn( $section ) { + static $exists = null; + // Only HHVM supports this, not the standard PECL extension + if ( $exists === null ) { + $exists = function_exists( 'xhprof_frame_begin' ); + } + + if ( $exists ) { + xhprof_frame_begin( $section ); + return new ScopedCallback( function() use ( $section ) { + xhprof_frame_end( $section ); + } ); + } + + return null; + } + /** * No-op for xhprof profiling. */