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