benchmarkPurge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Benchmark for Squid purge.
  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 Squid purge.
  26. *
  27. * @ingroup Benchmark
  28. */
  29. class BenchmarkPurge extends Benchmarker {
  30. public function __construct() {
  31. parent::__construct();
  32. $this->addDescription( 'Benchmark the Squid purge functions.' );
  33. }
  34. public function execute() {
  35. global $wgUseSquid, $wgSquidServers;
  36. if ( !$wgUseSquid ) {
  37. $this->error( "Squid purge benchmark doesn't do much without squid support on.", true );
  38. } else {
  39. $this->output( "There are " . count( $wgSquidServers ) . " defined squid servers:\n" );
  40. if ( $this->hasOption( 'count' ) ) {
  41. $lengths = [ intval( $this->getOption( 'count' ) ) ];
  42. } else {
  43. $lengths = [ 1, 10, 100 ];
  44. }
  45. foreach ( $lengths as $length ) {
  46. $urls = $this->randomUrlList( $length );
  47. $trial = $this->benchSquid( $urls );
  48. $this->output( $trial . "\n" );
  49. }
  50. }
  51. }
  52. /**
  53. * Run a bunch of URLs through SquidUpdate::purge()
  54. * to benchmark Squid response times.
  55. * @param array $urls A bunch of URLs to purge
  56. * @param int $trials How many times to run the test?
  57. * @return string
  58. */
  59. private function benchSquid( $urls, $trials = 1 ) {
  60. $start = microtime( true );
  61. for ( $i = 0; $i < $trials; $i++ ) {
  62. CdnCacheUpdate::purge( $urls );
  63. }
  64. $delta = microtime( true ) - $start;
  65. $pertrial = $delta / $trials;
  66. $pertitle = $pertrial / count( $urls );
  67. return sprintf( "%4d titles in %6.2fms (%6.2fms each)",
  68. count( $urls ), $pertrial * 1000.0, $pertitle * 1000.0 );
  69. }
  70. /**
  71. * Get an array of randomUrl()'s.
  72. * @param int $length How many urls to add to the array
  73. * @return array
  74. */
  75. private function randomUrlList( $length ) {
  76. $list = [];
  77. for ( $i = 0; $i < $length; $i++ ) {
  78. $list[] = $this->randomUrl();
  79. }
  80. return $list;
  81. }
  82. /**
  83. * Return a random URL of the wiki. Not necessarily an actual title in the
  84. * database, but at least a URL that looks like one.
  85. * @return string
  86. */
  87. private function randomUrl() {
  88. global $wgServer, $wgArticlePath;
  89. return $wgServer . str_replace( '$1', $this->randomTitle(), $wgArticlePath );
  90. }
  91. /**
  92. * Create a random title string (not necessarily a Title object).
  93. * For use with randomUrl().
  94. * @return string
  95. */
  96. private function randomTitle() {
  97. $str = '';
  98. $length = mt_rand( 1, 20 );
  99. for ( $i = 0; $i < $length; $i++ ) {
  100. $str .= chr( mt_rand( ord( 'a' ), ord( 'z' ) ) );
  101. }
  102. return ucfirst( $str );
  103. }
  104. }
  105. $maintClass = "BenchmarkPurge";
  106. require_once RUN_MAINTENANCE_IF_MAIN;