home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / sharewar / mysql / data1.cab / Development / examples / tests / test_delayed_insert.pl < prev    next >
Encoding:
Perl Script  |  2001-11-02  |  9.2 KB  |  331 lines

  1. #!/my/gnu/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_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 insert,";
  25. print "1 update, 1 delete 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.  
  63. $errors=0;
  64. while (($pid=wait()) != -1)
  65. {
  66.   $ret=$?/256;
  67.   print "thread '" . $work{$pid} . "' finnished with exit code $ret\n";
  68.   $errors++ if ($ret != 0);
  69. }
  70.  
  71. if (!$opt_skip_delete && !$errors)
  72. {
  73.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  74.   $dbh->do("drop table $firsttable");
  75.   $dbh->do("drop table $secondtable");
  76. }
  77. print ($errors ? "Test failed\n" :"Test ok\n");
  78.  
  79. $end_time=new Benchmark;
  80. print "Total time: " .
  81.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  82.  
  83. exit(0);
  84.  
  85. #
  86. # Insert records in the two tables
  87.  
  88. sub test_1
  89. {
  90.   my ($dbh,$tmpvar,$rows,$found,$i);
  91.  
  92.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  93.   $tmpvar=1;
  94.   $rows=$found=0;
  95.   for ($i=0 ; $i < $opt_loop_count; $i++)
  96.   {
  97.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  98.     $dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
  99.     $row_count=($i % 7)+1;
  100.     $rows+=1+$row_count;
  101.     for ($j=0 ; $j < $row_count; $j++)
  102.     {
  103.       $dbh->do("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  104.     }
  105.   }
  106.   $dbh->disconnect;
  107.   print "Test_1: Inserted $rows rows\n";
  108.   exit(0);
  109. }
  110.  
  111.  
  112. sub test_delayed_1
  113. {
  114.   my ($dbh,$tmpvar,$rows,$found,$i,$id);
  115.  
  116.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  117.   $tmpvar=1;
  118.   $rows=$found=0;
  119.   for ($i=0 ; $i < $opt_loop_count; $i++)
  120.   {
  121.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  122.     $id=$i+$opt_loop_count;
  123.     $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  124.     $row_count=($i % 7)+1;
  125.     $rows+=1+$row_count;
  126.     for ($j=0 ; $j < $row_count; $j++)
  127.     {
  128.       $dbh->do("insert into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  129.     }
  130.     if (($tmpvar % 100) == 0)
  131.     {
  132.       $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  133.       $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
  134.       $found+=2;
  135.     }
  136.   }
  137.   $dbh->disconnect;
  138.   print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  139.   exit(0);
  140. }
  141.  
  142.  
  143. sub test_delayed_2
  144. {
  145.   my ($dbh,$tmpvar,$rows,$found,$i,$id);
  146.  
  147.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  148.   $tmpvar=1;
  149.   $rows=$found=0;
  150.   for ($i=0 ; $i < $opt_loop_count; $i++)
  151.   {
  152.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  153.     $id=$i+$opt_loop_count*2;
  154.     $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  155.     $row_count=($i % 7)+1;
  156.     $rows+=1+$row_count;
  157.     for ($j=0 ; $j < $row_count; $j++)
  158.     {
  159.       $dbh->do("insert delayed into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  160.     }
  161.     if (($tmpvar % 100) == 0)
  162.     {
  163.       $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  164.       $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
  165.       $found+=2;
  166.     }
  167.   }
  168.   $dbh->disconnect;
  169.   print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  170.   exit(0);
  171. }
  172.  
  173. #
  174. # Update records in both tables
  175. #
  176.  
  177. sub test_2
  178. {
  179.   my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp,$sth,$count);
  180.  
  181.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  182.   $tmpvar=111111;
  183.   $rows=$found=$max_id=$id=0;
  184.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  185.   {
  186.     $tmp=(($tmpvar + 63) + $i)*3;
  187.     $tmp=$tmp-int($tmp/100000)*100000; 
  188.     $tmpvar^= $tmp;
  189.     $tmp=$tmpvar - int($tmpvar/10)*10;
  190.     if ($max_id*$tmp == 0)
  191.     {
  192.       $max_id=0;
  193.       $sth=$dbh->prepare("select max(id) from $firsttable where marker=''");
  194.       $sth->execute() || die "Got error select max: $DBI::errstr\n";
  195.       if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  196.       {
  197.     $found++;
  198.     $max_id=$id=$row[0];
  199.       }
  200.     }
  201.     else
  202.     {
  203.       $id= $tmpvar % ($max_id-1)+1;
  204.     }
  205.     if ($id)
  206.     {
  207.       ($count=$dbh->do("update $firsttable set marker='x' where id=$id")) || die "Got error update $firsttable: $DBI::errstr\n";
  208.       $rows+=$count;
  209.       if ($count > 0)
  210.       {
  211.     $count=$dbh->do("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $DBI::errstr\n";
  212.     $rows+=$count;
  213.       }
  214.     }
  215.   }
  216.   $dbh->disconnect;
  217.   print "Test_2: Found $found rows, Updated $rows rows\n";
  218.   exit(0);
  219. }
  220.  
  221.  
  222. #
  223. # select records
  224. #
  225.  
  226. sub test_3
  227. {
  228.   my ($dbh,$id,$tmpvar,$rows,$i,$count);
  229.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  230.   $tmpvar=222222;
  231.   $rows=0;
  232.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  233.   {
  234.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  235.     $id=$tmpvar % $opt_loop_count;
  236.     $count=$dbh->do("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $DBI::errstr\n";
  237.     $rows+=$count;
  238.   }
  239.   $dbh->disconnect;
  240.   print "Test_3: Found $rows rows\n";
  241.   exit(0);
  242. }
  243.  
  244.  
  245. #
  246. # Note that this uses row=1 and in some cases won't find any matching
  247. # records
  248. #
  249.  
  250. sub test_4
  251. {
  252.   my ($dbh,$id,$tmpvar,$rows,$i,$count);
  253.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  254.   $tmpvar=333333;
  255.   $rows=0;
  256.   for ($i=0 ; $i < $opt_loop_count; $i++)
  257.   {
  258.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  259.     $id=$tmpvar % $opt_loop_count;
  260.     $count=$dbh->do("select id from $secondtable where id=$id") || die "Got error on select from $secondtable: $DBI::errstr\n";
  261.     $rows+=$count;
  262.   }
  263.   $dbh->disconnect;
  264.   print "Test_4: Found $rows rows\n";
  265.   exit(0);
  266. }
  267.  
  268.  
  269. sub test_5
  270. {
  271.   my ($dbh,$id,$tmpvar,$rows,$i,$max_id,$count,$sth);
  272.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  273.   $tmpvar=444444;
  274.   $rows=$max_id=0;
  275.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  276.   {
  277.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  278.     if ($max_id == 0 || ($tmpvar % 10 == 0))
  279.     {
  280.       $sth=$dbh->prepare("select max(id) from $firsttable");
  281.       $sth->execute() || die "Got error select max: $DBI::errstr\n";
  282.       if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  283.       {
  284.     $max_id=$id=$row[0];
  285.       }
  286.       else
  287.       {
  288.     $id=0;
  289.       }
  290.       $sth->finish;
  291.     }
  292.     else
  293.     {
  294.       $id= $tmpvar % $max_id;
  295.     }
  296.     $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";
  297.     $rows+=$count;
  298.   }
  299.   $dbh->disconnect;
  300.   print "Test_5: Found $rows rows\n";
  301.   exit(0);
  302. }
  303.  
  304.  
  305. #
  306. # Delete the smallest row
  307. #
  308.  
  309. sub test_del
  310. {
  311.   my ($dbh,$min_id,$i,$sth,$rows);
  312.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  313.   $rows=0;
  314.   for ($i=0 ; $i < $opt_loop_count/3; $i++)
  315.   {
  316.     $sth=$dbh->prepare("select min(id) from $firsttable");
  317.     $sth->execute() || die "Got error on select from $firsttable: $DBI::errstr\n";
  318.     if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  319.     {
  320.       $min_id=$row[0];
  321.     }
  322.     $sth->finish;
  323.     $dbh->do("delete from $firsttable where id = $min_id") || die "Got error on DELETE from $firsttable: $DBI::errstr\n";
  324.     $rows++;
  325.   }
  326.   $dbh->disconnect;
  327.   print "Test_del: Deleted $rows rows\n";
  328.   exit(0);
  329. }
  330.