~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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/usr/bin/perl -w
#
# This is a test with uses many processes to test a MySQL server.
#
# Tested a lot with:  --threads=30

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

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

use DBI;
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_threads=5;
$opt_host=$opt_user=$opt_password=""; $opt_db="test";

GetOptions("host=s","db=s","user=s","password=s","loop-count=i","skip-create","skip-in","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","threads=i") || die "Aborted";
$opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these

print "Test of multiple connections that test the following things:\n";
print "insert, select, delete, update, alter, check, repair and flush\n";

@testtables = ( ["bench_f31", ""],
		["bench_f32", "row_format=fixed"],
		["bench_f33", "delay_key_write=1"],
		["bench_f34", "checksum=1"],
		["bench_f35", "delay_key_write=1"]);
$abort_table="bench_f39";

$numtables = $#testtables+1;
srand 100;			# Make random numbers repeatable

####
####  Start timeing and start test
####

$start_time=new Benchmark;
$dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		    $opt_user, $opt_password,
		  { PrintError => 0}) || die $DBI::errstr;
if (!$opt_skip_create)
{
  my $table_def;
  foreach $table_def (@testtables)
  {
    my ($table,$extra)= ($table_def->[0], $table_def->[1]);
    print "Creating table $table in database $opt_db\n";
    $dbh->do("drop table if exists $table");
    $dbh->do("create table $table".
	     " (id int(6) not null auto_increment,".
	     " info varchar(32)," .
	     " marker timestamp," .
	     " flag int not null," .
	     " primary key(id)) $extra")

      or die $DBI::errstr;
    # One row in the table will make future tests easier
    $dbh->do("insert into $table (id) values (null)")
      or die $DBI::errstr;
  }
  # Create the table we use to signal that we should end the test
  $dbh->do("drop table if exists $abort_table");
  $dbh->do("create table $abort_table (id int(6) not null) ENGINE=heap") ||
    die $DBI::errstr;
}

$dbh->do("delete from $abort_table");
$dbh->disconnect; $dbh=0;	# Close handler
$|= 1;				# Autoflush

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

for ($i=0 ; $i < $opt_threads ; $i ++)
{
  test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
}
for ($i=0 ; $i < $numtables ; $i ++)
{
  test_insert($i,$i) if (($pid=fork()) == 0); $work{$pid}="insert_one";
}
for ($i=0 ; $i < $opt_threads ; $i ++)
{
  test_select() if (($pid=fork()) == 0); $work{$pid}="select_key";
}
test_join() if (($pid=fork()) == 0); $work{$pid}="test_join";
test_select_count() if (($pid=fork()) == 0); $work{$pid}="select_count";
test_delete() if (($pid=fork()) == 0); $work{$pid}="delete";
test_update() if (($pid=fork()) == 0); $work{$pid}="update";
test_flush() if (($pid=fork()) == 0); $work{$pid}= "flush";
test_check() if (($pid=fork()) == 0); $work{$pid}="check";
test_repair() if (($pid=fork()) == 0); $work{$pid}="repair";
test_alter() if (($pid=fork()) == 0); $work{$pid}="alter";
#test_database("test2") if (($pid=fork()) == 0); $work{$pid}="check_database";

print "Started " . ($opt_threads*2+4) . " threads\n";

$errors=0;
$running_insert_threads=$opt_threads+$numtables;
while (($pid=wait()) != -1)
{
  $ret=$?/256;
  print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  if ($work{$pid} =~ /^insert/)
  {
    if (!--$running_insert_threads)
    {
      # Time to stop other threads
      signal_abort();
    }
  }
  $errors++ if ($ret != 0);
}

#
# Cleanup
#

if (!$opt_skip_delete && !$errors)
{
  my $table_def;
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $dbh->do("drop table $abort_table");
  foreach $table_def (@testtables)
  {
    $dbh->do("drop table " . $table_def->[0]);
  }
  $dbh->disconnect; $dbh=0;	# Close handler
}

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 table
#

