~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to tests/test_delayed_insert.pl

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
# This is a test for INSERT DELAYED
 
4
#
 
5
 
 
6
$opt_loop_count=10000; # 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","skip-delete",
 
21
"verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
 
22
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
 
23
 
 
24
print "Testing 8 multiple connections to a server with 1 insert, 2 delayed\n";
 
25
print "insert, 1 update, 1 delete, 1 flush tables and 3 select connections.\n";
 
26
 
 
27
$firsttable  = "bench_f1";
 
28
$secondtable = "bench_f2";
 
29
 
 
30
####  
 
31
####  Start timeing and start test
 
32
####
 
33
 
 
34
$start_time=new Benchmark;
 
35
if (!$opt_skip_create)
 
36
{
 
37
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
38
  $Mysql::QUIET = 1;
 
39
  $dbh->do("drop table if exists $firsttable,$secondtable");
 
40
  $Mysql::QUIET = 0;
 
41
 
 
42
  print "Creating tables $firsttable and $secondtable in database $opt_db\n";
 
43
  $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") or die $DBI::errstr;
 
44
  $dbh->do("create table $secondtable (id int(6) not null, row int(3) not null,value double, primary key(id,row))") or die $DBI::errstr;
 
45
  
 
46
  $dbh->disconnect;
 
47
}
 
48
$|= 1;                          # Autoflush
 
49
 
 
50
####
 
51
#### Start the tests
 
52
####
 
53
 
 
54
test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
 
55
test_delayed_1() if (($pid=fork()) == 0); $work{$pid}="delayed_insert1";
 
56
test_delayed_2() if (($pid=fork()) == 0); $work{$pid}="delayed_insert2";
 
57
test_2() if (($pid=fork()) == 0); $work{$pid}="update";
 
58
test_3() if (($pid=fork()) == 0); $work{$pid}="select1";
 
59
test_4() if (($pid=fork()) == 0); $work{$pid}="select2";
 
60
test_5() if (($pid=fork()) == 0); $work{$pid}="select3";
 
61
test_del() if (($pid=fork()) == 0); $work{$pid}="delete";
 
62
test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
 
63
 
 
64
$errors=0;
 
65
while (($pid=wait()) != -1)
 
66
{
 
67
  $ret=$?/256;
 
68
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
 
69
  $errors++ if ($ret != 0);
 
70
}
 
71
 
 
72
if (!$opt_skip_delete && !$errors)
 
73
{
 
74
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
75
  $dbh->do("drop table $firsttable");
 
76
  $dbh->do("drop table $secondtable");
 
77
}
 
78
print ($errors ? "Test failed\n" :"Test ok\n");
 
79
 
 
80
$end_time=new Benchmark;
 
81
print "Total time: " .
 
82
  timestr(timediff($end_time, $start_time),"noc") . "\n";
 
83
 
 
84
exit(0);
 
85
 
 
86
#
 
87
# Insert records in the two tables
 
88
 
89
 
 
90
sub test_1
 
