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

  1. #!/my/gnu/bin/perl -w
  2. #
  3. # This is a test with uses 3 processes to insert, delete and select
  4. #
  5.  
  6. $opt_loop_count=100000; # 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 3 multiple connections to a server with 1 insert, 1 delete\n";
  25. print "and 1 select connections.\n";
  26.  
  27. $firsttable  = "bench_f1";
  28.  
  29. ####  
  30. ####  Start timeing and start test
  31. ####
  32.  
  33. $start_time=new Benchmark;
  34. if (!$opt_skip_create)
  35. {
  36.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  37.               $opt_user, $opt_password,
  38.             { PrintError => 0}) || die $DBI::errstr;
  39.   $Mysql::QUIET = 1;
  40.   $dbh->do("drop table $firsttable");
  41.   $Mysql::QUIET = 0;
  42.  
  43.   print "Creating table $firsttable in database $opt_db\n";
  44.   $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  45.   $dbh->disconnect; $dbh=0;    # Close handler
  46. }
  47. $|= 1;                # Autoflush
  48.  
  49. ####
  50. #### Start the tests
  51. ####
  52.  
  53. test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
  54. test_delete() if (($pid=fork()) == 0); $work{$pid}="delete";
  55. test_select() if (($pid=fork()) == 0); $work{$pid}="select1";
  56.  
  57. $errors=0;
  58. while (($pid=wait()) != -1)
  59. {
  60.   $ret=$?/256;
  61.   print "thread '" . $work{$pid} . "' finnished with exit code $ret\n";
  62.   $errors++ if ($ret != 0);
  63. }
  64.  
  65. if (!$opt_skip_delete && !$errors)
  66. {
  67.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  68.               $opt_user, $opt_password,
  69.             { PrintError => 0}) || die $DBI::errstr;
  70.   $dbh->do("drop table $firsttable");
  71.   $dbh->disconnect; $dbh=0;    # Close handler
  72. }
  73. print ($errors ? "Test failed\n" :"Test ok\n");
  74.  
  75. $end_time=new Benchmark;
  76. print "Total time: " .
  77.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  78.  
  79. exit(0);
  80.  
  81. #
  82. # Insert records in the table
  83.  
  84. sub test_insert
  85. {
  86.   my ($dbh,$i,$sth);
  87.  
  88.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  89.               $opt_user, $opt_password,
  90.             { PrintError => 0}) || die $DBI::errstr;
  91.   for ($i=0 ; $i < $opt_loop_count; $i++)
  92.   {
  93.     $sth=$dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $Mysql::db_errstr\n";
  94.     $sth=0;
  95.   }
  96.   $dbh->disconnect; $dbh=0;
  97.   print "Test_insert: Inserted $i rows\n";
  98.   exit(0);
  99. }
  100.  
  101. sub test_delete
  102. {
  103.   my ($dbh,$i,$sth,@row);
  104.  
  105.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  106.               $opt_user, $opt_password,
  107.             { PrintError => 0}) || die $DBI::errstr;
  108.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  109.   {
  110.     sleep(5);
  111.     if ($opt_lock_tables)
  112.     {
  113.       $sth=$dbh->do("lock tables $firsttable WRITE") || die "Got error on lock tables $firsttable: $Mysql::db_errstr\n";
  114.     }
  115.     $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on select from $firsttable: $dbh->errstr\n";
  116.     $sth->execute || die $dbh->errstr;
  117.     if ((@row = $sth->fetchrow_array()))
  118.     {
  119.       last if (!$row[0]);    # Insert thread is probably ready
  120.     }
  121.     $sth=$dbh->do("delete from $firsttable") || die "Got error on delete from $firsttable: $dbh->errstr;\n";
  122.   }
  123.   $sth=0;
  124.   $dbh->disconnect; $dbh=0;
  125.   print "Test_delete: Deleted all rows $i times\n";
  126.   exit(0);
  127. }
  128.  
  129.  
  130. #
  131. # select records
  132. #
  133.  
  134. sub test_select
  135. {
  136.   my ($dbh,$i,$sth,@row);
  137.  
  138.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  139.               $opt_user, $opt_password,
  140.             { PrintError => 0}) || die $DBI::errstr;
  141.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  142.   {
  143.     $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on select from $firsttable: $dbh->errstr;\n";
  144.     $sth->execute || die $dbh->errstr;
  145.     @row = $sth->fetchrow_array();
  146.     $sth=0;
  147.   }
  148.   $dbh->disconnect; $dbh=0;
  149.   print "Test_select: ok\n";
  150.   exit(0);
  151. }
  152.