sub test_insert
{
  my ($from_table,$to_table)= @_;
  my ($dbh,$i,$j,$count,$table_def,$table);

  if (!defined($from_table))
  {
    $from_table=0; $to_table=$numtables-1;
  }

  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  for ($i=$count=0 ; $i < $opt_loop_count; $i++)
  {
    for ($j= $from_table ; $j <= $to_table ; $j++)
    {
      my ($table)= ($testtables[$j]->[0]);
      $dbh->do("insert into $table values (NULL,'This is entry $i','',0)") || die "Got error on insert: $DBI::errstr\n";
      $count++;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_insert: Inserted $count rows\n";
  exit(0);
}


#
# select records
# Do continously select over all tables as long as there is changed
# rows in the table
#

sub test_select
{
  my ($dbh, $i, $j, $count, $loop);

  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $count_query=make_count_query($numtables);
  $count=0;
  $loop=9999;

  $i=0;
  while (($i++ % 100) || !test_if_abort($dbh))
  {
    if ($loop++ >= 100)
    {
      $loop=0;
      $row_counts=simple_query($dbh, $count_query);
    }
    for ($j=0 ; $j < $numtables ; $j++)
    {
      my ($id)= int rand $row_counts->[$j];
      my ($table)= $testtables[$j]->[0];
      simple_query($dbh, "select id,info from $table where id=$id");
      $count++;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_select: Executed $count selects\n";
  exit(0);
}

#
# Do big select count(distinct..) over the table
# 

sub test_select_count
{
  my ($dbh, $i, $j, $count, $loop);

  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $count=0;
  $i=0;
  while (!test_if_abort($dbh))
  {
    for ($j=0 ; $j < $numtables ; $j++)
    {
      my ($table)= $testtables[$j]->[0];
      simple_query($dbh, "select count(distinct marker),count(distinct id),count(distinct info) from $table");
      $count++;
    }
    sleep(20);		# This query is quite slow
  }
  $dbh->disconnect; $dbh=0;
  print "Test_select: Executed $count select count(distinct) queries\n";
  exit(0);
}

#
# select records
# Do continously joins between the first and second table
#

sub test_join
{
  my ($dbh, $i, $j, $count, $loop);

  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $count_query=make_count_query($numtables);
  $count=0;
  $loop=9999;

  $i=0;
  while (($i++ % 100) || !test_if_abort($dbh))
  {
    if ($loop++ >= 100)
    {
      $loop=0;
      $row_counts=simple_query($dbh, $count_query);
    }
    for ($j=0 ; $j < $numtables-1 ; $j++)
    {
      my ($id)= int rand $row_counts->[$j];
      my ($t1,$t2)= ($testtables[$j]->[0],$testtables[$j+1]->[0]);
      simple_query($dbh, "select $t1.id,$t2.info from $t1, $t2 where $t1.id=$t2.id and $t1.id=$id");
      $count++;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_join: Executed $count joins\n";
  exit(0);
}

#
# Delete 1-5 rows from the first 2 tables.
# Test ends when the number of rows for table 3 didn't change during
# one loop
#

sub test_delete
{
  my ($dbh, $i,$j, $row_counts, $count_query, $table_count, $count);

  $table_count=2;
  $count=0;
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $count_query=make_count_query($table_count+1);

  sleep(5);			# Give time to insert some rows
  $i=0;
  while (($i++ % 10) || !test_if_abort($dbh))
  {
    sleep(1);
    $row_counts=simple_query($dbh, $count_query);

    for ($j=0 ; $j < $table_count ; $j++)
    {
      my ($id)= int rand $row_counts->[$j];
      my ($table)= $testtables[$j]->[0];
      $dbh->do("delete from $table where id >= $id-2 and id <= $id +2") || die "Got error on delete from $table: $DBI::errstr\n";
      $count++;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_delete: Executed $count deletes\n";
  exit(0);
}

#
# Update the flag for table 2 and 3
# Will abort after a while when table1 doesn't change max value
#

sub test_update
{
  my ($dbh, $i, $j, $row_counts, $count_query, $count, $loop);
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $count_query=make_count_query(3);
  $loop=9999;
  $count=0;

  sleep(5);			# Give time to insert some rows
  $i=0;
  while (($i++ % 100) || !test_if_abort($dbh))
  {
    if ($loop++ >= 100)
    {
      $loop=0;
      $row_counts=simple_query($dbh, $count_query);
    }

    for ($j=1 ; $j <= 2 ; $j++)
    {
      my ($id)= int rand $row_counts->[$j];
      my ($table)= $testtables[$j]->[0];
      # Fix to not change the same rows as the above delete
      $id= ($id + $count) % $row_counts->[$j];

      $dbh->do("update $table set flag=flag+1 where id >= $id-2 and id <= $id +2") || die "Got error on update of $table: $DBI::errstr\n";
      $count++;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "Test_update: Executed $count updates\n";
  exit(0);
}


#
# Run a check on all tables except the last one
# (The last one is not checked to put pressure on the key cache)
#

sub test_check
{
  my ($dbh, $row, $i, $j, $type, $table);
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $type= "check";
  for ($i=$j=0 ; !test_if_abort($dbh) ; $i++)
  {
    sleep(1000);
    $table=$testtables[$j]->[0];
    $sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $DBI::errstr\n";
    $sth->execute || die $DBI::errstr;

    while (($row=$sth->fetchrow_arrayref))
    {
      if ($row->[3] ne "OK")
      {
	print "Got error " . $row->[3] . " when doing $type on $table\n";
	exit(1);
      }
    }
    if (++$j == $numtables-1)
    {
      $j=0;
    }
  }
  $dbh->disconnect; $dbh=0;
  print "test_check: Executed $i checks\n";
  exit(0);
}

#
# Do a repair on the first table once in a while
#

sub test_repair
{
  my ($dbh, $row, $i, $type, $table);
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $type= "repair";
  for ($i=0 ; !test_if_abort($dbh) ; $i++)
  {
    sleep(2000);
    $table=$testtables[0]->[0];
    $sth=$dbh->prepare("$type table $table") || die "Got error on prepare: $DBI::errstr\n";
    $sth->execute || die $DBI::errstr;

    while (($row=$sth->fetchrow_arrayref))
    {
      if ($row->[3] ne "OK")
      {
	print "Got error " . $row->[3] . " when doing $type on $table\n";
	exit(1);
      }
    }
  }
  $dbh->disconnect; $dbh=0;
  print "test_repair: Executed $i repairs\n";
  exit(0);
}

#
# Do a flush tables on table 3 and 4 once in a while
#

sub test_flush
{
  my ($dbh,$count,$tables);

  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $tables=$testtables[2]->[0] . "," . $testtables[3]->[0];

  $count=0;
  while (!test_if_abort($dbh))
  {
    sleep(3000);
    $dbh->do("flush tables $tables") ||
      die "Got error on flush $DBI::errstr\n";
    $count++;
  }
  $dbh->disconnect; $dbh=0;
  print "flush: Executed $count flushs\n";
  exit(0);
}


#
# Test all tables in a database
#

sub test_database
{
  my ($database) = @_;
  my ($dbh, $row, $i, $type, $tables);
  $dbh = DBI->connect("DBI:mysql:$database:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $tables= join(',',$dbh->func('_ListTables'));
  $type= "check";
  for ($i=0 ; !test_if_abort($dbh) ; $i++)
  {
    sleep(120);
    $sth=$dbh->prepare("$type table $tables") || die "Got error on prepare: $DBI::errstr\n";
    $sth->execute || die $DBI::errstr;

    while (($row=$sth->fetchrow_arrayref))
    {
      if ($row->[3] ne "OK")
      {
	print "Got error " . $row->[2] . " " . $row->[3] . " when doing $type on " . $row->[0] . "\n";
	exit(1);
      }
    }
  }
  $dbh->disconnect; $dbh=0;
  print "test_check: Executed $i checks\n";
  exit(0);
}

#
# Test ALTER TABLE on the second table
#

sub test_alter
{
  my ($dbh, $row, $i, $type, $table);
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  for ($i=0 ; !test_if_abort($dbh) ; $i++)
  {
    sleep(100);
    $table=$testtables[1]->[0];
    $sth=$dbh->prepare("ALTER table $table modify info char(32)") || die "Got error on prepare: $DBI::errstr\n";
    $sth->execute || die $DBI::errstr;
  }
  $dbh->disconnect; $dbh=0;
  print "test_alter: Executed $i ALTER TABLE\n";
  exit(0);
}


#
# Help functions
#

sub signal_abort
{
  my ($dbh);
  $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
		      $opt_user, $opt_password,
		    { PrintError => 0}) || die $DBI::errstr;

  $dbh->do("insert into $abort_table values(1)") || die $DBI::errstr;
  $dbh->disconnect; $dbh=0;
  exit(0);
}


sub test_if_abort()
{
  my ($dbh)=@_;
  $row=simple_query($dbh,"select * from $opt_db.$abort_table");
  return (defined($row) && defined($row->[0]) != 0) ? 1 : 0;
}


sub make_count_query
{
  my ($table_count)= @_;
  my ($tables, $count_query, $i, $tables_def);
  $tables="";
  $count_query="select high_priority ";
  $table_count--;
  for ($i=0 ; $i < $table_count ; $i++)
  {
    my ($table_def)= $testtables[$i];
    $tables.=$table_def->[0] . ",";
    $count_query.= "max(" . $table_def->[0] . ".id),";
  }
  $table_def=$testtables[$table_count];
  $tables.=$table_def->[0];
  $count_query.= "max(" . $table_def->[0] . ".id) from $tables";
  return $count_query;
}

sub simple_query()
{
  my ($dbh, $query)= @_;
  my ($sth,$row);

  $sth=$dbh->prepare($query) || die "Got error on '$query': " . $dbh->errstr . "\n";
  $sth->execute || die "Got error on '$query': " . $dbh->errstr . "\n";
  $row= $sth->fetchrow_arrayref();
  $sth=0;
  return $row;
}