clear magicword cache before testing cleanSig
[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' );
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 $profiler->logData(); // prints
58 }
59
60 protected function doPerfTest( FileBackend $backend ) {
61 $ops1 = array();
62 $ops2 = array();
63 $ops3 = array();
64 $ops4 = array();
65 $ops5 = array();
66
67 $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
68 $backend->prepare( array( 'dir' => $baseDir ) );
69
70 $dirname = $this->getOption( 'srcdir' );
71 $dir = opendir( $dirname );
72 if ( !$dir ) {
73 return;
74 }
75
76 while ( $dir && ( $file = readdir( $dir ) ) !== false ) {
77 if ( $file[0] != '.' ) {
78 $this->output( "Using '$dirname/$file' in operations.\n" );
79 $dst = $baseDir . '/' . wfBaseName( $file );
80 $ops1[] = array( 'op' => 'store',
81 'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1);
82 $ops2[] = array( 'op' => 'copy',
83 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1 );
84 $ops3[] = array( 'op' => 'move',
85 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1 );
86 $ops4[] = array( 'op' => 'delete', 'src' => "$dst-1" );
87 $ops5[] = array( 'op' => 'delete', 'src' => "$dst-2" );
88 }
89 if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
90 break; // enough
91 }
92 }
93 closedir( $dir );
94 $this->output( "\n" );
95
96 $method = $this->hasOption( 'quick' ) ? 'doQuickOperations' : 'doOperations';
97
98 $start = microtime( true );
99 $status = $backend->$method( $ops1, array( 'force' => 1 ) );
100 $e = ( microtime( true ) - $start ) * 1000;
101 if ( $status->getErrorsArray() ) {
102 print_r( $status->getErrorsArray() );
103 exit(0);
104 }
105 $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
106
107 $start = microtime( true );
108 $backend->$method( $ops2, array( 'force' => 1 ) );
109 $e = ( microtime( true ) - $start ) * 1000;
110 if ( $status->getErrorsArray() ) {
111 print_r( $status->getErrorsArray() );
112 exit(0);
113 }
114 $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
115
116 $start = microtime( true );
117 $backend->$method( $ops3, array( 'force' => 1 ) );
118 $e = ( microtime( true ) - $start ) * 1000;
119 if ( $status->getErrorsArray() ) {
120 print_r( $status->getErrorsArray() );
121 exit(0);
122 }
123 $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
124
125 $start = microtime( true );
126 $backend->$method( $ops4, array( 'force' => 1 ) );
127 $e = ( microtime( true ) - $start ) * 1000;
128 if ( $status->getErrorsArray() ) {
129 print_r( $status->getErrorsArray() );
130 exit(0);
131 }
132 $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
133
134 $start = microtime( true );
135 $backend->$method( $ops5, array( 'force' => 1 ) );
136 $e = ( microtime( true ) - $start ) * 1000;
137 if ( $status->getErrorsArray() ) {
138 print_r( $status->getErrorsArray() );
139 exit(0);
140 }
141 $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
142 }
143 }
144
145 $maintClass = "TestFileOpPerformance";
146 require_once( RUN_MAINTENANCE_IF_MAIN );