1
by brian
clean slate |
1 |
#!/usr/bin/perl -w
|
2 |
#
|
|
3 |
# This is a test with uses processes to insert, select and drop tables.
|
|
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_delete=$opt_skip_flush=0; |
|
17 |
$opt_host=""; $opt_db="test"; |
|
18 |
||
19 |
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-delete", |
|
20 |
"skip-flush") || die "Aborted"; |
|
21 |
||
22 |
print "Testing 5 multiple connections to a server with 1 insert, 1 rename\n"; |
|
23 |
print "1 select and 1 flush thread\n"; |
|
24 |
||
25 |
$firsttable = "bench_f1"; |
|
26 |
||
27 |
####
|
|
28 |
#### Start timing and start test
|
|
29 |
####
|
|
30 |
||
31 |
$start_time=new Benchmark; |
|
32 |
if (!$opt_skip_create) |
|
33 |
{
|
|
34 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
35 |
$opt_user, $opt_password, |
|
36 |
{ PrintError => 0}) || die $DBI::errstr; |
|
37 |
$dbh->do("drop table if exists $firsttable, ${firsttable}_1, ${firsttable}_2"); |
|
38 |
||
39 |
print "Creating table $firsttable in database $opt_db\n"; |
|
40 |
$dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr; |
|
41 |
$dbh->disconnect; $dbh=0; # Close handler |
|
42 |
}
|
|
43 |
$|= 1; # Autoflush |
|
44 |
||
45 |
####
|
|
46 |
#### Start the tests
|
|
47 |
####
|
|
48 |
||
49 |
test_insert() if (($pid=fork()) == 0); $work{$pid}="insert"; |
|
50 |
test_rename(1) if (($pid=fork()) == 0); $work{$pid}="rename 1"; |
|
51 |
test_rename(2) if (($pid=fork()) == 0); $work{$pid}="rename 2"; |
|
52 |
test_select() if (($pid=fork()) == 0); $work{$pid}="select"; |
|
53 |
if (!$opt_skip_flush) |
|
54 |
{
|
|
55 |
test_flush() if (($pid=fork()) == 0); $work{$pid}="flush"; |
|
56 |
}
|
|
57 |
$errors=0; |
|
58 |
while (($pid=wait()) != -1) |
|
59 |
{
|
|
60 |
$ret=$?/256; |
|
61 |
print "thread '" . $work{$pid} . "' finished with exit code $ret\n"; |
|
62 |
$errors++ if ($ret != 0); |
|
63 |
}
|
|
64 |
||
65 |
if (!$opt_skip_delete && !$errors) |
|
66 |
{
|
|
67 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
68 |
$opt_user, $opt_password, |
|
69 |
{ PrintError => 0}) || die $DBI::errstr; |
|
70 |
$dbh->do("drop table $firsttable"); |
|
71 |
$dbh->disconnect; $dbh=0; # Close handler |
|
72 |
}
|
|
73 |
print ($errors ? "Test failed\n" :"Test ok\n"); |
|
74 |
||
75 |
$end_time=new Benchmark; |
|
76 |
print "Total time: " . |
|
77 |
timestr(timediff($end_time, $start_time),"noc") . "\n"; |
|
78 |
||
79 |
exit(0); |
|
80 |
||
81 |
#
|
|
82 |
# Insert records in the table. Delete table when test is finished
|
|
83 |
#
|
|
84 |
||
85 |
sub test_insert |
|
86 |
{
|
|
87 |
my ($dbh,$i,$error); |
|
88 |
||
89 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
90 |
$opt_user, $opt_password, |
|
91 |
{ PrintError => 0}) || die $DBI::errstr; |
|
92 |
for ($i=0 ; $i < $opt_loop_count; $i++) |
|
93 |
{
|
|
94 |
if (!$dbh->do("insert into $firsttable values ($i,'This is entry $i','')")) |
|
95 |
{
|
|
96 |
$error=$dbh->errstr; |
|
97 |
$dbh->do("drop table ${firsttable}"); # End other threads |
|
98 |
die "Warning; Got error on insert: " . $error . "\n"; |
|
99 |
}
|
|
100 |
}
|
|
101 |
sleep(1); |
|
102 |
$dbh->do("drop table ${firsttable}") || die "Got error on drop table: " . $dbh->errstr . "\n"; |
|
103 |
$dbh->disconnect; $dbh=0; |
|
104 |
print "Test_insert: Inserted $i rows\n"; |
|
105 |
exit(0); |
|
106 |
}
|
|
107 |
||
108 |
||
109 |
sub test_rename |
|
110 |
{
|
|
111 |
my ($id) = @_; |
|
112 |
my ($dbh,$i,$error_counter,$sleep_time); |
|
113 |
||
114 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
115 |
$opt_user, $opt_password, |
|
116 |
{ PrintError => 0}) || die $DBI::errstr; |
|
117 |
$error_counter=0; |
|
118 |
$sleep_time=2; |
|
119 |
for ($i=0 ; $i < $opt_loop_count ; $i++) |
|
120 |
{
|
|
121 |
sleep($sleep_time); |
|
122 |
$dbh->do("create table ${firsttable}_$id (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr; |
|
123 |
if (!$dbh->do("rename table $firsttable to ${firsttable}_${id}_1, ${firsttable}_$id to ${firsttable}")) |
|
124 |
{
|
|
125 |
last if ($dbh->errstr =~ /^Can\'t find/); |
|
126 |
die "Got error on rename: " . $dbh->errstr . "\n"; |
|
127 |
}
|
|
128 |
$dbh->do("drop table ${firsttable}_${id}_1") || die "Got error on drop table: " . $dbh->errstr . "\n"; |
|
129 |
}
|
|
130 |
$dbh->disconnect; $dbh=0; |
|
131 |
print "Test_drop: Did a drop $i times\n"; |
|
132 |
exit(0); |
|
133 |
}
|
|
134 |
||
135 |
||
136 |
#
|
|
137 |
# select records
|
|
138 |
#
|
|
139 |
||
140 |
sub test_select |
|
141 |
{
|
|
142 |
my ($dbh,$i,$sth,@row,$sleep_time); |
|
143 |
||
144 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
145 |
$opt_user, $opt_password, |
|
146 |
{ PrintError => 0}) || die $DBI::errstr; |
|
147 |
||
148 |
$sleep_time=3; |
|
149 |
for ($i=0 ; $i < $opt_loop_count ; $i++) |
|
150 |
{
|
|
151 |
sleep($sleep_time); |
|
152 |
$sth=$dbh->prepare("select sum(t.id) from $firsttable as t,$firsttable as t2") || die "Got error on select: $dbh->errstr;\n"; |
|
153 |
if ($sth->execute) |
|
154 |
{
|
|
155 |
@row = $sth->fetchrow_array(); |
|
156 |
$sth->finish; |
|
157 |
}
|
|
158 |
else
|
|
159 |
{
|
|
160 |
$sth->finish; |
|
161 |
last if (! ($dbh->errstr =~ /doesn\'t exist/)); |
|
162 |
die "Got error on select: " . $dbh->errstr . "\n"; |
|
163 |
}
|
|
164 |
}
|
|
165 |
$dbh->disconnect; $dbh=0; |
|
166 |
print "Test_select: ok\n"; |
|
167 |
exit(0); |
|
168 |
}
|
|
169 |
||
170 |
#
|
|
171 |
# flush records
|
|
172 |
#
|
|
173 |
||
174 |
sub test_flush |
|
175 |
{
|
|
176 |
my ($dbh,$i,$sth,@row,$error_counter,$sleep_time); |
|
177 |
||
178 |
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host", |
|
179 |
$opt_user, $opt_password, |
|
180 |
{ PrintError => 0}) || die $DBI::errstr; |
|
181 |
||
182 |
$error_counter=0; |
|
183 |
$sleep_time=5; |
|
184 |
for ($i=0 ; $i < $opt_loop_count ; $i++) |
|
185 |
{
|
|
186 |
sleep($sleep_time); |
|
187 |
$sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepar: $dbh->errstr;\n"; |
|
188 |
if ($sth->execute) |
|
189 |
{
|
|
190 |
@row = $sth->fetchrow_array(); |
|
191 |
$sth->finish; |
|
192 |
$sleep_time=5; |
|
193 |
$dbh->do("flush tables $firsttable") || die "Got error on flush table: " . $dbh->errstr . "\n"; |
|
194 |
}
|
|
195 |
else
|
|
196 |
{
|
|
197 |
last if (! ($dbh->errstr =~ /doesn\'t exist/)); |
|
198 |
die "Got error on flush: " . $dbh->errstr . "\n"; |
|
199 |
}
|
|
200 |
}
|
|
201 |
$dbh->disconnect; $dbh=0; |
|
202 |
print "Test_select: ok\n"; |
|
203 |
exit(0); |
|
204 |
}
|