| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use Wikimedia\FileBackend\FileBackend; |
| 25 |
|
| 26 |
|
| 27 |
require_once __DIR__ . '/Maintenance.php'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class FileOpPerfTest extends Maintenance { |
| 36 |
public function __construct() { |
| 37 |
parent::__construct(); |
| 38 |
$this->addDescription( 'Test fileop performance' ); |
| 39 |
$this->addOption( 'b1', 'Backend 1', true, true ); |
| 40 |
$this->addOption( 'b2', 'Backend 2', false, true ); |
| 41 |
$this->addOption( 'srcdir', 'File source directory', true, true ); |
| 42 |
$this->addOption( 'maxfiles', 'Max files', false, true ); |
| 43 |
$this->addOption( 'quick', 'Avoid operation pre-checks (use doQuickOperations())' ); |
| 44 |
$this->addOption( 'parallelize', '"parallelize" flag for doOperations()', false, true ); |
| 45 |
} |
| 46 |
|
| 47 |
public function execute() { |
| 48 |
$backendGroup = $this->getServiceContainer()->getFileBackendGroup(); |
| 49 |
$backend = $backendGroup->get( $this->getOption( 'b1' ) ); |
| 50 |
$this->doPerfTest( $backend ); |
| 51 |
|
| 52 |
if ( $this->getOption( 'b2' ) ) { |
| 53 |
$backend = $backendGroup->get( $this->getOption( 'b2' ) ); |
| 54 |
$this->doPerfTest( $backend ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
protected function doPerfTest( FileBackend $backend ) { |
| 59 |
$ops1 = []; |
| 60 |
$ops2 = []; |
| 61 |
$ops3 = []; |
| 62 |
$ops4 = []; |
| 63 |
$ops5 = []; |
| 64 |
|
| 65 |
$baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1'; |
| 66 |
$backend->prepare( [ 'dir' => $baseDir ] ); |
| 67 |
|
| 68 |
$dirname = $this->getOption( 'srcdir' ); |
| 69 |
$dir = opendir( $dirname ); |
| 70 |
if ( !$dir ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
while ( ( $file = readdir( $dir ) ) !== false ) { |
| 76 |
if ( $file[0] != '.' ) { |
| 77 |
$this->output( "Using '$dirname/$file' in operations.\n" ); |
| 78 |
$dst = $baseDir . '/' . wfBaseName( $file ); |
| 79 |
$ops1[] = [ 'op' => 'store', |
| 80 |
'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => true ]; |
| 81 |
$ops2[] = [ 'op' => 'copy', |
| 82 |
'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => true ]; |
| 83 |
$ops3[] = [ 'op' => 'move', |
| 84 |
'src' => $dst, 'dst' => "$dst-2", 'overwrite' => true ]; |
| 85 |
$ops4[] = [ 'op' => 'delete', 'src' => "$dst-1" ]; |
| 86 |
$ops5[] = [ 'op' => 'delete', 'src' => "$dst-2" ]; |
| 87 |
} |
| 88 |
if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) { |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
closedir( $dir ); |
| 93 |
$this->output( "\n" ); |
| 94 |
|
| 95 |
$method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations'; |
| 96 |
|
| 97 |
$opts = [ 'force' => 1 ]; |
| 98 |
if ( $this->hasOption( 'parallelize' ) ) { |
| 99 |
$opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' ); |
| 100 |
} |
| 101 |
|
| 102 |
$start = microtime( true ); |
| 103 |
$status = $backend->$method( $ops1, $opts ); |
| 104 |
$e = ( microtime( true ) - $start ) * 1000; |
| 105 |
if ( !$status->isGood() ) { |
| 106 |
$this->error( $status ); |
| 107 |
return; |
| 108 |
} |
| 109 |
$this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" ); |
| 110 |
|
| 111 |
$start = microtime( true ); |
| 112 |
$status = $backend->$method( $ops2, $opts ); |
| 113 |
$e = ( microtime( true ) - $start ) * 1000; |
| 114 |
if ( !$status->isGood() ) { |
| 115 |
$this->error( $status ); |
| 116 |
return; |
| 117 |
} |
| 118 |
$this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" ); |
| 119 |
|
| 120 |
$start = microtime( true ); |
| 121 |
$status = $backend->$method( $ops3, $opts ); |
| 122 |
$e = ( microtime( true ) - $start ) * 1000; |
| 123 |
if ( !$status->isGood() ) { |
| 124 |
$this->error( $status ); |
| 125 |
return; |
| 126 |
} |
| 127 |
$this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" ); |
| 128 |
|
| 129 |
$start = microtime( true ); |
| 130 |
$status = $backend->$method( $ops4, $opts ); |
| 131 |
$e = ( microtime( true ) - $start ) * 1000; |
| 132 |
if ( !$status->isGood() ) { |
| 133 |
$this->error( $status ); |
| 134 |
return; |
| 135 |
} |
| 136 |
$this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" ); |
| 137 |
|
| 138 |
$start = microtime( true ); |
| 139 |
$status = $backend->$method( $ops5, $opts ); |
| 140 |
$e = ( microtime( true ) - $start ) * 1000; |
| 141 |
if ( !$status->isGood() ) { |
| 142 |
$this->error( $status ); |
| 143 |
return; |
| 144 |
} |
| 145 |
$this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
$maintClass = FileOpPerfTest::class; |
| 151 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 152 |
|