benchmarkHooks.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Benchmark %MediaWiki hooks.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Benchmark
  22. */
  23. require_once __DIR__ . '/Benchmarker.php';
  24. /**
  25. * Maintenance script that benchmarks %MediaWiki hooks.
  26. *
  27. * @ingroup Benchmark
  28. */
  29. class BenchmarkHooks extends Benchmarker {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Benchmark MediaWiki Hooks.' );
  33. }
  34. public function execute() {
  35. global $wgHooks;
  36. $wgHooks['Test'] = [];
  37. $time = $this->benchHooks();
  38. $this->output( 'Empty hook: ' . $time . "\n" );
  39. $wgHooks['Test'][] = [ $this, 'test' ];
  40. $time = $this->benchHooks();
  41. $this->output( 'Loaded (one) hook: ' . $time . "\n" );
  42. for ( $i = 0; $i < 9; $i++ ) {
  43. $wgHooks['Test'][] = [ $this, 'test' ];
  44. }
  45. $time = $this->benchHooks();
  46. $this->output( 'Loaded (ten) hook: ' . $time . "\n" );
  47. for ( $i = 0; $i < 90; $i++ ) {
  48. $wgHooks['Test'][] = [ $this, 'test' ];
  49. }
  50. $time = $this->benchHooks();
  51. $this->output( 'Loaded (one hundred) hook: ' . $time . "\n" );
  52. $this->output( "\n" );
  53. }
  54. /**
  55. * @param int $trials
  56. * @return string
  57. */
  58. private function benchHooks( $trials = 10 ) {
  59. $start = microtime( true );
  60. for ( $i = 0; $i < $trials; $i++ ) {
  61. Hooks::run( 'Test' );
  62. }
  63. $delta = microtime( true ) - $start;
  64. $pertrial = $delta / $trials;
  65. return sprintf( "Took %6.3fms",
  66. $pertrial * 1000 );
  67. }
  68. /**
  69. * @return bool
  70. */
  71. public function test() {
  72. return true;
  73. }
  74. }
  75. $maintClass = 'BenchmarkHooks';
  76. require_once RUN_MAINTENANCE_IF_MAIN;