Merge "wfProfileOut() for new return added in c6396 (c4e407c)"
[lhc/web/wiklou.git] / maintenance / fileOpPerfTest.php
1 <?php
2 /**
3 * Maintenance script to test 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( dirname( __FILE__ ) . '/Maintenance.php' );
29
30 class TestFileOpPerformance extends Maintenance {
31 public function __construct() {
32 parent::__construct();
33 $this->mDescription = "Test fileop performance";
34 $this->addOption( 'b1', 'Backend 1', true, true );
35 $this->addOption( 'b2', 'Backend 2', false, true );
36 $this->addOption( 'srcdir', 'File source directory', true, true );
37 $this->addOption( 'maxfiles', 'Max files', false, true );
38 }
39
40 public function execute() {
41 $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b1' ) );
42 $this->doPerfTest( $backend );
43
44 if ( $this->getOption( 'b2' ) ) {
45 $backend = FileBackendGroup::singleton()->get( $this->getOption( 'b2' ) );
46 $this->doPerfTest( $backend );
47 }
48
49 $profiler = Profiler::instance();
50 $profiler->setTemplated( true );
51 $profiler->logData(); // prints
52 }
53
54 protected function doPerfTest( FileBackend $backend ) {
55 $ops1 = array();
56 $ops2 = array();
57 $ops3 = array();
58 $ops4 = array();
59 $ops5 = array();
60
61 $baseDir = 'mwstore://' . $backend->getName() . '/testing-cont1';
62 $backend->prepare( array( 'dir' => $baseDir ) );
63
64 $dirname = $this->getOption( 'srcdir' );
65 $dir = opendir( $dirname );
66 if ( !$dir ) {
67 return;
68 }
69
70 while ( $dir && ( $file = readdir( $dir ) ) !== false ) {
71 if ( $file[0] != '.' ) {
72 $this->output( "Using '$dirname/$file' in operations.\n" );
73 $dst = $baseDir . '/' . wfBaseName( $file );
74 $ops1[] = array( 'op' => 'store', 'src' => "$dirname/$file", 'dst' => $dst, 'overwrite' => 1);
75 $ops2[] = array( 'op' => 'copy', 'src' => "$dst", 'dst' => "$dst-1", 'overwrite' => 1);
76 $ops3[] = array( 'op' => 'move', 'src' => $dst, 'dst' => "$dst-2", 'overwrite' => 1);
77 $ops4[] = array( 'op' => 'delete', 'src' => "$dst-1", 'overwrite' => 1 );
78 $ops5[] = array( 'op' => 'delete', 'src' => "$dst-2", 'overwrite' => 1 );
79 }
80 if ( count( $ops1 ) >= $this->getOption( 'maxfiles', 20 ) ) {
81 break; // enough
82 }
83 }
84 closedir( $dir );
85 $this->output( "\n" );
86
87 $start = microtime( true );
88 $status = $backend->doOperations( $ops1, array( 'force' => 1 ) );
89 $e = ( microtime( true ) - $start ) * 1000;
90 if ( $status->getErrorsArray() ) {
91 print_r( $status->getErrorsArray() );
92 exit(0);
93 }
94 $this->output( $backend->getName() . ": Stored " . count( $ops1 ) . " files in $e ms.\n" );
95
96 $start = microtime( true );
97 $backend->doOperations( $ops2, array( 'force' => 1 ) );
98 $e = ( microtime( true ) - $start ) * 1000;
99 if ( $status->getErrorsArray() ) {
100 print_r( $status->getErrorsArray() );
101 exit(0);
102 }
103 $this->output( $backend->getName() . ": Copied " . count( $ops2 ) . " files in $e ms.\n" );
104
105 $start = microtime( true );
106 $backend->doOperations( $ops3, array( 'force' => 1 ) );
107 $e = ( microtime( true ) - $start ) * 1000;
108 if ( $status->getErrorsArray() ) {
109 print_r( $status->getErrorsArray() );
110 exit(0);
111 }
112 $this->output( $backend->getName() . ": Moved " . count( $ops3 ) . " files in $e ms.\n" );
113
114 $start = microtime( true );
115 $backend->doOperations( $ops4, array( 'force' => 1 ) );
116 $e = ( microtime( true ) - $start ) * 1000;
117 if ( $status->getErrorsArray() ) {
118 print_r( $status->getErrorsArray() );
119 exit(0);
120 }
121 $this->output( $backend->getName() . ": Deleted " . count( $ops4 ) . " files in $e ms.\n" );
122
123 $start = microtime( true );
124 $backend->doOperations( $ops5, array( 'force' => 1 ) );
125 $e = ( microtime( true ) - $start ) * 1000;
126 if ( $status->getErrorsArray() ) {
127 print_r( $status->getErrorsArray() );
128 exit(0);
129 }
130 $this->output( $backend->getName() . ": Deleted " . count( $ops5 ) . " files in $e ms.\n" );
131 }
132 }
133
134 $maintClass = "TestFileOpPerformance";
135 require_once( RUN_MAINTENANCE_IF_MAIN );