~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
#!/usr/bin/perl -w
2
#
3
# This is a test for a key cache bug (bug #10167)
4
# To expose the bug mysqld should be started with --key-buffer-size=64K
5
#
6
7
$opt_loop_count=100000; # Change this to make test harder/easier
8
9
##################### Standard benchmark inits ##############################
10
11
use DBI;
12
use Getopt::Long;
13
use Benchmark;
14
15
package main;
16
17
$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
18
  $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
19
$opt_host=$opt_user=$opt_password=""; $opt_db="test";
20
21
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
22
	   "skip-delete","verbose","fast-insert","lock-tables","debug","fast",
23
	   "force","user=s","password=s") || die "Aborted";
24
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
25
26
$firsttable  = "bench_f1";
27
$secondtable = "bench_f2";
28
$kill_file= "/tmp/mysqltest_index_corrupt.$$";
29
30
####
31
####  Start timeing and start test
32
####
33
34
$start_time=new Benchmark;
35
if (!$opt_skip_create)
36
{
37
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
38
		      $opt_user, $opt_password,
39
		    { PrintError => 0}) || die $DBI::errstr;
40
  $dbh->do("drop table if exists $firsttable, $secondtable");
41
42
  print "Creating tables in $opt_db\n";
43
  $dbh->do("create table $firsttable (
44
c_pollid    INTEGER  NOT NULL,
45
c_time      BIGINT   NOT NULL,
46
c_data      DOUBLE   NOT NULL,
47
c_error     INTEGER  NOT NULL,
48
c_warning   INTEGER  NOT NULL,
49
c_okay      INTEGER  NOT NULL,
50
c_unknown   INTEGER  NOT NULL,
51
c_rolled_up BIT      NOT NULL,
52
INDEX t_mgmt_hist_r_i1 (c_pollid),
53
INDEX t_mgmt_hist_r_i2 (c_time),
54
INDEX t_mgmt_hist_r_i3 (c_rolled_up))") or die $DBI::errstr;
55
56
  $dbh->do("create table $secondtable (
57
c_pollid  INTEGER  NOT NULL,
58
c_min_time  BIGINT   NOT NULL,
59
c_max_time  BIGINT   NOT NULL,
60
c_min_data  DOUBLE   NOT NULL,
61
c_max_data  DOUBLE   NOT NULL,
62
c_avg_data  DOUBLE   NOT NULL,
63
c_error     INTEGER  NOT NULL,
64
c_warning   INTEGER  NOT NULL,
65
c_okay      INTEGER  NOT NULL,
66
c_unknown   INTEGER  NOT NULL,
67
c_rolled_up BIT      NOT NULL,
68
INDEX t_mgmt_hist_d_i1 (c_pollid),
69
INDEX t_mgmt_hist_d_i2 (c_min_time),
70
INDEX t_mgmt_hist_d_i3 (c_max_time),
71
INDEX t_mgmt_hist_d_i4 (c_rolled_up))") or die $DBI::errstr;
72
73
74
  $dbh->disconnect; $dbh=0;	# Close handler
75
}
76
$|= 1;				# Autoflush
77
78
####
79
#### Start the tests
80
####
81
82
print "Running tests\n";
83
insert_in_bench() if (($pid=fork()) == 0); $work{$pid}="insert";
84
select_from_bench() if (($pid=fork()) == 0); $work{$pid}="insert-select;
85
delete_from_bench() if (($pid=fork()) == 0); $work{$pid}="delete";
86
87
$errors=0;
88
while (($pid=wait()) != -1)
89
{
90
  $ret=$?/256;
91
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
92
  $errors++ if ($ret != 0);
93
}
94
95
if (!$opt_skip_delete && !$errors)
96
{
97
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
98
		      $opt_user, $opt_password,
99
		    { PrintError => 0}) || die $DBI::errstr;
100
  $dbh->do("drop table $firsttable, $secondtable");
101
}
102
print ($errors ? "Test failed\n" :"Test ok\n");
103
104
$end_time=new Benchmark;
105
print "Total time: " .
106
  timestr(timediff($end_time, $start_time),"noc") . "\n";
107
108
unlink $kill_file;
109
110
exit(0);
111
112
#
113
# Insert records in the two tables
114
#
115
116
sub insert_in_bench
117
{
118
  my ($dbh,$rows,$found,$i);
119
120
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
121
		      $opt_user, $opt_password,
122
		    { PrintError => 0}) || die $DBI::errstr;
123
  for ($rows= 1; $rows <= $opt_loop_count ; $rows++)
124
  {
125
    $c_pollid = sprintf("%d",rand 1000);
126
    $c_time = sprintf("%d",rand 100000);
127
    $c_data = rand 1000000;
128
    $test = rand 1;
129
    $c_error=0;
130
    $c_warning=0;
131
    $c_okay=0;
132
    $c_unknown=0;
133
    if ($test < .8) {
134
      $c_okay=1;
135
    } elsif ($test <.9) {
136
      $c_error=1;
137
    } elsif ($test <.95) {
138
      $c_warning=1;
139
    } else {
140
      $c_unknown=1;
141
    }
142
    $statement = "INSERT INTO $firsttable (c_pollid, c_time, c_data, c_error
143
, c_warning, c_okay, c_unknown, c_rolled_up) ".
144
  "VALUES ($c_pollid,$c_time,$c_data,$c_error,$c_warning,$c_okay,$c_unknown,0)";
145
    $cursor = $dbh->prepare($statement);
146
    $cursor->execute();
147
    $cursor->finish();
148
  }
149
150
  $dbh->disconnect; $dbh=0;
151
  print "insert_in_bench: Inserted $rows rows\n";
152
153
  # Kill other threads
154
  open(KILLFILE, "> $kill_file");
155
  close(KILLFILE);
156
157
  exit(0);
158
}
159
160
161
sub select_from_bench
162
{
163
  my ($dbh,$rows,$cursor);
164
165
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
166
		      $opt_user, $opt_password,
167
		    { PrintError => 0}) || die $DBI::errstr;
168
  for ($rows= 1; $rows < $opt_loop_count ; $rows++)
169
  {
170
    $t_value = rand 100000;
171
    $t_value2 = $t_value+10000;
172
    $statement = "INSERT INTO $secondtable (c_pollid, c_min_time, c_max_time
173
, 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";
174
    $cursor = $dbh->prepare($statement);
175
    $cursor->execute();
176
    $cursor->finish();
177
    sleep 1;
178
    if (-e $kill_file)
179
    {
180
      last;
181
    }
182
  }
183
  print "select_from_bench: insert-select executed $rows times\n";
184
  exit(0);
185
}
186
187
188
sub delete_from_bench
189
{
190
  my ($dbh,$row, $t_value, $t2_value, $statement, $cursor);
191
192
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
193
		      $opt_user, $opt_password,
194
		    { PrintError => 0}) || die $DBI::errstr;
195
196
  for ($rows= 1; $rows < $opt_loop_count ; $rows++)
197
  {
198
    $t_value = rand 50000;
199
    $t2_value = $t_value + 50001;
200
    $statement = "DELETE FROM $firsttable WHERE (c_time>$t_value) AND (c_time<$t2_value)";
201
    $cursor = $dbh->prepare($statement);
202
    $cursor->execute();
203
    $cursor->finish();
204
    sleep 10;
205
    if (-e $kill_file)
206
    {
207
      last;
208
    }
209
  }
210
  print "delete: delete executed $rows times\n";
211
  exit(0);
212
}