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

  1. #!/my/gnu/bin/perl -w
  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. #
  8.  
  9. $opt_loop_count=10000; # Change this to make test harder/easier
  10.  
  11. ##################### Standard benchmark inits ##############################
  12.  
  13. use Mysql;
  14. use Getopt::Long;
  15. use Benchmark;
  16.  
  17. package main;
  18.  
  19. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  20.   $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  21. $opt_host=""; $opt_db="test";
  22.  
  23. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  24. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  25. $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
  26.  
  27. print "Testing 5 multiple connections to a server with 1 insert, 1 update\n";
  28. print "and 3 select connections.\n";
  29.  
  30.  
  31. $firsttable  = "bench_f1";
  32. $secondtable = "bench_f2";
  33.  
  34. ####  
  35. ####  Start timeing and start test
  36. ####
  37.  
  38. $start_time=new Benchmark;
  39. if (!$opt_skip_create)
  40. {
  41.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  42.   $Mysql::QUIET = 1;
  43.   $dbh->Query("drop table $firsttable");
  44.   $dbh->Query("drop table $secondtable");
  45.   $Mysql::QUIET = 0;
  46.  
  47.   print "Creating tables $firsttable and $secondtable in database $opt_db\n";
  48.   $dbh->Query("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") or die $Mysql::db_errstr;
  49.   $dbh->Query("create table $secondtable (id int(6) not null, row int(3) not null,value double, primary key(id,row))") or die $Mysql::db_errstr;
  50.  
  51.   $dbh=0;            # Close handler
  52. }
  53. $|= 1;                # Autoflush
  54.  
  55. ####
  56. #### Start the tests
  57. ####
  58.  
  59. test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
  60. test_2() if (($pid=fork()) == 0); $work{$pid}="update";
  61. test_3() if (($pid=fork()) == 0); $work{$pid}="select1";
  62. test_4() if (($pid=fork()) == 0); $work{$pid}="select2";
  63. test_5() if (($pid=fork()) == 0); $work{$pid}="select3";
  64.  
  65. $errors=0;
  66. while (($pid=wait()) != -1)
  67. {
  68.   $ret=$?/256;
  69.   print "thread '" . $work{$pid} . "' finnished with exit code $ret\n";
  70.   $errors++ if ($ret != 0);
  71. }
  72.  
  73. if (!$opt_skip_delete && !$errors)
  74. {
  75.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  76.   $dbh->Query("drop table $firsttable");
  77.   $dbh->Query("drop table $secondtable");
  78. }
  79. print ($errors ? "Test failed\n" :"Test ok\n");
  80.  
  81. $end_time=new Benchmark;
  82. print "Total time: " .
  83.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  84.  
  85. exit(0);
  86.  
  87. #
  88. # Insert records in the two tables
  89.  
  90. sub test_1
  91. {
  92.   my ($dbh,$tmpvar,$rows,$found,$i);
  93.  
  94.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_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.     $sth=$dbh->Query("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $Mysql::db_errstr\n";
  101.     $row_count=($i % 7)+1;
  102.     $rows+=1+$row_count;
  103.     for ($j=0 ; $j < $row_count; $j++)
  104.     {
  105.       $sth=$dbh->Query("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $Mysql::db_errstr\n";
  106.     }
  107.     if (($tmpvar % 10) == 0)
  108.     {
  109.       $sth=$dbh->Query("select max(info) from $firsttable") || die "Got error on select max(info): $Mysql::db_errstr\n";
  110.       $sth=$dbh->Query("select max(value) from $secondtable") || die "Got error on select max(info): $Mysql::db_errstr\n";      
  111.       $found+=2;
  112.     }
  113.   }
  114.   $dbh=0;
  115.   print "Test_1: Inserted $rows rows, found $found rows\n";
  116.   exit(0);
  117. }
  118.  
  119. #
  120. # Update records in both tables
  121. #
  122.  
  123. sub test_2
  124. {
  125.   my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp);
  126.  
  127.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  128.   $tmpvar=111111;
  129.   $rows=$found=$max_id=$id=0;
  130.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  131.   {
  132.     $tmp=(($tmpvar + 63) + $i)*3;
  133.     $tmp=$tmp-int($tmp/100000)*100000; 
  134.     $tmpvar^= $tmp;
  135.     $tmp=$tmpvar - int($tmpvar/10)*10;
  136.     if ($max_id*$tmp == 0)
  137.     {
  138.       $max_id=0;
  139.       $sth=$dbh->Query("select max(id) from $firsttable where marker=''") || die "Got error select max: $Mysql::db_errstr\n";
  140.       if ((@row = $sth->FetchRow()) && defined($row[0]))
  141.       {
  142.     $found++;
  143.     $max_id=$id=$row[0];
  144.       }
  145.     }
  146.     else
  147.     {
  148.       $id= $tmpvar % ($max_id-1)+1;
  149.     }
  150.     if ($id)
  151.     {
  152.       $sth=$dbh->Query("update $firsttable set marker='x' where id=$id") || die "Got error update $firsttable: $Mysql::db_errstr\n";
  153.       $rows+=$sth->affected_rows;
  154.       if ($sth->affected_rows)
  155.       {
  156.     $sth=$dbh->Query("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $Mysql::db_errstr\n";
  157.     $rows+=$sth->affected_rows;
  158.       }
  159.     }
  160.   }
  161.   $dbh=0;
  162.   print "Test_2: Found $found rows, Updated $rows rows\n";
  163.   exit(0);
  164. }
  165.  
  166.  
  167. #
  168. # select records
  169. #
  170.  
  171. sub test_3
  172. {
  173.   my ($dbh,$id,$tmpvar,$rows,$i);
  174.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  175.   $tmpvar=222222;
  176.   $rows=0;
  177.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  178.   {
  179.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  180.     $id=$tmpvar % $opt_loop_count;
  181.     $sth=$dbh->Query("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $Mysql::db_errstr\n";
  182.     $rows+=$sth->numrows;
  183.   }
  184.   $dbh=0;
  185.   print "Test_3: Found $rows rows\n";
  186.   exit(0);
  187. }
  188.  
  189.  
  190. #
  191. # Note that this uses row=1 and in some cases won't find any matching
  192. # records
  193. #
  194.  
  195. sub test_4
  196. {
  197.   my ($dbh,$id,$tmpvar,$rows,$i);
  198.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  199.   $tmpvar=333333;
  200.   $rows=0;
  201.   for ($i=0 ; $i < $opt_loop_count; $i++)
  202.   {
  203.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  204.     $id=$tmpvar % $opt_loop_count;
  205.     $sth=$dbh->Query("select id from $secondtable where id=$id") || die "Got error on select form $secondtable: $Mysql::db_errstr\n";
  206.     $rows+=$sth->numrows;
  207.   }
  208.   $dbh=0;
  209.   print "Test_4: Found $rows rows\n";
  210.   exit(0);
  211. }
  212.  
  213.  
  214. sub test_5
  215. {
  216.   my ($dbh,$id,$tmpvar,$rows,$i,$max_id);
  217.   $dbh = Mysql->Connect($opt_host, $opt_db) || die $Mysql::db_errstr;
  218.   $tmpvar=444444;
  219.   $rows=$max_id=0;
  220.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  221.   {
  222.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  223.     if ($max_id == 0 || ($tmpvar % 10 == 0))
  224.     {
  225.       $sth=$dbh->Query("select max(id) from $firsttable") || die "Got error select max: $Mysql::db_errstr\n";
  226.       if ((@row = $sth->FetchRow()) && defined($row[0]))
  227.       {
  228.     $max_id=$id=$row[0];
  229.       }
  230.       else
  231.       {
  232.     $id=0;
  233.       }
  234.     }
  235.     else
  236.     {
  237.       $id= $tmpvar % $max_id;
  238.     }
  239.     $sth=$dbh->Query("select value from $firsttable,$secondtable where $firsttable.id=$id and $secondtable.id=$firsttable.id") || die "Got error on select form $secondtable: $Mysql::db_errstr\n";
  240.     $rows+=$sth->numrows;
  241.   }
  242.   $dbh=0;
  243.   print "Test_5: Found $rows rows\n";
  244.   exit(0);
  245. }
  246.