mctest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Makes several 'set', 'incr' and 'get' requests on every memcached
  4. * server and shows a report.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that makes several 'set', 'incr' and 'get' requests
  27. * on every memcached server and shows a report.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class McTest extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription( "Makes several 'set', 'incr' and 'get' requests on every"
  35. . " memcached server and shows a report" );
  36. $this->addOption( 'i', 'Number of iterations', false, true );
  37. $this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', false, true );
  38. $this->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
  39. }
  40. public function execute() {
  41. global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
  42. $cache = $this->getOption( 'cache' );
  43. $iterations = $this->getOption( 'i', 100 );
  44. if ( $cache ) {
  45. if ( !isset( $wgObjectCaches[$cache] ) ) {
  46. $this->error( "MediaWiki isn't configured with a cache named '$cache'", 1 );
  47. }
  48. $servers = $wgObjectCaches[$cache]['servers'];
  49. } elseif ( $this->hasArg() ) {
  50. $servers = [ $this->getArg() ];
  51. } elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
  52. global $wgMemCachedServers;
  53. $servers = $wgMemCachedServers;
  54. } elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
  55. $servers = $wgObjectCaches[$wgMainCacheType]['servers'];
  56. } else {
  57. $this->error( "MediaWiki isn't configured for Memcached usage", 1 );
  58. }
  59. # find out the longest server string to nicely align output later on
  60. $maxSrvLen = $servers ? max( array_map( 'strlen', $servers ) ) : 0;
  61. foreach ( $servers as $server ) {
  62. $this->output(
  63. str_pad( $server, $maxSrvLen ),
  64. $server # output channel
  65. );
  66. $mcc = new MemcachedClient( [
  67. 'persistant' => true,
  68. 'timeout' => $wgMemCachedTimeout
  69. ] );
  70. $mcc->set_servers( [ $server ] );
  71. $set = 0;
  72. $incr = 0;
  73. $get = 0;
  74. $time_start = microtime( true );
  75. for ( $i = 1; $i <= $iterations; $i++ ) {
  76. if ( $mcc->set( "test$i", $i ) ) {
  77. $set++;
  78. }
  79. }
  80. for ( $i = 1; $i <= $iterations; $i++ ) {
  81. if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
  82. $incr++;
  83. }
  84. }
  85. for ( $i = 1; $i <= $iterations; $i++ ) {
  86. $value = $mcc->get( "test$i" );
  87. if ( $value == $i * 2 ) {
  88. $get++;
  89. }
  90. }
  91. $exectime = microtime( true ) - $time_start;
  92. $this->output( " set: $set incr: $incr get: $get time: $exectime", $server );
  93. }
  94. }
  95. }
  96. $maintClass = "McTest";
  97. require_once RUN_MAINTENANCE_IF_MAIN;