MediaWiki  master
fileOpPerfTest.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
33 class FileOpPerfTest extends Maintenance {
34  public function __construct() {
35  parent::__construct();
36  $this->addDescription( 'Test fileop performance' );
37  $this->addOption( 'b1', 'Backend 1', true, true );
38  $this->addOption( 'b2', 'Backend 2', false, true );
39  $this->addOption( 'srcdir', 'File source directory', true, true );
40  $this->addOption( 'maxfiles', 'Max files', false, true );
41  $this->addOption( 'quick', 'Avoid operation pre-checks (use doQuickOperations())' );
42  $this->addOption( 'parallelize', '"parallelize" flag for doOperations()', false, true );
43  }
44 
45  public function execute() {
46  $backendGroup = MediaWikiServices::getInstance()->getFileBackendGroup();
47  $backend = $backendGroup->get( $this->getOption( 'b1' ) );
48  $this->doPerfTest( $backend );
49 
50  if ( $this->getOption( 'b2' ) ) {
51  $backend = $backendGroup->get( $this->getOption( 'b2' ) );
52  $this->doPerfTest( $backend );
53  }
54  }
55 
56  protected function doPerfTest( FileBackend $backend ) {
57  $ops1 = [];
58  $ops2 = [];
59  $ops3 = [];
60  $ops4 = [];
61  $ops5 = [];
62 
63  $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
64  $backend->prepare( [ 'dir' => $baseDir ] );
65 
66  $dirname = $this->getOption( 'srcdir' );
67  $dir = opendir( $dirname );
68  if ( !$dir ) {
69  return;
70  }
71 
72  while ( ( $file = readdir( $dir ) ) !== false ) {
73  if ( $file[0] != '.' ) {
74  $this->output( "Using '$dirname/$file' in operations.\n" );
75  $dst = $baseDir . '/' . wfBaseName( $file );
76  $ops1[] = [ 'op' => 'store',
77  'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1 ];
78  $ops2[] = [ 'op' => 'copy',
79  'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1 ];
80  $ops3[] = [ 'op' => 'move',
81  'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1 ];
82  $ops4[] = [ 'op' => 'delete', 'src' => "$dst-1" ];
83  $ops5[] = [ 'op' => 'delete', 'src' => "$dst-2" ];
84  }
85  if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
86  break;
87  }
88  }
89  closedir( $dir );
90  $this->output( "\n" );
91 
92  $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
93 
94  $opts = [ 'force' => 1 ];
95  if ( $this->hasOption( 'parallelize' ) ) {
96  $opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' );
97  }
98 
99  $start = microtime( true );
100  $status = $backend->$method( $ops1, $opts );
101  $e = ( microtime( true ) - $start ) * 1000;
102  if ( $status->getErrorsArray() ) {
103  print_r( $status->getErrorsArray() );
104  exit( 0 );
105  }
106  $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
107 
108  $start = microtime( true );
109  $backend->$method( $ops2, $opts );
110  $e = ( microtime( true ) - $start ) * 1000;
111  if ( $status->getErrorsArray() ) {
112  print_r( $status->getErrorsArray() );
113  exit( 0 );
114  }
115  $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
116 
117  $start = microtime( true );
118  $backend->$method( $ops3, $opts );
119  $e = ( microtime( true ) - $start ) * 1000;
120  if ( $status->getErrorsArray() ) {
121  print_r( $status->getErrorsArray() );
122  exit( 0 );
123  }
124  $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
125 
126  $start = microtime( true );
127  $backend->$method( $ops4, $opts );
128  $e = ( microtime( true ) - $start ) * 1000;
129  if ( $status->getErrorsArray() ) {
130  print_r( $status->getErrorsArray() );
131  exit( 0 );
132  }
133  $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
134 
135  $start = microtime( true );
136  $backend->$method( $ops5, $opts );
137  $e = ( microtime( true ) - $start ) * 1000;
138  if ( $status->getErrorsArray() ) {
139  print_r( $status->getErrorsArray() );
140  exit( 0 );
141  }
142  $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
143  }
144 }
145 
146 $maintClass = FileOpPerfTest::class;
147 require_once RUN_MAINTENANCE_IF_MAIN;
FileBackend
Base class for all file backend classes (including multi-write backends).
Definition: FileBackend.php:98
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:186
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
FileBackend\getName
getName()
Get the unique backend name.
Definition: FileBackend.php:251
wfBaseName
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
Definition: GlobalFunctions.php:2118
$maintClass
$maintClass
Definition: fileOpPerfTest.php:146
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
FileOpPerfTest\__construct
__construct()
Default constructor.
Definition: fileOpPerfTest.php:34
FileBackend\prepare
prepare(array $params)
Prepare a storage directory for usage.
Definition: FileBackend.php:873
FileOpPerfTest\execute
execute()
Do the actual work.
Definition: fileOpPerfTest.php:45
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
FileOpPerfTest\doPerfTest
doPerfTest(FileBackend $backend)
Definition: fileOpPerfTest.php:56
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
FileOpPerfTest
Maintenance script to test fileop performance.
Definition: fileOpPerfTest.php:33