3
# This is a test with uses 5 processes to insert, update and select from
5
# One inserts records in the tables, one updates some record in it and
6
# the last 3 does different selects on the tables.
7
# Er, hmmm..., something like that :^)
8
# Modified to do crazy-join, � la Nasdaq.
10
# This test uses the old obsolete mysql interface. For a test that uses
11
# DBI, please take a look at fork_big.pl
13
$opt_loop_count=10000; # Change this to make test harder/easier
15
##################### Standard benchmark inits ##############################
23
$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
24
$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
25
$opt_host=""; $opt_db="test";
27
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
28
"skip-delete", "verbose","fast-insert","lock-tables","debug","fast",
29
"force") || die "Aborted";
30
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$Mysql::db_errstr=$opt_force=undef; # Ignore warnings from these
32
print "Testing 10 multiple connections to a server with 1 insert/update\n";
33
print "and 8 select connections and one ALTER TABLE.\n";
36
@testtables = qw(bench_f21 bench_f22 bench_f23 bench_f24 bench_f25);
37
$numtables = $#testtables; # make emacs happier
38
$dtable = "directory";
40
#### Start timeing and start test
43
$start_time=new Benchmark;
44
if (!$opt_skip_create)
46
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
48
foreach $table (@testtables) {
49
$dbh->Query("drop table $table");
51
$dbh->Query("drop table $dtable");
54
foreach $table (@testtables) {
55
print "Creating table $table in database $opt_db\n";
56
$dbh->Query("create table $table".
57
" (id int(6) not null,".
61
or die $Mysql::db_errstr;
63
print "Creating directory table $dtable in $opt_db\n";
64
$dbh->Query("create table $dtable (id int(6), last int(6))")
65
or die $Mysql::db_errstr;
66
# Populate directory table
67
for $i ( 0 .. $numtables ) {
68
$dbh->Query("insert into $dtable values($i, 0)");
70
$dbh=0; # Close handler
80
test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
81
test_2() if (($pid=fork()) == 0); $work{$pid}="simple1";
82
test_3() if (($pid=fork()) == 0); $work{$pid}="funny1";
83
test_2() if (($pid=fork()) == 0); $work{$pid}="simple2";
84
test_3() if (($pid=fork()) == 0); $work{$pid}="funny2";
85
test_2() if (($pid=fork()) == 0); $work{$pid}="simple3";
86
test_3() if (($pid=fork()) == 0); $work{$pid}="funny3";
87
test_2() if (($pid=fork()) == 0); $work{$pid}="simple4";
88
test_3() if (($pid=fork()) == 0); $work{$pid}="funny4";
89
alter_test() if (($pid=fork()) == 0); $work{$pid}="alter";
92
while (($pid=wait()) != -1)
95
print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
96
$errors++ if ($ret != 0);
99
if (!$opt_skip_delete && !$errors)
101
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
102
foreach $table (@testtables) {
103
$dbh->Query("drop table $table");
106
print ($errors ? "Test failed\n" :"Test ok\n");
108
$end_time=new Benchmark;
109
print "Total time: " .
110
timestr(timediff($end_time, $start_time),"noc") . "\n";
115
# Insert records in the ?? tables the Nasdaq way
120
my ($dbh,$table,$tmpvar,$rows,$found,$i);
122
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
125
for ($i=0 ; $i < $opt_loop_count; $i++)
127
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
129
$sth=$dbh->Query("select id,last from $dtable where id='$tmpvar'")
130
or die "Select directory row: $Mysql::db_errstr\n";
132
my ($did,$dlast) = $sth->FetchRow
133
or die "Fetch directory row: $Mysql::db_errstr\n";
135
$sth=$dbh->Query("INSERT into $testtables[$did]".
136
" VALUES($dlast,'This is entry $dlast',NULL)")
137
|| die "Got error on insert table $testtable[$did]:".
138
" $Mysql::db_errstr\n";
139
# Nasdaq step 3 - where my application hangs
140
$sth=$dbh->Query("update $dtable set last='$dlast' where id='$tmpvar'")
141
or die "Updating directory for table $testtable[$did]:".
142
" Mysql::db_errstr\n";
146
print "Test_1: Inserted $rows rows\n";
151
# Nasdaq simple select
156
my ($dbh,$id,$tmpvar,$rows,$found,$i);
158
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
161
for ($i=0 ; $i < $opt_loop_count ; $i++)
163
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
164
$sth=$dbh->Query("select a.id,a.info from $testtables[$tmpvar] as a,".
166
" where a.id=d.last and $i >= 0")
167
|| die "Got error select max: $Mysql::db_errstr\n";
168
if ((@row = $sth->FetchRow()) && defined($row[0]))
174
print "Test_2: Found $found rows\n";
180
# Nasdaq not-so-simple select
185
my ($dbh,$id,$tmpvar,$rows,$i);
186
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
188
$tmpvar ||= $numtables;
189
for ($i=0 ; $i < $opt_loop_count ; $i++)
191
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
192
$id1 = ($tmpvar+1) % $numtables;
193
$id2 = ($id1+1) % $numtables;
194
$id3 = ($id2+1) % $numtables;
195
$sth = $dbh->Query("SELECT greatest(a.id, b.id, c.id), a.info".
196
" FROM $testtables[$id1] as a,".
197
" $testtables[$id2] as b,".
198
" $testtables[$id3] as c,".
199
" $dtable as d1, $dtable as d2, $dtable as d3".
201
" d1.last=a.id AND d2.last=b.id AND d3.last=c.id".
202
" AND d1.id='$id1' AND d2.id='$id2'".
204
or die "Funny select: $Mysql::db_errstr\n";
205
$rows+=$sth->numrows;
208
print "Test_3: Found $rows rows\n";
213
# Do an ALTER TABLE every 20 seconds
218
my ($dbh,$count,$old_row_count,$row_count,$id,@row,$sth);
220
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
221
$id=$count=$row_count=0; $old_row_count= -1;
223
# Execute the test as long as we get more data into the table
224
while ($row_count != $old_row_count)
227
$sth=$dbh->Query("ALTER TABLE $testtables[$id] modify info varchar(32)") or die "Couldn't execute ALTER TABLE\n";
229
$id=($id+1) % $numtables;
231
# Test if insert test has ended
232
$sth=$dbh->query("select count(*) from $testtables[0]") or die "Couldn't execute count(*)\n";
233
@row = $sth->FetchRow();
234
$old_row_count= $row_count;
239
print "alter: Executed $count ALTER TABLE commands\n";