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.

125 lines
3.0 KiB

  1. #!/usr/bin/perl -w
  2. #
  3. # This is a test with uses many processes to test a MySQL server.
  4. #
  5. $opt_loop_count=10000; # 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_threads=2;
  14. $opt_host=$opt_user=$opt_password=""; $opt_db="test";
  15. GetOptions("host=s","db=s","user=s","password=s","loop-count=i","skip-create","skip-in","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","threads=i") || 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 truncate from $opt_threads multiple connections $opt_loop_count times\n";
  18. @testtables = ( ["bench_f31", "type=heap"]);
  19. ####
  20. #### Start timeing and start test
  21. ####
  22. $start_time=new Benchmark;
  23. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  24. $opt_user, $opt_password,
  25. { PrintError => 0}) || die $DBI::errstr;
  26. if (!$opt_skip_create)
  27. {
  28. my $table_def;
  29. foreach $table_def (@testtables)
  30. {
  31. my ($table,$extra)= ($table_def->[0], $table_def->[1]);
  32. print "Creating table $table in database $opt_db\n";
  33. $dbh->do("drop table if exists $table");
  34. $dbh->do("create table $table".
  35. " (id int(6) not null,".
  36. " info varchar(32)," .
  37. " marker timestamp," .
  38. " flag int not null," .
  39. " primary key(id)) $extra")
  40. or die $DBI::errstr;
  41. }
  42. }
  43. $dbh->disconnect; $dbh=0; # Close handler
  44. $|= 1; # Autoflush
  45. ####
  46. #### Start the tests
  47. ####
  48. for ($i=0 ; $i < $opt_threads ; $i ++)
  49. {
  50. test_truncate() if (($pid=fork()) == 0); $work{$pid}="truncate";
  51. }
  52. print "Started $opt_threads threads\n";
  53. $errors=0;
  54. $running_insert_threads=$opt_threads;
  55. while (($pid=wait()) != -1)
  56. {
  57. $ret=$?/256;
  58. print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  59. --$running_insert_threads;
  60. $errors++ if ($ret != 0);
  61. }
  62. #
  63. # Cleanup
  64. #
  65. if (!$opt_skip_delete && !$errors)
  66. {
  67. my $table_def;
  68. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  69. $opt_user, $opt_password,
  70. { PrintError => 0}) || die $DBI::errstr;
  71. foreach $table_def (@testtables)
  72. {
  73. $dbh->do("drop table " . $table_def->[0]);
  74. }
  75. $dbh->disconnect; $dbh=0; # Close handler
  76. }
  77. print ($errors ? "Test failed\n" :"Test ok\n");
  78. $end_time=new Benchmark;
  79. print "Total time: " .
  80. timestr(timediff($end_time, $start_time),"noc") . "\n";
  81. exit(0);
  82. #
  83. # Insert records in the table
  84. #
  85. sub test_truncate
  86. {
  87. my ($dbh,$i,$j,$count,$table_def,$table);
  88. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  89. $opt_user, $opt_password,
  90. { PrintError => 0}) || die $DBI::errstr;
  91. for ($count=0; $count < $opt_loop_count ; $count++)
  92. {
  93. my ($table)= ($testtables[0]->[0]);
  94. $dbh->do("truncate table $table") || die "Got error on truncate: $DBI::errstr\n";
  95. }
  96. $dbh->disconnect; $dbh=0;
  97. print "Test_truncate: Run $count times\n";
  98. exit(0);
  99. }