Xhprof.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Convenience class for working with XHProf
  22. * <https://github.com/phacility/xhprof>. XHProf can be installed as a PECL
  23. * package for use with PHP5 (Zend PHP) and is built-in to HHVM 3.3.0.
  24. *
  25. * This also supports using the Tideways profiler
  26. * <https://github.com/tideways/php-profiler-extension>, which additionally
  27. * has support for PHP7.
  28. *
  29. * @since 1.28
  30. */
  31. class Xhprof {
  32. /**
  33. * @var bool $enabled Whether XHProf is currently running.
  34. */
  35. protected static $enabled;
  36. /**
  37. * Start xhprof profiler
  38. * @return bool
  39. */
  40. public static function isEnabled() {
  41. return self::$enabled;
  42. }
  43. /**
  44. * Start xhprof profiler
  45. * @param int $flags
  46. * @param array $options
  47. * @throws Exception
  48. */
  49. public static function enable( $flags = 0, $options = [] ) {
  50. if ( self::isEnabled() ) {
  51. throw new Exception( 'Profiling is already enabled.' );
  52. }
  53. $args = [ $flags ];
  54. if ( $options ) {
  55. $args[] = $options;
  56. }
  57. self::$enabled = true;
  58. self::callAny(
  59. [
  60. 'xhprof_enable',
  61. 'tideways_enable',
  62. 'tideways_xhprof_enable'
  63. ],
  64. $args
  65. );
  66. }
  67. /**
  68. * Stop xhprof profiler
  69. *
  70. * @return array|null xhprof data from the run, or null if xhprof was not running.
  71. */
  72. public static function disable() {
  73. if ( self::isEnabled() ) {
  74. self::$enabled = false;
  75. return self::callAny( [
  76. 'xhprof_disable',
  77. 'tideways_disable',
  78. 'tideways_xhprof_disable'
  79. ] );
  80. } else {
  81. return null;
  82. }
  83. }
  84. /**
  85. * Call the first available function from $functions.
  86. * @param array $functions
  87. * @param array $args
  88. * @return mixed
  89. * @throws Exception
  90. */
  91. protected static function callAny( array $functions, array $args = [] ) {
  92. foreach ( $functions as $func ) {
  93. if ( function_exists( $func ) ) {
  94. return $func( ...$args );
  95. }
  96. }
  97. throw new Exception( "Neither xhprof nor tideways are installed" );
  98. }
  99. }