MediaWiki master
fileOpPerfTest.php
Go to the documentation of this file.
1<?php
12
13// @codeCoverageIgnoreStart
14require_once __DIR__ . '/Maintenance.php';
15// @codeCoverageIgnoreEnd
16
23 public function __construct() {
24 parent::__construct();
25 $this->addDescription( 'Test fileop performance' );
26 $this->addOption( 'b1', 'Backend 1', true, true );
27 $this->addOption( 'b2', 'Backend 2', false, true );
28 $this->addOption( 'srcdir', 'File source directory', true, true );
29 $this->addOption( 'maxfiles', 'Max files', false, true );
30 $this->addOption( 'quick', 'Avoid operation pre-checks (use doQuickOperations())' );
31 $this->addOption( 'parallelize', '"parallelize" flag for doOperations()', false, true );
32 }
33
34 public function execute() {
35 $backendGroup = $this->getServiceContainer()->getFileBackendGroup();
36 $backend = $backendGroup->get( $this->getOption( 'b1' ) );
37 $this->doPerfTest( $backend );
38
39 if ( $this->getOption( 'b2' ) ) {
40 $backend = $backendGroup->get( $this->getOption( 'b2' ) );
41 $this->doPerfTest( $backend );
42 }
43 }
44
45 protected function doPerfTest( FileBackend $backend ) {
46 $ops1 = [];
47 $ops2 = [];
48 $ops3 = [];
49 $ops4 = [];
50 $ops5 = [];
51
52 $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
53 $backend->prepare( [ 'dir' => $baseDir ] );
54
55 $dirname = $this->getOption( 'srcdir' );
56 $dir = opendir( $dirname );
57 if ( !$dir ) {
58 return;
59 }
60
61 // phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
62 while ( ( $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' => true ];
68 $ops2[] = [ 'op' => 'copy',
69 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => true ];
70 $ops3[] = [ 'op' => 'move',
71 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => true ];
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;
77 }
78 }
79 closedir( $dir );
80 $this->output( "\n" );
81
82 $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
83
84 $opts = [ 'force' => 1 ];
85 if ( $this->hasOption( 'parallelize' ) ) {
86 $opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' );
87 }
88
89 $start = microtime( true );
90 $status = $backend->$method( $ops1, $opts );
91 $e = ( microtime( true ) - $start ) * 1000;
92 if ( !$status->isGood() ) {
93 $this->error( $status );
94 return;
95 }
96 $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
97
98 $start = microtime( true );
99 $status = $backend->$method( $ops2, $opts );
100 $e = ( microtime( true ) - $start ) * 1000;
101 if ( !$status->isGood() ) {
102 $this->error( $status );
103 return;
104 }
105 $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
106
107 $start = microtime( true );
108 $status = $backend->$method( $ops3, $opts );
109 $e = ( microtime( true ) - $start ) * 1000;
110 if ( !$status->isGood() ) {
111 $this->error( $status );
112 return;
113 }
114 $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
115
116 $start = microtime( true );
117 $status = $backend->$method( $ops4, $opts );
118 $e = ( microtime( true ) - $start ) * 1000;
119 if ( !$status->isGood() ) {
120 $this->error( $status );
121 return;
122 }
123 $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
124
125 $start = microtime( true );
126 $status = $backend->$method( $ops5, $opts );
127 $e = ( microtime( true ) - $start ) * 1000;
128 if ( !$status->isGood() ) {
129 $this->error( $status );
130 return;
131 }
132 $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
133 }
134}
135
136// @codeCoverageIgnoreStart
137$maintClass = FileOpPerfTest::class;
138require_once RUN_MAINTENANCE_IF_MAIN;
139// @codeCoverageIgnoreEnd
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
Maintenance script to test fileop performance.
__construct()
Default constructor.
doPerfTest(FileBackend $backend)
execute()
Do the actual work.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Base class for all file backend classes (including multi-write backends).
prepare(array $params)
Prepare a storage directory for usage.
getName()
Get the unique backend name.
$maintClass