summaryrefslogtreecommitdiff
path: root/tests/index_corrupt.pl
blob: 275747d86f87d7bad344ea7ecb6a04e23e175147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env perl

# Copyright (C) 2005 MySQL AB
# Use is subject to license terms
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA

#
# This is a test for a key cache bug (bug #10167)
# To expose the bug mysqld should be started with --key-buffer-size=64K
#

$opt_loop_count=100000; # Change this to make test harder/easier

##################### Standard benchmark inits ##############################

use DBI;
use Getopt::Long;
use Benchmark;

package main;

$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
$opt_host=$opt_user=$opt_password=""; $opt_db="test";

GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
	   "skip-delete","verbose","fast-insert","lock-tables","debug","fast",
	   "force","user=s","password=s") || die "Aborted";
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these

$firsttable  = "bench_f1";
$secondtable = "bench_f2";
$kill_file= "/tmp/mysqltest_index_corrupt.$$";

####
####  Start timeing and start test
####

$start_time=new Benchmark;
if (!$opt_skip_create)
{
  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  $dbh->do("drop table if exists $firsttable, $secondtable");

  print "Creating tables in $opt_db\n";
  $dbh->do("create table $firsttable (
c_pollid    INTEGER  NOT NULL,
c_time      BIGINT   NOT NULL,
c_data      DOUBLE   NOT NULL,
c_error     INTEGER  NOT NULL,
c_warning   INTEGER  NOT NULL,
c_okay      INTEGER  NOT NULL,
c_unknown   INTEGER  NOT NULL,
c_rolled_up BIT      NOT NULL,
INDEX t_mgmt_hist_r_i1 (c_pollid),
INDEX t_mgmt_hist_r_i2 (c_time),
INDEX t_mgmt_hist_r_i3 (c_rolled_up))") or die $DBI::errstr;

  $dbh->do("create table $secondtable (
c_pollid  INTEGER  NOT NULL,
c_min_time  BIGINT   NOT NULL,
c_max_time  BIGINT   NOT NULL,
c_min_data  DOUBLE   NOT NULL,
c_max_data  DOUBLE   NOT NULL,
c_avg_data  DOUBLE   NOT NULL,
c_error     INTEGER  NOT NULL,
c_warning   INTEGER  NOT NULL,
c_okay      INTEGER  NOT NULL,
c_unknown   INTEGER  NOT NULL,
c_rolled_up BIT      NOT NULL,
INDEX t_mgmt_hist_d_i1 (c_pollid),
INDEX t_mgmt_hist_d_i2 (c_min_time),
INDEX t_mgmt_hist_d_i3 (c_max_time),
INDEX t_mgmt_hist_d_i4 (c_rolled_up))") or die $DBI::errstr;


  $dbh->disconnect; $dbh=0;	# Close handler
}
$|= 1;				# Autoflush

####
#### Start the tests
####

print "Running tests\n";
insert_in_bench() if (($pid=fork()) == 0); $work{$pid}="insert";
select_from_bench() if (($pid=fork()) == 0); $work{$pid}="insert-select;
delete_from_bench() if (($pid=fork()) == 0); $work{$pid}="delete";

$errors=0;
while (($pid=wait()) != -1)
{
  $ret=$?/256;
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  $errors++ if ($ret != 0);
}

if (!$opt_skip_delete && !$errors)
{
  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  $dbh->do("drop table $firsttable, $secondtable");
}
print ($errors ? "Test failed\n" :"Test ok\n");

$end_time=new Benchmark;
print "Total time: " .
  timestr(timediff($end_time, $start_time),"noc") . "\n";

unlink $kill_file;

exit(0);

#
# Insert records in the two tables
#

sub insert_in_bench
{
  my ($dbh,$rows,$found,$i);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  for ($rows= 1; $rows <= $opt_loop_count ; $rows++)
  {
    $c_pollid = sprintf("%d",rand 1000);
    $c_time = sprintf("%d",rand 100000);
    $c_data = rand 1000000;
    $test = rand 1;
    $c_error=0;
    $c_warning=0;
    $c_okay=0;
    $c_unknown=0;
    if ($test < .8) {
      $c_okay=1;
    } elsif ($test <.9) {
      $c_error=1;
    } elsif ($test <.95) {
      $c_warning=1;
    } else {
      $c_unknown=1;
    }
    $statement = "INSERT INTO $firsttable (c_pollid, c_time, c_data, c_error
, c_warning, c_okay, c_unknown, c_rolled_up) ".
  "VALUES ($c_pollid,$c_time,$c_data,$c_error,$c_warning,$c_okay,$c_unknown,0)";
    $cursor = $dbh->prepare($statement);
    $cursor->execute();
    $cursor->finish();
  }

  $dbh->disconnect; $dbh=0;
  print "insert_in_bench: Inserted $rows rows\n";

  # Kill other threads
  open(KILLFILE, "> $kill_file");
  close(KILLFILE);

  exit(0);
}


sub select_from_bench
{
  my ($dbh,$rows,$cursor);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;
  for ($rows= 1; $rows < $opt_loop_count ; $rows++)
  {
    $t_value = rand 100000;
    $t_value2 = $t_value+10000;
    $statement = "INSERT INTO $secondtable (c_pollid, c_min_time, c_max_time
, c_min_data, c_max_data, c_avg_data, c_error, c_warning, c_okay, c_unknown, c_rolled_up) SELECT c_pollid, MIN(c_time), MAX(c_time), MIN(c_data), MAX(c_data), AVG(c_data), SUM(c_error), SUM(c_warning), SUM(c_okay), SUM(c_unknown), 0 FROM $firsttable WHERE (c_time>=$t_value) AND (c_time<$t_value2) AND (c_rolled_up=0) GROUP BY c_pollid";
    $cursor = $dbh->prepare($statement);
    $cursor->execute();
    $cursor->finish();
    sleep 1;
    if (-e $kill_file)
    {
      last;
    }
  }
  print "select_from_bench: insert-select executed $rows times\n";
  exit(0);
}


sub delete_from_bench
{
  my ($dbh,$row, $t_value, $t2_value, $statement, $cursor);

  $dbh = DBI->connect("DBI:MariaDB:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  for ($rows= 1; $rows < $opt_loop_count ; $rows++)
  {
    $t_value = rand 50000;
    $t2_value = $t_value + 50001;
    $statement = "DELETE FROM $firsttable WHERE (c_time>$t_value) AND (c_time<$t2_value)";
    $cursor = $dbh->prepare($statement);
    $cursor->execute();
    $cursor->finish();
    sleep 10;
    if (-e $kill_file)
    {
      last;
    }
  }
  print "delete: delete executed $rows times\n";
  exit(0);
}