91
{
 
92
  my ($dbh,$tmpvar,$rows,$found,$i);
 
93
 
 
94
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
95
  $tmpvar=1;
 
96
  $rows=$found=0;
 
97
  for ($i=0 ; $i < $opt_loop_count; $i++)
 
98
  {
 
99
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
100
    $dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
 
101
    $row_count=($i % 7)+1;
 
102
    $rows+=1+$row_count;
 
103
    for ($j=0 ; $j < $row_count; $j++)
 
104
    {
 
105
      $dbh->do("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $DBI::errstr\n";
 
106
    }
 
107
  }
 
108
  $dbh->disconnect;
 
109
  print "Test_1: Inserted $rows rows\n";
 
110
  exit(0);
 
111
}
 
112
 
 
113
 
 
114
sub test_delayed_1
 
115
{
 
116
  my ($dbh,$tmpvar,$rows,$found,$i,$id);
 
117
 
 
118
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
119
  $tmpvar=1;
 
120
  $rows=$found=0;
 
121
  for ($i=0 ; $i < $opt_loop_count; $i++)
 
122
  {
 
123
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
124
    $id=$i+$opt_loop_count;
 
125
    $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
 
126
    $row_count=($i % 7)+1;
 
127
    $rows+=1+$row_count;
 
128
    for ($j=0 ; $j < $row_count; $j++)
 
129
    {
 
130
      $dbh->do("insert into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
 
131
    }
 
132
    if (($tmpvar % 100) == 0)
 
133
    {
 
134
      $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
 
135
      $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
 
136
      $found+=2;
 
137
    }
 
138
  }
 
139
  $dbh->disconnect;
 
140
  print "Test_1: Inserted delayed $rows rows, found $found rows\n";
 
141
  exit(0);
 
142
}
 
143
 
 
144
 
 
145
sub test_delayed_2
 
146
{
 
147
  my ($dbh,$tmpvar,$rows,$found,$i,$id);
 
148
 
 
149
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
150
  $tmpvar=1;
 
151
  $rows=$found=0;
 
152
  for ($i=0 ; $i < $opt_loop_count; $i++)
 
153
  {
 
154
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
155
    $id=$i+$opt_loop_count*2;
 
156
    $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
 
157
    $row_count=($i % 7)+1;
 
158
    $rows+=1+$row_count;
 
159
    for ($j=0 ; $j < $row_count; $j++)
 
160
    {
 
161
      $dbh->do("insert delayed into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
 
162
    }
 
163
    if (($tmpvar % 100) == 0)
 
164
    {
 
165
      $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
 
166
      $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
 
167
      $found+=2;
 
168
    }
 
169
  }
 
170
  $dbh->disconnect;
 
171
  print "Test_1: Inserted delayed $rows rows, found $found rows\n";
 
172
  exit(0);
 
173
}
 
174
 
 
175
#
 
176
# Update records in both tables
 
177
#
 
178
 
 
179
sub test_2
 
180
{
 
181
  my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp,$sth,$count);
 
182
 
 
183
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
184
  $tmpvar=111111;
 
185
  $rows=$found=$max_id=$id=0;
 
186
  for ($i=0 ; $i < $opt_loop_count ; $i++)
 
187
  {
 
188
    $tmp=(($tmpvar + 63) + $i)*3;
 
189
    $tmp=$tmp-int($tmp/100000)*100000; 
 
190
    $tmpvar^= $tmp;
 
191
    $tmp=$tmpvar - int($tmpvar/10)*10;
 
192
    if ($max_id*$tmp == 0)
 
193
    {
 
194
      $max_id=0;
 
195
      $sth=$dbh->prepare("select max(id) from $firsttable where marker=''");
 
196
      $sth->execute() || die "Got error select max: $DBI::errstr\n";
 
197
      if ((@row = $sth->fetchrow_array()) && defined($row[0]))
 
198
      {
 
199
        $found++;
 
200
        $max_id=$id=$row[0];
 
201
      }
 
202
      $sth->finish;
 
203
    }
 
204
    else
 
205
    {
 
206
      $id= $tmpvar % ($max_id-1)+1;
 
207
    }
 
208
    if ($id)
 
209
    {
 
210
      ($count=$dbh->do("update $firsttable set marker='x' where id=$id")) || die "Got error update $firsttable: $DBI::errstr\n";
 
211
      $rows+=$count;
 
212
      if ($count > 0)
 
213
      {
 
214
        $count=$dbh->do("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $DBI::errstr\n";
 
215
        $rows+=$count;
 
216
      }
 
217
    }
 
218
  }
 
219
  $dbh->disconnect;
 
220
  print "Test_2: Found $found rows, Updated $rows rows\n";
 
221
  exit(0);
 
222
}
 
223
 
 
224
 
 
225
#
 
226
# select records
 
227
#
 
228
 
 
229
sub test_3
 
230
{
 
231
  my ($dbh,$id,$tmpvar,$rows,$i,$count);
 
232
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
233
  $tmpvar=222222;
 
234
  $rows=0;
 
235
  for ($i=0 ; $i < $opt_loop_count ; $i++)
 
236
  {
 
237
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
238
    $id=$tmpvar % $opt_loop_count;
 
239
    $count=$dbh->do("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $DBI::errstr\n";
 
240
    $rows+=$count;
 
241
  }
 
242
  $dbh->disconnect;
 
243
  print "Test_3: Found $rows rows\n";
 
244
  exit(0);
 
245
}
 
246
 
 
247
 
 
248
#
 
249
# Note that this uses row=1 and in some cases won't find any matching
 
250
# records
 
251
#
 
252
 
 
253
sub test_4
 
254
{
 
255
  my ($dbh,$id,$tmpvar,$rows,$i,$count);
 
256
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
257
  $tmpvar=333333;
 
258
  $rows=0;
 
259
  for ($i=0 ; $i < $opt_loop_count; $i++)
 
260
  {
 
261
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
262
    $id=$tmpvar % $opt_loop_count;
 
263
    $count=$dbh->do("select id from $secondtable where id=$id") || die "Got error on select from $secondtable: $DBI::errstr\n";
 
264
    $rows+=$count;
 
265
  }
 
266
  $dbh->disconnect;
 
267
  print "Test_4: Found $rows rows\n";
 
268
  exit(0);
 
269
}
 
270
 
 
271
 
 
272
sub test_5
 
273
{
 
274
  my ($dbh,$id,$tmpvar,$rows,$i,$max_id,$count,$sth);
 
275
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
276
  $tmpvar=444444;
 
277
  $rows=$max_id=0;
 
278
  for ($i=0 ; $i < $opt_loop_count ; $i++)
 
279
  {
 
280
    $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
 
281
    if ($max_id == 0 || ($tmpvar % 10 == 0))
 
282
    {
 
283
      $sth=$dbh->prepare("select max(id) from $firsttable");
 
284
      $sth->execute() || die "Got error select max: $DBI::errstr\n";
 
285
      if ((@row = $sth->fetchrow_array()) && defined($row[0]))
 
286
      {
 
287
        $max_id=$id=$row[0];
 
288
      }
 
289
      else
 
290
      {
 
291
        $id=0;
 
292
      }
 
293
      $sth->finish;
 
294
    }
 
295
    else
 
296
    {
 
297
      $id= $tmpvar % $max_id;
 
298
    }
 
299
    $count=$dbh->do("select value from $firsttable,$secondtable where $firsttable.id=$id and $secondtable.id=$firsttable.id") || die "Got error on select from $secondtable: $DBI::errstr\n";
 
300
    $rows+=$count;
 
301
  }
 
302
  $dbh->disconnect;
 
303
  print "Test_5: Found $rows rows\n";
 
304
  exit(0);
 
305
}
 
306
 
 
307
 
 
308
#
 
309
# Delete the smallest row
 
310
#
 
311
 
 
312
sub test_del
 
313
{
 
314
  my ($dbh,$min_id,$i,$sth,$rows);
 
315
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
 
316
  $rows=0;
 
317
  for ($i=0 ; $i < $opt_loop_count/3; $i++)
 
318
  {
 
319
    $sth=$dbh->prepare("select min(id) from $firsttable");
 
320
    $sth->execute() || die "Got error on select from $firsttable: $DBI::errstr\n";
 
321
    if ((@row = $sth->fetchrow_array()) && defined($row[0]))
 
322
    {
 
323
      $min_id=$row[0];
 
324
    }
 
325
    $sth->finish;
 
326
    $dbh->do("delete from $firsttable where id = $min_id") || die "Got error on DELETE from $firsttable: $DBI::errstr\n";
 
327
    $rows++;
 
328
  }
 
329
  $dbh->disconnect;
 
330
  print "Test_del: Deleted $rows rows\n";
 
331
  exit(0);
 
332
}
 
333
 
 
334
 
 
335
#
 
336
# Do a flush tables once in a while
 
337
#
 
338
 
 
339
sub test_flush
 
340
{
 
341
  my ($dbh,$sth,$found1,$last_found1,$i,@row);
 
342
  $found1=0; $last_found1=-1;
 
343
 
 
344
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
 
345
                      $opt_user, $opt_password,
 
346
                    { PrintError => 0}) || die $DBI::errstr;
 
347
 
 
348
  for ($i=0; $found1 != $last_found1 ; $i++)
 
349
  {
 
350
    $sth=$dbh->prepare("flush tables") || die "Got error on prepare: $dbh->errstr\n";
 
351
    $sth->execute || die $dbh->errstr;    
 
352
    $sth->finish;
 
353
 
 
354
    $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepare: $dbh->errstr\n";
 
355
    $sth->execute || die $dbh->errstr;
 
356
    @row = $sth->fetchrow_array();
 
357
    $last_found1=$found1;
 
358
    $found1= $row[0];
 
359
    $sth->finish;
 
360
    sleep(5);
 
361
  }
 
362
  $dbh->disconnect; $dbh=0;
 
363
  print "flush: Did $i repair/checks\n";
 
364
  exit(0);
 
365
}