You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0 KiB

26 years ago
26 years ago
  1. #!/usr/bin/perl
  2. # Copyright (C) 2000 MySQL AB
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. # This is a test with uses two processes to a database.
  17. # The other inserts records in two tables, the other does a lot of joins
  18. # on these.
  19. # Every time the read thread outputs info, it does a ALTER TABLE command
  20. # which should stop the insert thread until the ALTER TABLE command is ready.
  21. #
  22. # Warning, the output from this test will differ in 'found' from time to time,
  23. # but there should never be any errors
  24. #
  25. $host = shift || "";
  26. $test_db="test";
  27. $test_count=10000;
  28. srand 0; # Repeatable test
  29. use Mysql;
  30. $|= 1; # Autoflush
  31. $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  32. $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  33. $firsttable = "test_lock_1";
  34. $secondtable = "test_lock_2";
  35. $dbh->Query("drop table $firsttable");
  36. $dbh->Query("drop table $secondtable");
  37. print "Creating tables $firsttable and $secondtable in database $test_db\n";
  38. $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;
  39. $dbh->Query("create table $secondtable (id int(6) not null, info varchar(32), key(id))") or die $Mysql::db_errstr;
  40. $dbh=0; # Close handler
  41. if (fork() == 0)
  42. { # Insert process
  43. $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  44. $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  45. $first_id=1; $second_id=1;
  46. $first_count=$second_count=0;
  47. print "Writing started\n";
  48. for ($i=1 ; $i <= $test_count ; $i++)
  49. {
  50. if (rand(3) <= 1)
  51. {
  52. $sth=$dbh->Query("insert into $firsttable values ($first_id,'This is entry $i',NULL)") || die "Got error on insert: $Mysql::db_errstr\n";
  53. die "Row not inserted, aborting\n" if ($sth->affected_rows != 1);
  54. $first_id++;
  55. $first_count++;
  56. }
  57. else
  58. {
  59. $sth=$dbh->Query("insert into $secondtable values ($second_id,'This is entry $i')") || die "Got error on insert: $Mysql::db_errstr\n";
  60. die "Row not inserted, aborting\n" if ($sth->affected_rows != 1);
  61. $second_id++ if (rand(10) <= 1); # Don't always count it up
  62. $second_count++;
  63. }
  64. print "Write: $i\n" if ($i % 1000 == 0);
  65. }
  66. print "Writing done ($first_count $second_count)\n";
  67. }
  68. else
  69. {
  70. $dbh = Mysql->Connect($host) || die "Can't connect: $Mysql::db_errstr\n";
  71. $dbh->SelectDB($test_db) || die "Can't use database $test_db: $Mysql::db_errstr\n";
  72. $locked=$found=0;
  73. print "Reading started\n";
  74. for ($i=1 ; $i <= $test_count ; $i++)
  75. {
  76. $id=int(rand($test_count)/3)+1;
  77. $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";
  78. $found++ if ($sth->numrows);
  79. if ($i % 1000 == 0)
  80. {
  81. print "Read: $i Found: $found\n";
  82. if ($found)
  83. {
  84. $locked=1-$locked;
  85. if ($locked)
  86. {
  87. $sth=$dbh->Query("lock tables $firsttable write,$secondtable write");
  88. }
  89. $sth=$dbh->Query("alter table $firsttable CHANGE id id int(6) not null") || die "Got error on ALTER TABLE: $Mysql::db_errstr\n";
  90. $sth=$dbh->Query("alter table $secondtable CHANGE info info char(32) not null") || die "Got error on ALTER TABLE: $Mysql::db_errstr\n";
  91. if ($locked)
  92. {
  93. $sth=$dbh->Query("unlock tables");
  94. }
  95. }
  96. }
  97. }
  98. print "Reading done Found: $found\n";
  99. }