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 / lock_test.pl < prev    next >
Perl Script  |  2000-11-22  |  3KB  |  95 lines

  1. #!/my/gnu/bin/perl
  2.  
  3. # This is a test with uses two processes to a database.
  4. # The other inserts records in two tables, the other does a lot of joins
  5. # on these.
  6. # Every time the read thread outputs info, it does a ALTER TABLE command
  7. # which should stop the insert thread until the ALTER TABLE command is ready.
  8. #
  9. # Warning, the output from this test will differ in 'found' from time to time,
  10. # but there should never be any errors
  11. #
  12.  
  13. $host = shift || "";
  14. $test_db="test";
  15. $test_count=10000;
  16. srand 0;            # Repeatable test
  17.  
  18. use Mysql;
  19. $|= 1;                # Autoflush
  20.  
  21. $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  22. $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  23.  
  24. $firsttable  = "test_lock_1";
  25. $secondtable = "test_lock_2";
  26. $dbh->Query("drop table $firsttable");
  27. $dbh->Query("drop table $secondtable");
  28.  
  29. print "Creating tables $firsttable and $secondtable in database $test_db\n";
  30. $dbh->Query("create table $firsttable (id int(6) not null, info char(32), auto int(11) not null auto_increment, primary key(id),key(auto))") or die $Mysql::db_errstr;
  31.  
  32. $dbh->Query("create table $secondtable (id int(6) not null, info varchar(32), key(id))") or die $Mysql::db_errstr;
  33.  
  34. $dbh=0;                # Close handler
  35.  
  36. if (fork() == 0)
  37. {                # Insert process
  38.   $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  39.   $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  40.   $first_id=1; $second_id=1;
  41.   $first_count=$second_count=0;
  42.   print "Writing started\n";
  43.   for ($i=1 ; $i <= $test_count ; $i++)
  44.   {
  45.     if (rand(3) <= 1)
  46.     {
  47.       $sth=$dbh->Query("insert into $firsttable values ($first_id,'This is entry $i',NULL)") || die "Got error on insert: $Mysql::db_errstr\n";
  48.       die "Row not inserted, aborting\n" if ($sth->affected_rows != 1);
  49.       $first_id++;
  50.       $first_count++;
  51.     }
  52.     else
  53.     {
  54.       $sth=$dbh->Query("insert into $secondtable values ($second_id,'This is entry $i')") || die "Got error on insert: $Mysql::db_errstr\n";
  55.       die "Row not inserted, aborting\n" if ($sth->affected_rows != 1);
  56.       $second_id++ if (rand(10) <= 1); # Don't always count it up
  57.       $second_count++;
  58.     }
  59.     print "Write: $i\n" if ($i % 1000 == 0);
  60.   }
  61.   print "Writing done ($first_count $second_count)\n";
  62. }
  63. else
  64. {
  65.   $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  66.   $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  67.   $locked=$found=0;
  68.   print "Reading started\n";
  69.   for ($i=1 ; $i <= $test_count ; $i++)
  70.   {
  71.     $id=int(rand($test_count)/3)+1;
  72.     $sth=$dbh->Query("select count(*) from $firsttable,$secondtable where $firsttable.id = $secondtable.id and $firsttable.id=$id") || die "Got error on select: $Mysql::db_errstr\n";
  73.     $found++ if ($sth->numrows);
  74.     if ($i % 1000 == 0)
  75.     {
  76.       print "Read:  $i  Found: $found\n";
  77.       if ($found)
  78.       {
  79.     $locked=1-$locked;
  80.     if ($locked)
  81.     {
  82.       $sth=$dbh->Query("lock tables $firsttable write,$secondtable write");
  83.     }
  84.     $sth=$dbh->Query("alter table $firsttable CHANGE id id int(6) not null") || die "Got error on ALTER TABLE: $Mysql::db_errstr\n";
  85.     $sth=$dbh->Query("alter table $secondtable CHANGE info info char(32) not null") || die "Got error on ALTER TABLE: $Mysql::db_errstr\n";
  86.     if ($locked)
  87.     {
  88.       $sth=$dbh->Query("unlock tables");
  89.     }
  90.       }
  91.     }
  92.   }
  93.   print "Reading done  Found: $found\n";
  94. }
  95.