Merge "Remove some ancient upgrade information from release notes"
[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 use Wikimedia\Rdbms\IDatabase;
27 use Wikimedia\Rdbms\IMaintainableDatabase;
28
29 /**
30 * Maintenance script that benchmarks SQL DELETE vs SQL TRUNCATE.
31 *
32 * @ingroup Benchmark
33 */
34 class BenchmarkDeleteTruncate extends Benchmarker {
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Benchmarks SQL DELETE vs SQL TRUNCATE.' );
38 }
39
40 public function execute() {
41 $dbw = $this->getDB( DB_MASTER );
42
43 $test = $dbw->tableName( 'test' );
44 $dbw->query( "CREATE TABLE IF NOT EXISTS /*_*/$test (
45 test_id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
46 text varbinary(255) NOT NULL
47 );" );
48
49 $this->insertData( $dbw );
50
51 $start = microtime( true );
52
53 $this->delete( $dbw );
54
55 $end = microtime( true );
56
57 echo "Delete: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
58 echo "\r\n";
59
60 $this->insertData( $dbw );
61
62 $start = microtime( true );
63
64 $this->truncate( $dbw );
65
66 $end = microtime( true );
67
68 echo "Truncate: " . sprintf( "%6.3fms", ( $end - $start ) * 1000 );
69 echo "\r\n";
70
71 $dbw->dropTable( 'test' );
72 }
73
74 /**
75 * @param IDatabase $dbw
76 * @return void
77 */
78 private function insertData( $dbw ) {
79 $range = range( 0, 1024 );
80 $data = [];
81 foreach ( $range as $r ) {
82 $data[] = [ 'text' => $r ];
83 }
84 $dbw->insert( 'test', $data, __METHOD__ );
85 }
86
87 /**
88 * @param IDatabase $dbw
89 * @return void
90 */
91 private function delete( $dbw ) {
92 $dbw->delete( 'text', '*', __METHOD__ );
93 }
94
95 /**
96 * @param IMaintainableDatabase $dbw
97 * @return void
98 */
99 private function truncate( $dbw ) {
100 $test = $dbw->tableName( 'test' );
101 $dbw->query( "TRUNCATE TABLE $test" );
102 }
103 }
104
105 $maintClass = "BenchmarkDeleteTruncate";
106 require_once RUN_MAINTENANCE_IF_MAIN;