home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win.exe / DATA1.CAB / Examples / examples / tests / deadlock-script.pl < prev    next >
Perl Script  |  2000-11-22  |  6KB  |  210 lines

  1. #!/my/gnu/bin/perl
  2.  
  3. # This is a test with uses 5 processes to insert, update and select from
  4. # two tables.
  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.
  9.  
  10. $opt_loop_count=10000; # Change this to make test harder/easier
  11.  
  12. ##################### Standard benchmark inits ##############################
  13.  
  14. use Mysql;
  15. use Getopt::Long;
  16. use Benchmark;
  17.  
  18. package main;
  19.  
  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";
  23.  
  24. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  25. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  26. $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
  27.  
  28. print "Testing 9 multiple connections to a server with 1 insert/update\n";
  29. print "and 8 select connections.\n";
  30.  
  31.  
  32. @testtables = qw(bench_1 bench_2 bench_3 bench_4 bench_5);
  33. $numtables = $#testtables;    # make emacs happier
  34. $dtable = "directory";
  35. ####  
  36. ####  Start timeing and start test
  37. ####
  38.  
  39. $start_time=new Benchmark;
  40. if (!$opt_skip_create)
  41. {
  42.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  43.   $Mysql::QUIET = 1;
  44.   foreach $table (@testtables) {
  45.       $dbh->Query("drop table $table");
  46.   }
  47.   $dbh->Query("drop table $dtable");
  48.   $Mysql::QUIET = 0;
  49.  
  50.   foreach $table (@testtables) {
  51.       print "Creating table $table in database $opt_db\n";
  52.       $dbh->Query("create table $table".
  53.           " (id int(6) not null,".
  54.           " info varchar(32),".
  55.           " marker timestamp,".
  56.           " primary key(id))")
  57.       or die $Mysql::db_errstr;
  58.   }
  59.   print "Creating directory table $dtable in $opt_db\n";
  60.   $dbh->Query("create table $dtable (id int(6), last int(6))")
  61.       or die $Mysql::db_errstr;
  62.   # Populate directory table
  63.   for $i ( 0 .. $numtables ) {
  64.       $dbh->Query("insert into $dtable values($i, 0)");
  65.   }
  66.   $dbh=0;            # Close handler
  67. }
  68. $|= 1;                # Autoflush
  69.  
  70. ####
  71. #### Start the tests
  72. ####
  73.  
  74. #$test_index = 0;
  75.  
  76. test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
  77. test_2() if (($pid=fork()) == 0); $work{$pid}="simple1";
  78. test_3() if (($pid=fork()) == 0); $work{$pid}="funny1";
  79. test_2() if (($pid=fork()) == 0); $work{$pid}="simple2";
  80. test_3() if (($pid=fork()) == 0); $work{$pid}="funny2";
  81. test_2() if (($pid=fork()) == 0); $work{$pid}="simple3";
  82. test_3() if (($pid=fork()) == 0); $work{$pid}="funny3";
  83. test_2() if (($pid=fork()) == 0); $work{$pid}="simple4";
  84. test_3() if (($pid=fork()) == 0); $work{$pid}="funny4";
  85.  
  86. $errors=0;
  87. while (($pid=wait()) != -1)
  88. {
  89.   $ret=$?/256;
  90.   print "thread '" . $work{$pid} . "' finnished with exit code $ret\n";
  91.   $errors++ if ($ret != 0);
  92. }
  93.  
  94. if (!$opt_skip_delete)
  95. {
  96.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  97.   foreach $table (@testtables) {
  98.       $dbh->Query("drop table $table");
  99.   }
  100. }
  101. print ($errors ? "Test failed\n" :"Test ok\n");
  102.  
  103. $end_time=new Benchmark;
  104. print "Total time: " .
  105.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  106.  
  107. exit(0);
  108.  
  109. #
  110. # Insert records in the ?? tables the Nasdaq way
  111.  
  112. sub test_1
  113. {
  114.     my ($dbh,$table,$tmpvar,$rows,$found,$i);
  115.  
  116.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  117.     $tmpvar=1;
  118.     $rows=$found=0;
  119.     for ($i=0 ; $i < $opt_loop_count; $i++)
  120.     {
  121.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  122.     # Nasdaq step 1:
  123.     $sth=$dbh->Query("select id,last from $dtable where id='$tmpvar'")
  124.         or die "Select directory row: $Mysql::db_errstr\n";
  125.     # Nasdaq step 2:
  126.     my ($did,$dlast) = $sth->FetchRow
  127.         or die "Fetch directory row: $Mysql::db_errstr\n";
  128.     $dlast++;
  129.     $sth=$dbh->Query("INSERT into $testtables[$did]".
  130.              " VALUES($dlast,'This is entry $dlast',NULL)")
  131.         || die "Got error on insert table $testtable[$did]:". 
  132.         " $Mysql::db_errstr\n";
  133.     # Nasdaq step 3 - where my application hangs
  134.     $sth=$dbh->Query("update $dtable set last='$dlast' where id='$tmpvar'")
  135.         or die "Updating directory for table $testtable[$did]:".
  136.         " Mysql::db_errstr\n";
  137.     $rows++;
  138.     }
  139.     $dbh=0;
  140.     print "Test_1: Inserted $rows rows\n";
  141.     exit(0);
  142. }
  143.  
  144. #
  145. # Nasdaq simple select
  146. #
  147.  
  148. sub test_2
  149. {
  150.     my ($dbh,$id,$tmpvar,$rows,$found,$i);
  151.  
  152.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  153.     $rows=$found=0;
  154.     $tmpvar=1;
  155.     for ($i=0 ; $i < $opt_loop_count ; $i++)
  156.     {
  157.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  158.     $sth=$dbh->Query("select a.id,a.info from $testtables[$tmpvar] as a,".
  159.              "$dtable as d".
  160.              " where a.id=d.last")
  161.         || die "Got error select max: $Mysql::db_errstr\n";
  162.     if ((@row = $sth->FetchRow()) && defined($row[0]))
  163.     {
  164.         $found++;
  165.     }
  166.     }
  167.     $dbh=0;
  168.     print "Test_2: Found $found rows\n";
  169.     exit(0);
  170. }
  171.  
  172.  
  173. #
  174. # Nasdaq not-so-simple select
  175. #
  176.  
  177. sub test_3
  178. {
  179.     my ($dbh,$id,$tmpvar,$rows,$i);
  180.     $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  181.     $rows=0;
  182.     $tmpvar ||= $numtables;
  183.     for ($i=0 ; $i < $opt_loop_count ; $i++)
  184.     {
  185.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % $numtables);
  186.     $id1 = ($tmpvar+1) % $numtables;
  187.     $id2 = ($id1+1) % $numtables;
  188.     $id3 = ($id2+1) % $numtables;
  189.     $sth = $dbh->Query("SELECT greatest(a.id, b.id, c.id), a.info".
  190.                " FROM $testtables[$id1] as a,".
  191.                " $testtables[$id2] as b,".
  192.                " $testtables[$id3] as c,".
  193.                " $dtable as d1, $dtable as d2, $dtable as d3".
  194.                " WHERE ".
  195.                " d1.last=a.id AND d2.last=b.id AND d3.last=c.id".
  196.                " AND d1.id='$id1' AND d2.id='$id2'".
  197.                " AND d3.id='$id3'")
  198.         or die "Funny select: $Mysql::db_errstr\n";
  199.     $rows+=$sth->numrows;
  200.     }
  201.     $dbh=0;
  202.     print "Test_3: Found $rows rows\n";
  203.     exit(0);
  204. }
  205.  
  206.  
  207.  
  208.  
  209.