fileOpPerfTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Test for fileop performance.
  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. error_reporting( E_ALL );
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script to test fileop performance.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class TestFileOpPerformance extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Test fileop performance' );
  34. $this->addOption( 'b1', 'Backend 1', true, true );
  35. $this->addOption( 'b2', 'Backend 2', false, true );
  36. $this->addOption( 'srcdir', 'File source directory', true, true );
  37. $this->addOption( 'maxfiles', 'Max files', false, true );
  38. $this->addOption( 'quick', 'Avoid operation pre-checks (use doQuickOperations())' );
  39. $this->addOption( 'parallelize', '"parallelize" flag for doOperations()', false, true );
  40. }
  41. public function execute() {
  42. Profiler::setInstance( new ProfilerSimpleText( [] ) ); // clear
  43. $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b1' ) );
  44. $this->doPerfTest( $backend );
  45. if ( $this->getOption( 'b2' ) ) {
  46. $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b2' ) );
  47. $this->doPerfTest( $backend );
  48. }
  49. Profiler::instance()->setTemplated( true );
  50. // NOTE: as of MW1.21, $profiler->logData() is called implicitly by doMaintenance.php.
  51. }
  52. protected function doPerfTest( FileBackend $backend ) {
  53. $ops1 = [];
  54. $ops2 = [];
  55. $ops3 = [];
  56. $ops4 = [];
  57. $ops5 = [];
  58. $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
  59. $backend->prepare( [ 'dir' => $baseDir ] );
  60. $dirname = $this->getOption( 'srcdir' );
  61. $dir = opendir( $dirname );
  62. if ( !$dir ) {
  63. return;
  64. }
  65. while ( $dir && ( $file = readdir( $dir ) ) !== false ) {
  66. if ( $file[0] != '.' ) {
  67. $this->output( "Using '$dirname/$file' in operations.\n" );
  68. $dst = $baseDir . '/' . wfBaseName( $file );
  69. $ops1[] = [ 'op' => 'store',
  70. 'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1 ];
  71. $ops2[] = [ 'op' => 'copy',
  72. 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1 ];
  73. $ops3[] = [ 'op' => 'move',
  74. 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1 ];
  75. $ops4[] = [ 'op' => 'delete', 'src' => "$dst-1" ];
  76. $ops5[] = [ 'op' => 'delete', 'src' => "$dst-2" ];
  77. }
  78. if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
  79. break; // enough
  80. }
  81. }
  82. closedir( $dir );
  83. $this->output( "\n" );
  84. $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
  85. $opts = [ 'force' => 1 ];
  86. if ( $this->hasOption( 'parallelize' ) ) {
  87. $opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' );
  88. }
  89. $start = microtime( true );
  90. $status = $backend->$method( $ops1, $opts );
  91. $e = ( microtime( true ) - $start ) * 1000;
  92. if ( $status->getErrorsArray() ) {
  93. print_r( $status->getErrorsArray() );
  94. exit( 0 );
  95. }
  96. $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
  97. $start = microtime( true );
  98. $backend->$method( $ops2, $opts );
  99. $e = ( microtime( true ) - $start ) * 1000;
  100. if ( $status->getErrorsArray() ) {
  101. print_r( $status->getErrorsArray() );
  102. exit( 0 );
  103. }
  104. $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
  105. $start = microtime( true );
  106. $backend->$method( $ops3, $opts );
  107. $e = ( microtime( true ) - $start ) * 1000;
  108. if ( $status->getErrorsArray() ) {
  109. print_r( $status->getErrorsArray() );
  110. exit( 0 );
  111. }
  112. $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
  113. $start = microtime( true );
  114. $backend->$method( $ops4, $opts );
  115. $e = ( microtime( true ) - $start ) * 1000;
  116. if ( $status->getErrorsArray() ) {
  117. print_r( $status->getErrorsArray() );
  118. exit( 0 );
  119. }
  120. $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
  121. $start = microtime( true );
  122. $backend->$method( $ops5, $opts );
  123. $e = ( microtime( true ) - $start ) * 1000;
  124. if ( $status->getErrorsArray() ) {
  125. print_r( $status->getErrorsArray() );
  126. exit( 0 );
  127. }
  128. $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
  129. }
  130. }
  131. $maintClass = "TestFileOpPerformance";
  132. require_once RUN_MAINTENANCE_IF_MAIN;