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.

220 lines
6.1 KiB

  1. #!/usr/bin/perl -w
  2. #
  3. # This is a test with uses processes to insert, select and drop tables.
  4. #
  5. $opt_loop_count=100000; # Change this to make test harder/easier
  6. ##################### Standard benchmark inits ##############################
  7. use DBI;
  8. use Getopt::Long;
  9. use Benchmark;
  10. package main;
  11. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  12. $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  13. $opt_host=""; $opt_db="test";
  14. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  15. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  16. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef; # Ignore warnings from these
  17. print "Testing 5 multiple connections to a server with 1 insert, 2 drop/rename\n";
  18. print "1 select and 1 flush thread\n";
  19. $firsttable = "bench_f1";
  20. ####
  21. #### Start timeing and start test
  22. ####
  23. $start_time=new Benchmark;
  24. if (!$opt_skip_create)
  25. {
  26. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  27. $opt_user, $opt_password,
  28. { PrintError => 0}) || die $DBI::errstr;
  29. $dbh->do("drop table if exists $firsttable, ${firsttable}_1, ${firsttable}_2");
  30. print "Creating table $firsttable in database $opt_db\n";
  31. $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  32. $dbh->disconnect; $dbh=0; # Close handler
  33. }
  34. $|= 1; # Autoflush
  35. ####
  36. #### Start the tests
  37. ####
  38. test_insert() if (($pid=fork()) == 0); $work{$pid}="insert";
  39. test_drop(1) if (($pid=fork()) == 0); $work{$pid}="drop 1";
  40. test_drop(2) if (($pid=fork()) == 0); $work{$pid}="drop 2";
  41. test_select() if (($pid=fork()) == 0); $work{$pid}="select";
  42. test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
  43. $errors=0;
  44. while (($pid=wait()) != -1)
  45. {
  46. $ret=$?/256;
  47. print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  48. $errors++ if ($ret != 0);
  49. }
  50. if (!$opt_skip_delete && !$errors)
  51. {
  52. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  53. $opt_user, $opt_password,
  54. { PrintError => 0}) || die $DBI::errstr;
  55. $dbh->do("drop table $firsttable");
  56. $dbh->disconnect; $dbh=0; # Close handler
  57. }
  58. print ($errors ? "Test failed\n" :"Test ok\n");
  59. $end_time=new Benchmark;
  60. print "Total time: " .
  61. timestr(timediff($end_time, $start_time),"noc") . "\n";
  62. exit(0);
  63. #
  64. # Insert records in the table
  65. #
  66. sub test_insert
  67. {
  68. my ($dbh,$i);
  69. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  70. $opt_user, $opt_password,
  71. { PrintError => 0}) || die $DBI::errstr;
  72. for ($i=0 ; $i < $opt_loop_count; $i++)
  73. {
  74. if (!$dbh->do("insert into $firsttable values ($i,'This is entry $i','')"))
  75. {
  76. print "Warning; Got error on insert: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  77. }
  78. }
  79. $dbh->disconnect; $dbh=0;
  80. print "Test_insert: Inserted $i rows\n";
  81. exit(0);
  82. }
  83. sub test_drop
  84. {
  85. my ($id) = @_;
  86. my ($dbh,$i,$sth,$error_counter,$sleep_time);
  87. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  88. $opt_user, $opt_password,
  89. { PrintError => 0}) || die $DBI::errstr;
  90. $error_counter=0;
  91. $sleep_time=2;
  92. for ($i=0 ; $i < $opt_loop_count ; $i++)
  93. {
  94. sleep($sleep_time);
  95. # Check if insert thread is ready
  96. $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on select from $firsttable: $dbh->errstr\n";
  97. if (!$sth->execute || !(@row = $sth->fetchrow_array()) ||
  98. !$row[0])
  99. {
  100. $sth->finish;
  101. $sleep_time=1;
  102. last if ($error_counter++ == 5);
  103. next;
  104. }
  105. $sleep_time=2;
  106. $sth->finish;
  107. # Change to use a new table
  108. $dbh->do("create table ${firsttable}_$id (id int(6) not null, info varchar(32), marker char(1), primary key(id))") || die $DBI::errstr;
  109. $dbh->do("drop table if exists $firsttable") || die "Got error on drop table: $dbh->errstr\n";
  110. if (!$dbh->do("alter table ${firsttable}_$id rename to $firsttable"))
  111. {
  112. print "Warning; Got error from alter table: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /already exist/));
  113. $dbh->do("drop table if exists ${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
  114. }
  115. }
  116. $dbh->do("drop table if exists $firsttable,${firsttable}_$id") || die "Got error on drop table: $dbh->errstr\n";
  117. $dbh->disconnect; $dbh=0;
  118. print "Test_drop: Did a drop $i times\n";
  119. exit(0);
  120. }
  121. #
  122. # select records
  123. #
  124. sub test_select
  125. {
  126. my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
  127. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  128. $opt_user, $opt_password,
  129. { PrintError => 0}) || die $DBI::errstr;
  130. $error_counter=0;
  131. $sleep_time=3;
  132. for ($i=0 ; $i < $opt_loop_count ; $i++)
  133. {
  134. sleep($sleep_time);
  135. $sth=$dbh->prepare("select sum(t.id) from $firsttable as t,$firsttable as t2") || die "Got error on select: $dbh->errstr;\n";
  136. if ($sth->execute)
  137. {
  138. @row = $sth->fetchrow_array();
  139. $sth->finish;
  140. $sleep_time=3;
  141. }
  142. else
  143. {
  144. print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  145. $sth->finish;
  146. last if ($error_counter++ == 5);
  147. $sleep_time=1;
  148. }
  149. }
  150. $dbh->disconnect; $dbh=0;
  151. print "Test_select: ok\n";
  152. exit(0);
  153. }
  154. #
  155. # flush records
  156. #
  157. sub test_flush
  158. {
  159. my ($dbh,$i,$sth,@row,$error_counter,$sleep_time);
  160. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  161. $opt_user, $opt_password,
  162. { PrintError => 0}) || die $DBI::errstr;
  163. $error_counter=0;
  164. $sleep_time=5;
  165. for ($i=0 ; $i < $opt_loop_count ; $i++)
  166. {
  167. sleep($sleep_time);
  168. $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepar: $dbh->errstr;\n";
  169. if ($sth->execute)
  170. {
  171. @row = $sth->fetchrow_array();
  172. $sth->finish;
  173. $sleep_time=5;
  174. $dbh->do("flush tables $firsttable") || die "Got error on flush table: " . $dbh->errstr . "\n";
  175. }
  176. else
  177. {
  178. print "Warning; Got error from select: " . $dbh->errstr . "\n" if (! ($dbh->errstr =~ /doesn't exist/));
  179. $sth->finish;
  180. last if ($error_counter++ == 5);
  181. $sleep_time=1;
  182. }
  183. }
  184. $dbh->disconnect; $dbh=0;
  185. print "Test_select: ok\n";
  186. exit(0);
  187. }