Merge "[LockManager] Split QuorumLockManager into its own file."
[lhc/web/wiklou.git] / maintenance / fileOpPerfTest.php
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
24 $initialTime = microtime( true );
25 $wgProfiler = array( 'class' => 'ProfilerSimpleText' );
26 error_reporting( E_ALL );
27
28 require_once( __DIR__ . '/Maintenance.php' );
29
30 /**
31 * Maintenance script to test fileop performance.
32 *
33 * @ingroup Maintenance
34 */
35 class TestFileOpPerformance extends Maintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->mDescription = "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 $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b1' ) );
49 $this->doPerfTest( $backend );
50
51 if ( $this->getOption( 'b2' ) ) {
52 $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b2' ) );
53 $this->doPerfTest( $backend );
54 }
55
56 $profiler = Profiler::instance();
57 $profiler->setTemplated( true );
58
59 //NOTE: as of MW1.21, $profiler->logData() is called implicitly by doMaintenance.php.
60 }
61
62 protected function doPerfTest( FileBackend $backend ) {
63 $ops1 = array();
64 $ops2 = array();
65 $ops3 = array();
66 $ops4 = array();
67 $ops5 = array();
68
69 $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
70 $backend->prepare( array( 'dir' => $baseDir ) );
71
72 $dirname = $this->getOption( 'srcdir' );
73 $dir = opendir( $dirname );
74 if ( !$dir ) {
75 return;
76 }
77
78 while ( $dir && ( $file = readdir( $dir ) ) !== false ) {
79 if ( $file[0] != '.' ) {
80 $this->output( "Using '$dirname/$file' in operations.\n" );
81 $dst = $baseDir . '/' . wfBaseName( $file );
82 $ops1[] = array( 'op' => 'store',
83 'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1);
84 $ops2[] = array( 'op' => 'copy',
85 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1 );
86 $ops3[] = array( 'op' => 'move',
87 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1 );
88 $ops4[] = array( 'op' => 'delete', 'src' => "$dst-1" );
89 $ops5[] = array( 'op' => 'delete', 'src' => "$dst-2" );
90 }
91 if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
92 break; // enough
93 }
94 }
95 closedir( $dir );
96 $this->output( "\n" );
97
98 $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
99
100 $opts = array( 'force' => 1 );
101 if ( $this->hasOption( 'parallelize' ) ) {
102 $opts['parallelize'] = ( $this->getOption( 'parallelize' ) === 'true' );
103 }
104
105 $start = microtime( true );
106 $status = $backend->$method( $ops1, $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() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
113
114 $start = microtime( true );
115 $backend->$method( $ops2, $opts );
116 $e = ( microtime( true ) - $start ) * 1000;
117 if ( $status->getErrorsArray() ) {
118 print_r( $status->getErrorsArray() );
119 exit(0);
120 }
121 $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
122
123 $start = microtime( true );
124 $backend->$method( $ops3, $opts );
125 $e = ( microtime( true ) - $start ) * 1000;
126 if ( $status->getErrorsArray() ) {
127 print_r( $status->getErrorsArray() );
128 exit(0);
129 }
130 $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
131
132 $start = microtime( true );
133 $backend->$method( $ops4, $opts );
134 $e = ( microtime( true ) - $start ) * 1000;
135 if ( $status->getErrorsArray() ) {
136 print_r( $status->getErrorsArray() );
137 exit(0);
138 }
139 $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
140
141 $start = microtime( true );
142 $backend->$method( $ops5, $opts );
143 $e = ( microtime( true ) - $start ) * 1000;
144 if ( $status->getErrorsArray() ) {
145 print_r( $status->getErrorsArray() );
146 exit(0);
147 }
148 $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
149 }
150 }
151
152 $maintClass = "TestFileOpPerformance";
153 require_once( RUN_MAINTENANCE_IF_MAIN );