PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/maintenance/benchmarks/benchmarkPurge.php

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