phpcs: More require/include is not a function
[lhc/web/wiklou.git] / maintenance / benchmarks / bench_delete_truncate.php
1 <?php
2 /**
3 * Benchmark SQL DELETE vs SQL TRUNCATE.
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 Benchmark
22 */
23
24 require_once __DIR__ . '/Benchmarker.php';
25
26 /**
27 * Maintenance script that benchmarks SQL DELETE vs SQL TRUNCATE.
28 *
29 * @ingroup Benchmark
30 */
31 class BenchmarkDeleteTruncate extends Benchmarker {
32
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Benchmarks SQL DELETE vs SQL TRUNCATE.";
36 }
37
38 public function execute() {
39 $dbw = wfGetDB( DB_MASTER );
40
41 $test = $dbw->tableName( 'test' );
42 $dbw->query( "CREATE TABLE IF NOT EXISTS /*_*/$test (
43 test_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
44 text varbinary(255) NOT NULL
45 );" );
46
47 $this->insertData( $dbw );
48
49 $start = microtime( true );
50
51 $this->delete( $dbw );
52
53 $end = microtime( true );
54
55 echo "Delete: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
56 echo "\r\n";
57
58 $this->insertData( $dbw );
59
60 $start = microtime( true );
61
62 $this->truncate( $dbw );
63
64 $end = microtime( true );
65
66 echo "Truncate: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
67 echo "\r\n";
68
69 $dbw->dropTable( 'test' );
70 }
71
72 /**
73 * @param $dbw DatabaseBase
74 * @return void
75 */
76 private function insertData( $dbw ) {
77 $range = range( 0, 1024 );
78 $data = array();
79 foreach( $range as $r ) {
80 $data[] = array( 'text' => $r );
81 }
82 $dbw->insert( 'test', $data, __METHOD__ );
83 }
84
85 /**
86 * @param $dbw DatabaseBase
87 * @return void
88 */
89 private function delete( $dbw ) {
90 $dbw->delete( 'text', '*', __METHOD__ );
91 }
92
93 /**
94 * @param $dbw DatabaseBase
95 * @return void
96 */
97 private function truncate( $dbw ) {
98 $test = $dbw->tableName( 'test' );
99 $dbw->query( "TRUNCATE TABLE $test" );
100 }
101 }
102
103 $maintClass = "BenchmarkDeleteTruncate";
104 require_once RUN_MAINTENANCE_IF_MAIN;