1
by brian
clean slate |
1 |
#!/usr/bin/perl -w
|
2 |
#
|
|
3 |
# This is a test of insert and repair/check.
|
|
4 |
#
|
|
5 |
||
6 |
$opt_loop_count=100000; # Change this to make test harder/easier |
|
7 |
||
8 |
##################### Standard benchmark inits ##############################
|
|
9 |
||
10 |
use DBI; |
|
11 |
use Getopt::Long; |
|
12 |
use Benchmark; |
|
13 |
||
14 |
package main; |
|
15 |
||
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"; |
|
19 |
||
20 |
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in", |
|
21 |
"skip-delete","verbose","fast-insert","lock-tables","debug","fast", |
|
22 |
"force","user=s","password=s") || die "Aborted"; |
|
23 |
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef; # Ignore warnings from these |
|
24 |
||
25 |
$firsttable = "bench_f1"; |
|
26 |
$secondtable = "bench_f2"; |
|
27 |
||
28 |
####
|
|
29 |
#### Start timeing and start test
|
|
30 |
####
|
|
31 |
||
32 |
$start_time=new Benchmark; |
|
33 |
if (!$opt_skip_create) |
|
34 |
{
|
|
35 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
36 |
$opt_user, $opt_password, |
|
37 |
{ PrintError => 0}) || die $DBI::errstr; |
|
38 |
$dbh->do("drop table if exists $firsttable, $secondtable"); |
|
39 |
||
40 |
print "Creating tables $firsttable and $secondtable in database $opt_db\n"; |
|
41 |
$dbh->do("create table $firsttable (id int(7) not null, thread tinyint not null, info varchar(32), marker char(1), primary key(id,thread))") or die $DBI::errstr; |
|
42 |
$dbh->do("create table $secondtable (id int(7) not null, thread tinyint not null, row int(3) not null,value double, primary key(id,thread,row)) delay_key_write=1") or die $DBI::errstr; |
|
43 |
$dbh->disconnect; $dbh=0; # Close handler |
|
44 |
}
|
|
45 |
$|= 1; # Autoflush |
|
46 |
||
47 |
####
|
|
48 |
#### Start the tests
|
|
49 |
####
|
|
50 |
||
51 |
insert_in_bench1() if (($pid=fork()) == 0); $work{$pid}="insert in bench1"; |
|
52 |
insert_in_bench2() if (($pid=fork()) == 0); $work{$pid}="insert in bench2"; |
|
53 |
repair_and_check() if (($pid=fork()) == 0); $work{$pid}="repair/check"; |
|
54 |
||
55 |
$errors=0; |
|
56 |
while (($pid=wait()) != -1) |
|
57 |
{
|
|
58 |
$ret=$?/256; |
|
59 |
print "thread '" . $work{$pid} . "' finished with exit code $ret\n"; |
|
60 |
$errors++ if ($ret != 0); |
|
61 |
}
|
|
62 |
||
63 |
if (!$opt_skip_delete && !$errors) |
|
64 |
{
|
|
65 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
66 |
$opt_user, $opt_password, |
|
67 |
{ PrintError => 0}) || die $DBI::errstr; |
|
68 |
$dbh->do("drop table $firsttable,$secondtable"); |
|
69 |
}
|
|
70 |
print ($errors ? "Test failed\n" :"Test ok\n"); |
|
71 |
||
72 |
$end_time=new Benchmark; |
|
73 |
print "Total time: " . |
|
74 |
timestr(timediff($end_time, $start_time),"noc") . "\n"; |
|
75 |
||
76 |
exit(0); |
|
77 |
||
78 |
#
|
|
79 |
# Insert records in the two tables
|
|
80 |
#
|
|
81 |
||
82 |
sub insert_in_bench1 |
|
83 |
{
|
|
84 |
my ($dbh,$rows,$found,$i); |
|
85 |
||
86 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
87 |
$opt_user, $opt_password, |
|
88 |
{ PrintError => 0}) || die $DBI::errstr; |
|
89 |
$rows=$found=0; |
|
90 |
for ($i=0 ; $i < $opt_loop_count; $i++) |
|
91 |
{
|
|
92 |
$sth=$dbh->do("insert into $firsttable values ($i,0,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n"; |
|
93 |
$row_count=($i % 7)+1; |
|
94 |
$rows+=1+$row_count; |
|
95 |
for ($j=0 ; $j < $row_count; $j++) |
|
96 |
{
|
|
97 |
$sth=$dbh->do("insert into $secondtable values ($i,0,$j,0)") || die "Got error on insert: $DBI::errstr\n"; |
|
98 |
}
|
|
99 |
}
|
|
100 |
$dbh->disconnect; $dbh=0; |
|
101 |
print "insert_in_bench1: Inserted $rows rows\n"; |
|
102 |
exit(0); |
|
103 |
}
|
|
104 |
||
105 |
sub insert_in_bench2 |
|
106 |
{
|
|
107 |
my ($dbh,$rows,$found,$i); |
|
108 |
||
109 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
110 |
$opt_user, $opt_password, |
|
111 |
{ PrintError => 0}) || die $DBI::errstr; |
|
112 |
$rows=$found=0; |
|
113 |
for ($i=0 ; $i < $opt_loop_count; $i++) |
|
114 |
{
|
|
115 |
$sth=$dbh->do("insert into $firsttable values ($i,1,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n"; |
|
116 |
$row_count=((7-$i) % 7)+1; |
|
117 |
$rows+=1+$row_count; |
|
118 |
for ($j=0 ; $j < $row_count; $j++) |
|
119 |
{
|
|
120 |
$sth=$dbh->do("insert into $secondtable values ($i,1,$j,0)") || die "Got error on insert: $DBI::errstr\n"; |
|
121 |
}
|
|
122 |
}
|
|
123 |
$dbh->disconnect; $dbh=0; |
|
124 |
print "insert_in_bench2: Inserted $rows rows\n"; |
|
125 |
exit(0); |
|
126 |
}
|
|
127 |
||
128 |
||
129 |
sub repair_and_check |
|
130 |
{
|
|
131 |
my ($dbh,$row,@row,$found1,$found2,$last_found1,$last_found2,$i,$type, |
|
132 |
$table); |
|
133 |
$found1=$found2=0; $last_found1=$last_found2= -1; |
|
134 |
||
135 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
136 |
$opt_user, $opt_password, |
|
137 |
{ PrintError => 0}) || die $DBI::errstr; |
|
138 |
||
139 |
for ($i=0; $found1 != $last_found1 && $found2 != $last_found1 ; $i++) |
|
140 |
{
|
|
141 |
$type=($i & 2) ? "repair" : "check"; |
|
142 |
if ($i & 1) |
|
143 |
{
|
|
144 |
$table=$firsttable; |
|
145 |
$last_found1=$found1; |
|
146 |
}
|
|
147 |
else
|
|
148 |
{
|
|
149 |
$table=$secondtable; |
|
150 |
$last_found2=$found2; |
|
151 |
}
|
|
152 |
$sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $dbh->errstr\n"; |
|
153 |
$sth->execute || die $dbh->errstr; |
|
154 |
||
155 |
while (($row=$sth->fetchrow_arrayref)) |
|
156 |
{
|
|
157 |
if ($row->[3] ne "OK") |
|
158 |
{
|
|
159 |
print "Got error " . $row->[3] . " when doing $type on $table\n"; |
|
160 |
exit(1); |
|
161 |
}
|
|
162 |
}
|
|
163 |
$sth=$dbh->prepare("select count(*) from $table") || die "Got error on prepare: $dbh->errstr\n"; |
|
164 |
$sth->execute || die $dbh->errstr; |
|
165 |
@row = $sth->fetchrow_array(); |
|
166 |
if ($i & 1) |
|
167 |
{
|
|
168 |
$found1= $row[0]; |
|
169 |
}
|
|
170 |
else
|
|
171 |
{
|
|
172 |
$found2= $row[0]; |
|
173 |
}
|
|
174 |
$sth->finish; |
|
175 |
sleep(2); |
|
176 |
}
|
|
177 |
$dbh->disconnect; $dbh=0; |
|
178 |
print "check/repair: Did $i repair/checks\n"; |
|
179 |
exit(0); |
|
180 |
}
|