wiki.techinc.nl/includes/libs/Xhprof.php
Timo Tijhof c77f9a8c60 profiler: Fixup broken logic for the new 'running' option
Follows-up I8808cad03418 which added this option, but I neglected to
actually test it. When trying it out with a patched wmf-config on
a mwdebug server, the following issue surfaced:

> Fatal TypeError: Argument 1 passed to XhprofData::__construct() must
> be of type array, null given, called in ProfilerXhprof.php:90

This is because ProfilerXhprof::disable() actually has its own state
tracking which refuses to even try calling tideways_xhprof_disable()
unless its state says it was in control of enabling it.

Remove this check and reduce the class to just abstracting the calls
to the PHP extension.

Also, remove the useless null return which is incompatible with the
callers to this function as evidenced by above Fatal TypeError.
The PHP extensions already default to an empty array, which is now
documented.

Test Plan:
* Local wiki with php-tideways installed, on this pach applied.
  Note that MediaWiki-Docker now has php-tideways pre-installed!
* In LocalSettings, configure profiling as per
  <https://www.mediawiki.org/wiki/MediaWiki-Docker/Recipes/Profiling>
  $wgProfiler = [
    'class'  => ProfilerXhprof::class,
    'output' => 'text',
  ];
* At load.php?forceprofile=1, there are valid percentages (100% "main").
* Add `tideways_xhprof_enable();` and `$wgProfiler['running'] = true;`
  to LocalSettings.
  The percentages are still valid, and the profile is now longer
  with additional coverage of early code such as MediaWikiServices.
* Switching to master, there is a fatal error with these settings.

Bug: T247332
Change-Id: Iaad88a0c46d39aee1374e5b02a77251bfa4c6f09
2021-09-16 03:26:23 +01:00

85 lines
2.1 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* Convenience class for working with XHProf
* <https://github.com/phacility/xhprof>. XHProf can be installed via PECL.
*
* This also supports using the Tideways profiler
* <https://github.com/tideways/php-xhprof-extension>.
*
* @internal For use by ProfilerXhprof
* @since 1.28
*/
class Xhprof {
/**
* Start profiler
*
* @param int $flags
* @param array $options
*/
public static function enable( $flags = 0, $options = [] ) {
$args = [ $flags ];
if ( $options ) {
$args[] = $options;
}
self::callAny(
[
'xhprof_enable',
'tideways_enable',
'tideways_xhprof_enable'
],
$args
);
}
/**
* Stop profiler
*
* @return array Xhprof data since last enable call,
* or empty array if it was never enabled.
*/
public static function disable() {
return self::callAny( [
'xhprof_disable',
'tideways_disable',
'tideways_xhprof_disable'
] );
}
/**
* Call the first available function from $functions.
* @param array $functions
* @param array $args
* @return mixed
* @throws Exception
*/
protected static function callAny( array $functions, array $args = [] ) {
foreach ( $functions as $func ) {
if ( function_exists( $func ) ) {
return $func( ...$args );
}
}
throw new Exception( "Neither xhprof nor tideways are installed" );
}
}