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
$opt_loop_count=10000; # Change this to make test harder/easier
12
##################### Standard benchmark inits ##############################
20
$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
21
$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
22
$opt_host=""; $opt_db="test";
24
GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
25
"skip-delete", "verbose","fast-insert","lock-tables","debug","fast",
26
"force") || die "Aborted";
27
$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
29
print "Testing 9 multiple connections to a server with 1 insert/update\n";
30
print "and 8 select connections.\n";
33
@testtables = qw(bench_f21 bench_f22 bench_f23 bench_f24 bench_f25);
34
$numtables = $#testtables; # make emacs happier
35
$dtable = "directory";
37
#### Start timeing and start test
40
$start_time=new Benchmark;
41
if (!$opt_skip_create)
43
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
45
foreach $table (@testtables) {
46
$dbh->Query("drop table $table");
48
$dbh->Query("drop table $dtable");
51
foreach $table (@testtables) {
52
print "Creating table $table in database $opt_db\n";
53
$dbh->Query("create table $table".
54
" (id int(6) not null,".
58
or die $Mysql::db_errstr;
60
print "Creating directory table $dtable in $opt_db\n";
61
$dbh->Query("create table $dtable (id int(6), last int(6))")
62
or die $Mysql::db_errstr;
63
# Populate directory table
64
for $i ( 0 .. $numtables ) {
65
$dbh->Query("insert into $dtable values($i, 0)");
67
$dbh=0; # Close handler
77
test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
78
test_2() if (($pid=fork()) == 0); $work{$pid}="simple1";
79
test_3() if (($pid=fork()) == 0); $work{$pid}="funny1";
80
test_2() if (($pid=fork()) == 0); $work{$pid}="simple2";
81
test_3() if (($pid=fork()) == 0); $work{$pid}="funny2";
82
test_2() if (($pid=fork()) == 0); $work{$pid}="simple3";
83
test_3() if (($pid=fork()) == 0); $work{$pid}="funny3";
84
test_2() if (($pid=fork()) == 0); $work{$pid}="simple4";
85
test_3() if (($pid=fork()) == 0); $work{$pid}="funny4";
88
while (($pid=wait()) != -1)
91
print "thread '" . $work{$pid} . "' finnished with exit code $ret\n";
92
$errors++ if ($ret != 0);
95
if (!$opt_skip_delete && !$errors)
97
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
98
foreach $table (@testtables) {
99
$dbh->Query("drop table $table");
102
print ($errors ? "Test failed\n" :"Test ok\n");
104
$end_time=new Benchmark;
105
print "Total time: " .
106
timestr(timediff($end_time, $start_time),"noc") . "\n";
111
# Insert records in the ?? tables the Nasdaq way
116
my ($dbh,$table,$tmpvar,$rows,$found,$i);
118
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
121
for ($i=0 ; $i < $opt_loop_count; $i++)
123
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
125
$sth=$dbh->Query("select id,last from $dtable where id='$tmpvar'")
126
or die "Select directory row: $Mysql::db_errstr\n";
128
my ($did,$dlast) = $sth->FetchRow
129
or die "Fetch directory row: $Mysql::db_errstr\n";
131
$sth=$dbh->Query("INSERT into $testtables[$did]".
132
" VALUES($dlast,'This is entry $dlast',NULL)")
133
|| die "Got error on insert table $testtable[$did]:".
134
" $Mysql::db_errstr\n";
135
# Nasdaq step 3 - where my application hangs
136
$sth=$dbh->Query("update $dtable set last='$dlast' where id='$tmpvar'")
137
or die "Updating directory for table $testtable[$did]:".
138
" Mysql::db_errstr\n";
142
print "Test_1: Inserted $rows rows\n";
147
# Nasdaq simple select
152
my ($dbh,$id,$tmpvar,$rows,$found,$i);
154
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
157
for ($i=0 ; $i < $opt_loop_count ; $i++)
159
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
160
$sth=$dbh->Query("select a.id,a.info from $testtables[$tmpvar] as a,".
162
" where a.id=d.last and $i >= 0")
163
|| die "Got error select max: $Mysql::db_errstr\n";
164
if ((@row = $sth->FetchRow()) && defined($row[0]))
170
print "Test_2: Found $found rows\n";
176
# Nasdaq not-so-simple select
181
my ($dbh,$id,$tmpvar,$rows,$i);
182
$dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
184
$tmpvar ||= $numtables;
185
for ($i=0 ; $i < $opt_loop_count ; $i++)
187
$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
188
$id1 = ($tmpvar+1) % $numtables;
189
$id2 = ($id1+1) % $numtables;
190
$id3 = ($id2+1) % $numtables;
191
$sth = $dbh->Query("SELECT greatest(a.id, b.id, c.id), a.info".
192
" FROM $testtables[$id1] as a,".
193
" $testtables[$id2] as b,".
194
" $testtables[$id3] as c,".
195
" $dtable as d1, $dtable as d2, $dtable as d3".
197
" d1.last=a.id AND d2.last=b.id AND d3.last=c.id".
198
" AND d1.id='$id1' AND d2.id='$id2'".
200
or die "Funny select: $Mysql::db_errstr\n";
201
$rows+=$sth->numrows;
204
print "Test_3: Found $rows rows\n";