fileOpPerfTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b1' ) );
  43. $this->doPerfTest( $backend );
  44. if ( $this->getOption( 'b2' ) ) {
  45. $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b2' ) );
  46. $this->doPerfTest( $backend );
  47. }
  48. }
  49. protected function doPerfTest( FileBackend $backend ) {
  50. $ops1 = [];
  51. $ops2 = [];
  52. $ops3 = [];
  53. $ops4 = [];
  54. $ops5 = [];
  55. $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
  56. $backend->prepare( [ 'dir' => $baseDir ] );
  57. $dirname = $this->getOption( 'srcdir' );
  58. $dir = opendir( $dirname );
  59. if ( !$dir ) {
  60. return;
  61. }
  62. while ( $dir && ( $file = readdir( $dir ) ) !== false ) {
  63. if ( $file[0] != '.' ) {
  64. $this->output( "Using '$dirname/$file' in operations.\n" );
  65. $dst = $baseDir . '/' . wfBaseName( $file );
  66. $ops1[] = [ 'op' => 'store',
  67. 'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1 ];
  68. $ops2[] = [ 'op' => 'copy',
  69. 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1 ];
  70. $ops3[] = [ 'op' => 'move',
  71. 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1 ];
  72. $ops4[] = [ 'op' => 'delete', 'src' => "$dst-1" ];
  73. $ops5[] = [ 'op' => 'delete', 'src' => "$dst-2" ];
  74. }
  75. if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
  76. break; // enough
  77. }
  78. }
  79. closedir( $dir );
  80. $this->output( "\n" );
  81. $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
  82. $opts = [ 'force' => 1 ];
  83. if ( $this->hasOption( 'parallelize' ) ) {
  84. $opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' );
  85. }
  86. $start = microtime( true );
  87. $status = $backend->$method( $ops1, $opts );
  88. $e = ( microtime( true ) - $start ) * 1000;
  89. if ( $status->getErrorsArray() ) {
  90. print_r( $status->getErrorsArray() );
  91. exit( 0 );
  92. }
  93. $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
  94. $start = microtime( true );
  95. $backend->$method( $ops2, $opts );
  96. $e = ( microtime( true ) - $start ) * 1000;
  97. if ( $status->getErrorsArray() ) {
  98. print_r( $status->getErrorsArray() );
  99. exit( 0 );
  100. }
  101. $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
  102. $start = microtime( true );
  103. $backend->$method( $ops3, $opts );
  104. $e = ( microtime( true ) - $start ) * 1000;
  105. if ( $status->getErrorsArray() ) {
  106. print_r( $status->getErrorsArray() );
  107. exit( 0 );
  108. }
  109. $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
  110. $start = microtime( true );
  111. $backend->$method( $ops4, $opts );
  112. $e = ( microtime( true ) - $start ) * 1000;
  113. if ( $status->getErrorsArray() ) {
  114. print_r( $status->getErrorsArray() );
  115. exit( 0 );
  116. }
  117. $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
  118. $start = microtime( true );
  119. $backend->$method( $ops5, $opts );
  120. $e = ( microtime( true ) - $start ) * 1000;
  121. if ( $status->getErrorsArray() ) {
  122. print_r( $status->getErrorsArray() );
  123. exit( 0 );
  124. }
  125. $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
  126. }
  127. }
  128. $maintClass = "TestFileOpPerformance";
  129. require_once RUN_MAINTENANCE_IF_MAIN;