3
# This is a test for INSERT DELAYED
6
$opt_loop_count=10000; # Change this to make test harder/easier
8
##################### Standard benchmark inits ##############################
16
$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
17
$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
18
$opt_host=$opt_user=$opt_password=""; $opt_db="test";
20
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
21
"verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
22
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef; # Ignore warnings from these
24
print "Testing 8 multiple connections to a server with 1 insert, 2 delayed\n";
25
print "insert, 1 update, 1 delete, 1 flush tables and 3 select connections.\n";
27
$firsttable = "bench_f1";
28
$secondtable = "bench_f2";
31
#### Start timeing and start test
34
$start_time=new Benchmark;
35
if (!$opt_skip_create)
37
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
39
$dbh->do("drop table if exists $firsttable,$secondtable");
42
print "Creating tables $firsttable and $secondtable in database $opt_db\n";
43
$dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") or die $DBI::errstr;
44
$dbh->do("create table $secondtable (id int(6) not null, row int(3) not null,value double, primary key(id,row))") or die $DBI::errstr;
54
test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
55
test_delayed_1() if (($pid=fork()) == 0); $work{$pid}="delayed_insert1";
56
test_delayed_2() if (($pid=fork()) == 0); $work{$pid}="delayed_insert2";
57
test_2() if (($pid=fork()) == 0); $work{$pid}="update";
58
test_3() if (($pid=fork()) == 0); $work{$pid}="select1";
59
test_4() if (($pid=fork()) == 0); $work{$pid}="select2";
60
test_5() if (($pid=fork()) == 0); $work{$pid}="select3";
61
test_del() if (($pid=fork()) == 0); $work{$pid}="delete";
62
test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
65
while (($pid=wait()) != -1)
68
print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
69
$errors++ if ($ret != 0);
72
if (!$opt_skip_delete && !$errors)
74
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
75
$dbh->do("drop table $firsttable");
76
$dbh->do("drop table $secondtable");
78
print ($errors ? "Test failed\n" :"Test ok\n");
80
$end_time=new Benchmark;
81
print "Total time: " .
82
timestr(timediff($end_time, $start_time),"noc") . "\n";
87
# Insert records in the two tables
92
my ($dbh,$tmpvar,$rows,$found,$i);
94
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
97
for ($i=0 ; $i < $opt_loop_count; $i++)
99
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
100
$dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
101
$row_count=($i % 7)+1;
103
for ($j=0 ; $j < $row_count; $j++)
105
$dbh->do("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $DBI::errstr\n";
109
print "Test_1: Inserted $rows rows\n";
116
my ($dbh,$tmpvar,$rows,$found,$i,$id);
118
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
121
for ($i=0 ; $i < $opt_loop_count; $i++)
123
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
124
$id=$i+$opt_loop_count;
125
$dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
126
$row_count=($i % 7)+1;
128
for ($j=0 ; $j < $row_count; $j++)
130
$dbh->do("insert into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
132
if (($tmpvar % 100) == 0)
134
$dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
135
$dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";
140
print "Test_1: Inserted delayed $rows rows, found $found rows\n";
147
my ($dbh,$tmpvar,$rows,$found,$i,$id);
149
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
152
for ($i=0 ; $i < $opt_loop_count; $i++)
154
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
155
$id=$i+$opt_loop_count*2;
156
$dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
157
$row_count=($i % 7)+1;
159
for ($j=0 ; $j < $row_count; $j++)
161
$dbh->do("insert delayed into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
163
if (($tmpvar % 100) == 0)
165
$dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
166
$dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";
171
print "Test_1: Inserted delayed $rows rows, found $found rows\n";
176
# Update records in both tables
181
my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp,$sth,$count);
183
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
185
$rows=$found=$max_id=$id=0;
186
for ($i=0 ; $i < $opt_loop_count ; $i++)
188
$tmp=(($tmpvar + 63) + $i)*3;
189
$tmp=$tmp-int($tmp/100000)*100000;
191
$tmp=$tmpvar - int($tmpvar/10)*10;
192
if ($max_id*$tmp == 0)
195
$sth=$dbh->prepare("select max(id) from $firsttable where marker=''");
196
$sth->execute() || die "Got error select max: $DBI::errstr\n";
197
if ((@row = $sth->fetchrow_array()) && defined($row[0]))
206
$id= $tmpvar % ($max_id-1)+1;
210
($count=$dbh->do("update $firsttable set marker='x' where id=$id")) || die "Got error update $firsttable: $DBI::errstr\n";
214
$count=$dbh->do("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $DBI::errstr\n";
220
print "Test_2: Found $found rows, Updated $rows rows\n";
231
my ($dbh,$id,$tmpvar,$rows,$i,$count);
232
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
235
for ($i=0 ; $i < $opt_loop_count ; $i++)
237
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
238
$id=$tmpvar % $opt_loop_count;
239
$count=$dbh->do("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $DBI::errstr\n";
243
print "Test_3: Found $rows rows\n";
249
# Note that this uses row=1 and in some cases won't find any matching
255
my ($dbh,$id,$tmpvar,$rows,$i,$count);
256
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
259
for ($i=0 ; $i < $opt_loop_count; $i++)
261
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
262
$id=$tmpvar % $opt_loop_count;
263
$count=$dbh->do("select id from $secondtable where id=$id") || die "Got error on select from $secondtable: $DBI::errstr\n";
267
print "Test_4: Found $rows rows\n";
274
my ($dbh,$id,$tmpvar,$rows,$i,$max_id,$count,$sth);
275
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
278
for ($i=0 ; $i < $opt_loop_count ; $i++)
280
$tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
281
if ($max_id == 0 || ($tmpvar % 10 == 0))
283
$sth=$dbh->prepare("select max(id) from $firsttable");
284
$sth->execute() || die "Got error select max: $DBI::errstr\n";
285
if ((@row = $sth->fetchrow_array()) && defined($row[0]))
297
$id= $tmpvar % $max_id;
299
$count=$dbh->do("select value from $firsttable,$secondtable where $firsttable.id=$id and $secondtable.id=$firsttable.id") || die "Got error on select from $secondtable: $DBI::errstr\n";
303
print "Test_5: Found $rows rows\n";
309
# Delete the smallest row
314
my ($dbh,$min_id,$i,$sth,$rows);
315
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
317
for ($i=0 ; $i < $opt_loop_count/3; $i++)
319
$sth=$dbh->prepare("select min(id) from $firsttable");
320
$sth->execute() || die "Got error on select from $firsttable: $DBI::errstr\n";
321
if ((@row = $sth->fetchrow_array()) && defined($row[0]))
326
$dbh->do("delete from $firsttable where id = $min_id") || die "Got error on DELETE from $firsttable: $DBI::errstr\n";
330
print "Test_del: Deleted $rows rows\n";
336
# Do a flush tables once in a while
341
my ($dbh,$sth,$found1,$last_found1,$i,@row);
342
$found1=0; $last_found1=-1;
344
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
345
$opt_user, $opt_password,
346
{ PrintError => 0}) || die $DBI::errstr;
348
for ($i=0; $found1 != $last_found1 ; $i++)
350
$sth=$dbh->prepare("flush tables") || die "Got error on prepare: $dbh->errstr\n";
351
$sth->execute || die $dbh->errstr;
354
$sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepare: $dbh->errstr\n";
355
$sth->execute || die $dbh->errstr;
356
@row = $sth->fetchrow_array();
357
$last_found1=$found1;
362
$dbh->disconnect; $dbh=0;
363
print "flush: Did $i repair/checks\n";