~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/perl -w

# This is a test with uses 5 processes to insert, update and select from
# two tables.
# One inserts records in the tables, one updates some record in it and
# the last 3 does different selects on the tables.
# Er, hmmm..., something like that :^)
# Modified to do crazy-join, à la Nasdaq.
#
# This test uses the old obsolete mysql interface. For a test that uses
# DBI, please take a look at fork_big.pl

$opt_loop_count=10000; # Change this to make test harder/easier

##################### Standard benchmark inits ##############################

use Mysql;
use Getopt::Long;
use Benchmark;

package main;

$opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
$opt_host=""; $opt_db="test";

GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in",
	   "skip-delete", "verbose","fast-insert","lock-tables","debug","fast",
	   "force") || die "Aborted";
$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

print "Testing 10 multiple connections to a server with 1 insert/update\n";
print "and 8 select connections and one ALTER TABLE.\n";


@testtables = qw(bench_f21 bench_f22 bench_f23 bench_f24 bench_f25);
$numtables = $#testtables;	# make emacs happier
$dtable = "directory";
####  
####  Start timeing and start test
####

$start_time=new Benchmark;
if (!$opt_skip_create)
{
  $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  $Mysql::QUIET = 1;
  foreach $table (@testtables) {
      $dbh->Query("drop table $table");
  }
  $dbh->Query("drop table $dtable");
  $Mysql::QUIET = 0;

  foreach $table (@testtables) {
      print "Creating table $table in database $opt_db\n";
      $dbh->Query("create table $table".
		  " (id int(6) not null,".
		  " info varchar(32),".
		  " marker timestamp,".
		  " primary key(id))")
	  or die $Mysql::db_errstr;
  }
  print "Creating directory table $dtable in $opt_db\n";
  $dbh->Query("create table $dtable (id int(6), last int(6))")
      or die $Mysql::db_errstr;
  # Populate directory table
  for $i ( 0 .. $numtables ) {
      $dbh->Query("insert into $dtable values($i, 0)");
  }
  $dbh=0;			# Close handler
}
$|= 1;				# Autoflush

####
#### Start the tests
####

#$test_index = 0;

test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
test_2() if (($pid=fork()) == 0); $work{$pid}="simple1";
test_3() if (($pid=fork()) == 0); $work{$pid}="funny1";
test_2() if (($pid=fork()) == 0); $work{$pid}="simple2";
test_3() if (($pid=fork()) == 0); $work{$pid}="funny2";
test_2() if (($pid=fork()) == 0); $work{$pid}="simple3";
test_3() if (($pid=fork()) == 0); $work{$pid}="funny3";
test_2() if (($pid=fork()) == 0); $work{$pid}="simple4";
test_3() if (($pid=fork()) == 0); $work{$pid}="funny4";
alter_test() if (($pid=fork()) == 0); $work{$pid}="alter";

$errors=0;
while (($pid=wait()) != -1)
{
  $ret=$?/256;
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  $errors++ if ($ret != 0);
}

if (!$opt_skip_delete && !$errors)
{
  $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  foreach $table (@testtables) {
      $dbh->Query("drop table $table");
  }
}
print ($errors ? "Test failed\n" :"Test ok\n");

$end_time=new Benchmark;
print "Total time: " .
  timestr(timediff($end_time, $start_time),"noc") . "\n";

exit(0);

#
# Insert records in the ?? tables the Nasdaq way
# 

sub test_1
{
    my ($dbh,$table,$tmpvar,$rows,$found,$i);

    $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
    $tmpvar=1;
    $rows=$found=0;
    for ($i=0 ; $i < $opt_loop_count; $i++)
    {
	$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
	# Nasdaq step 1:
	$sth=$dbh->Query("select id,last from $dtable where id='$tmpvar'")
	    or die "Select directory row: $Mysql::db_errstr\n";
	# Nasdaq step 2:
	my ($did,$dlast) = $sth->FetchRow
	    or die "Fetch directory row: $Mysql::db_errstr\n";
	$dlast++;
	$sth=$dbh->Query("INSERT into $testtables[$did]".
			 " VALUES($dlast,'This is entry $dlast',NULL)")
	    || die "Got error on insert table $testtable[$did]:". 
		" $Mysql::db_errstr\n";
	# Nasdaq step 3 - where my application hangs
	$sth=$dbh->Query("update $dtable set last='$dlast' where id='$tmpvar'")
	    or die "Updating directory for table $testtable[$did]:".
		" Mysql::db_errstr\n";
	$rows++;
    }
    $dbh=0;
    print "Test_1: Inserted $rows rows\n";
    exit(0);
}

#
# Nasdaq simple select
#

sub test_2
{
    my ($dbh,$id,$tmpvar,$rows,$found,$i);
 
    $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
    $rows=$found=0;
    $tmpvar=1;
    for ($i=0 ; $i < $opt_loop_count ; $i++)
    {
	$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
	$sth=$dbh->Query("select a.id,a.info from $testtables[$tmpvar] as a,".
			 "$dtable as d".
			 " where a.id=d.last and $i >= 0")
	    || die "Got error select max: $Mysql::db_errstr\n";
	if ((@row = $sth->FetchRow()) && defined($row[0]))
	{
	    $found++;
	}
    }
    $dbh=0;
    print "Test_2: Found $found rows\n";
    exit(0);
}


#
# Nasdaq not-so-simple select
#

sub test_3
{
    my ($dbh,$id,$tmpvar,$rows,$i);
    $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
    $rows=0;
    $tmpvar ||= $numtables;
    for ($i=0 ; $i < $opt_loop_count ; $i++)
    {
	$tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
	$id1 = ($tmpvar+1) % $numtables;
	$id2 = ($id1+1) % $numtables;
	$id3 = ($id2+1) % $numtables;
	$sth = $dbh->Query("SELECT greatest(a.id, b.id, c.id), a.info".
			   " FROM $testtables[$id1] as a,".
			   " $testtables[$id2] as b,".
			   " $testtables[$id3] as c,".
			   " $dtable as d1, $dtable as d2, $dtable as d3".
			   " WHERE ".
			   " d1.last=a.id AND d2.last=b.id AND d3.last=c.id".
			   " AND d1.id='$id1' AND d2.id='$id2'".
			   " AND d3.id='$id3'")
	    or die "Funny select: $Mysql::db_errstr\n";
	$rows+=$sth->numrows;
    }
    $dbh=0;
    print "Test_3: Found $rows rows\n";
    exit(0);
}

#
# Do an ALTER TABLE every 20 seconds
#

sub alter_test
{
    my ($dbh,$count,$old_row_count,$row_count,$id,@row,$sth);

    $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
    $id=$count=$row_count=0; $old_row_count= -1;

    # Execute the test as long as we get more data into the table
    while ($row_count != $old_row_count)
    {
      sleep(10);
      $sth=$dbh->Query("ALTER TABLE $testtables[$id] modify info varchar(32)") or die "Couldn't execute ALTER TABLE\n";
      $sth=0;
      $id=($id+1) % $numtables;

      # Test if insert test has ended
      $sth=$dbh->query("select count(*) from $testtables[0]") or die "Couldn't execute count(*)\n";
      @row = $sth->FetchRow();
      $old_row_count= $row_count;
      $row_count=$row[0];
      $count++;
    }
    $dbh=0;
    print "alter: Executed $count ALTER TABLE commands\n";
    exit(0);
}