bench-init.pl.sh
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:14k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. #!@PERL@
  2. # Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  3. #
  4. # This library is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Library General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 2 of the License, or (at your option) any later version.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. # Library General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Library General Public
  15. # License along with this library; if not, write to the Free
  16. # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  17. # MA 02111-1307, USA
  18. #
  19. ##########################################################
  20. # this is the base file every test is using ....
  21. # this is made for not changing every file if we want to
  22. # add an option or just want to change something in
  23. # code what is the same in every file ...
  24. ##########################################################
  25. #
  26. # The exported values are:
  27. # $opt_... Various options
  28. # $date Current date in ISO format
  29. # $server Object for current server
  30. # $limits Hash reference to limits for benchmark
  31. $benchmark_version="2.12";
  32. use Getopt::Long;
  33. require "$pwd/server-cfg" || die "Can't read Configuration file: $!n";
  34. $|=1; # Output data immediately
  35. $opt_skip_test=$opt_skip_create=$opt_skip_delete=$opt_verbose=$opt_fast_insert=$opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=$opt_log=$opt_use_old_results=$opt_help=$opt_odbc=$opt_small_test=$opt_small_tables=$opt_samll_key_tables=$opt_stage=$opt_old_headers=$opt_die_on_errors=$opt_tcpip=0;
  36. $opt_cmp=$opt_user=$opt_password="";
  37. $opt_server="mysql"; $opt_dir="output";
  38. $opt_host="localhost";$opt_database="test";
  39. $opt_machine=""; $opt_suffix="";
  40. $opt_create_options=undef;
  41. $opt_time_limit=10*60; # Don't wait more than 10 min for some tests
  42. $log_prog_args=join(" ", skip_arguments(@ARGV,"comments","cmp","server",
  43. "user", "host", "database", "password",
  44. "use-old-results","skip-test",
  45. "machine", "dir", "suffix", "log"));
  46. GetOptions("skip-test=s","comments=s","cmp=s","server=s","user=s","host=s","database=s","password=s","loop-count=i","row-count=i","skip-create","skip-delete","verbose","fast-insert","lock-tables","debug","fast","force","field-count=i","regions=i","groups=i","time-limit=i","log","use-old-results","machine=s","dir=s","suffix=s","help","odbc","small-test","small-tables","small-key-tables","stage=i","old-headers","die-on-errors","create-options=s","hires","tcpip","silent") || usage();
  47. usage() if ($opt_help);
  48. $server=get_server($opt_server,$opt_host,$opt_database,$opt_odbc,
  49.                    machine_part());
  50. $limits=merge_limits($server,$opt_cmp);
  51. $date=date();
  52. @estimated=(0.0,0.0,0.0); # For estimated time support
  53. if ($opt_hires)
  54. {
  55.   eval "use Time::HiRes;";
  56. }
  57. {
  58.   my $tmp= $opt_server;
  59.   $tmp =~ s/_odbc$//;
  60.   if (length($opt_cmp) && index($opt_cmp,$tmp) < 0)
  61.   {
  62.     $opt_cmp.=",$tmp";
  63.   }
  64. }
  65. $opt_cmp=lc(join(",",sort(split(',',$opt_cmp))));
  66. #
  67. # set opt_lock_tables if one uses --fast and drivers supports it
  68. #
  69. if (($opt_lock_tables || $opt_fast) && $server->{'limits'}->{'lock_tables'})
  70. {
  71.   $opt_lock_tables=1;
  72. }
  73. else
  74. {
  75.   $opt_lock_tables=0;
  76. }
  77. if ($opt_fast)
  78. {
  79.   $opt_fast_insert=1;
  80.   $opt_suffix="_fast" if (!length($opt_suffix));
  81. }
  82. if ($opt_odbc)
  83. {
  84.    $opt_suffix="_odbc" if (!length($opt_suffix));
  85. }
  86. if (!$opt_silent)
  87. {
  88.   print "Testing server '" . $server->version() . "' at $datenn";
  89. }
  90. if ($opt_debug)
  91. {
  92.   print "nCurrent limits: n";
  93.   foreach $key (sort keys %$limits)
  94.   {
  95.     print $key . " " x (30-length($key)) . $limits->{$key} . "n";
  96.   }
  97.   print "n";
  98. }
  99. #
  100. # Some help functions
  101. #
  102. sub skip_arguments
  103. {
  104.   my($argv,@skip_args)=@_;
  105.   my($skip,$arg,$name,@res);
  106.   foreach $arg (@$argv)
  107.   {
  108.     if ($arg =~ /^-+([^=]*)/)
  109.     {
  110.       $name=$1;
  111.       foreach $skip (@skip_args)
  112.       {
  113. if (index($skip,$name) == 0)
  114. {
  115.   $name=""; # Don't use this parameters
  116.   last;
  117. }
  118.       }
  119.       push (@res,$arg) if (length($name));
  120.     }
  121.   }
  122.   return @res;
  123. }
  124. sub merge_limits
  125. {
  126.   my ($server,$cmp)= @_;
  127.   my ($name,$tmp_server,$limits,$res_limits,$limit,$tmp_limits);
  128.   $res_limits=$server->{'limits'};
  129.   if ($cmp)
  130.   {
  131.     foreach $name (split(",",$cmp))
  132.     {
  133.       $tmp_server= get_server($name,$opt_host, $opt_database,
  134.       $opt_odbc) || die "Unknown SQL server: $namen";
  135.       $limits=$tmp_server->{'limits'};
  136.       %new_limits=();
  137.       foreach $limit (keys(%$limits))
  138.       {
  139. if (defined($res_limits->{$limit}) && defined($limits->{$limit}))
  140. {
  141.   $new_limits{$limit}=min($res_limits->{$limit},$limits->{$limit});
  142. }
  143.       }
  144.       %tmp_limits=%new_limits;
  145.       $res_limits=%tmp_limits;
  146.     }
  147.   }
  148.   return $res_limits;
  149. }
  150. sub date
  151. {
  152.   my ($sec, $min, $hour, $mday, $mon, $year) = localtime(time());
  153.   sprintf("%04d-%02d-%02d %2d:%02d:%02d",
  154.   1900+$year,$mon+1,$mday,$hour,$min,$sec);
  155. }
  156. sub min
  157. {
  158.   my($min)=$_[0];
  159.   my($i);
  160.   for ($i=1 ; $i <= $#_; $i++)
  161.   {
  162.     $min=$_[$i] if ($min > $_[$i]);
  163.   }
  164.   return $min;
  165. }
  166. sub max
  167. {
  168.   my($max)=$_[0];
  169.   my($i);
  170.   for ($i=1 ; $i <= $#_; $i++)
  171.   {
  172.     $max=$_[$i] if ($max < $_[$i]);
  173.   }
  174.   return $max;
  175. }
  176. #
  177. # Execute many statements in a row
  178. #
  179. sub do_many
  180. {
  181.   my ($dbh,@statements)=@_;
  182.   my ($statement,$sth);
  183.   foreach $statement (@statements)
  184.   {
  185.     if (!($sth=$dbh->do($statement)))
  186.     {
  187.       die "Can't execute command '$statement'nError: $DBI::errstrn";
  188.     }
  189.   }
  190. }
  191. sub safe_do_many
  192. {
  193.   my ($dbh,@statements)=@_;
  194.   my ($statement,$sth);
  195.   foreach $statement (@statements)
  196.   {
  197.     if (!($sth=$dbh->do($statement)))
  198.     {
  199.       print STDERR "Can't execute command '$statement'nError: $DBI::errstrn";
  200.       return 1;
  201.     }
  202.   }
  203.   return 0;
  204. }
  205. #
  206. # Do a query and fetch all rows from a statement and return the number of rows
  207. #
  208. sub fetch_all_rows
  209. {
  210.   my ($dbh,$query,$must_get_result)=@_;
  211.   my ($count,$sth);
  212.   $count=0;
  213.   print "$query: " if ($opt_debug);
  214.   if (!($sth= $dbh->prepare($query)))
  215.   {
  216.     print "n" if ($opt_debug);
  217.     die "Error occured with prepare($query)n -> $DBI::errstrn";
  218.     return undef;
  219.   }
  220.   if (!$sth->execute)
  221.   {
  222.     print "n" if ($opt_debug);
  223.     if (defined($server->{'error_on_execute_means_zero_rows'}) &&
  224.        !$server->abort_if_fatal_error())
  225.     {
  226.       if (defined($must_get_result) && $must_get_result)
  227.       {
  228. die "Error: Query $query didn't return any rowsn";
  229.       }
  230.       $sth->finish;
  231.       print "0n" if ($opt_debug);
  232.       return 0;
  233.     }
  234.     die "Error occured with execute($query)n -> $DBI::errstrn";
  235.     $sth->finish;
  236.     return undef;
  237.   }
  238.   while ($sth->fetchrow_arrayref)
  239.   {
  240.     $count++;
  241.   }
  242.   print "$countn" if ($opt_debug);
  243.   if (defined($must_get_result) && $must_get_result && !$count)
  244.   {
  245.     die "Error: Query $query didn't return any rowsn";
  246.   }
  247.   $sth->finish;
  248.   undef($sth);
  249.   return $count;
  250. }
  251. sub do_query
  252. {
  253.   my($dbh,$query)=@_;
  254.   print "$queryn" if ($opt_debug);
  255.   $dbh->do($query) or
  256.     die "nError executing '$query':n$DBI::errstrn";
  257. }
  258. #
  259. # Handle estimated time of the server is too slow
  260. # Returns 0 if one should continue as normal
  261. #
  262. sub predict_query_time
  263. {
  264.   my ($loop_time,$end_time,$count_ref,$loop,$loop_count)= @_;
  265.   my ($k,$tmp);
  266.   if (($end_time->[0] - $loop_time->[0]) > $opt_time_limit)
  267.   {
  268.     # We can't wait until the SUN dies.  Try to predict the end time
  269.     if ($loop != $loop_count)
  270.     {
  271.       $tmp=($end_time->[0] - $loop_time->[0]);
  272.       print "Note: Query took longer then time-limit: $opt_time_limitnEstimating end time based on:n";
  273.       print "$$count_ref queries in $loop loops of $loop_count loops took $tmp secondsn";
  274.       for ($k=0; $k < 3; $k++)
  275.       {
  276. $tmp=$loop_time->[$k]+($end_time->[$k]-$loop_time->[$k])/$loop*
  277.   $loop_count;
  278. $estimated[$k]+=($tmp-$end_time->[$k]);
  279. $end_time->[$k]=$tmp;
  280.       }
  281.       $$count_ref= int($$count_ref/$loop*$loop_count);
  282.       return 1;
  283.     }
  284.   }
  285.   return 0;
  286. }
  287. #
  288. # standard end of benchmark
  289. #
  290. sub end_benchmark
  291. {
  292.   my ($start_time)=@_;
  293.   $end_time=new Benchmark;
  294.   if ($estimated[0])
  295.   {
  296.     print "Estimated total time: ";
  297.     $end_time->[0]+=$estimated[0];
  298.     $end_time->[1]+=$estimated[1];
  299.     $end_time->[2]+=$estimated[2];
  300.   }
  301.   else
  302.   {
  303.     print "Total time: "
  304.     }
  305.   print timestr(timediff($end_time, $start_time),"all") . "n";
  306.   exit 0;
  307. }
  308. sub print_time
  309. {
  310.   my ($estimated)=@_;
  311.   if ($estimated)
  312.   { print "Estimated time"; }
  313.   else
  314.   { print "Time"; }
  315. }
  316. #
  317. # Create a filename part for the machine that can be used for log file.
  318. #
  319. sub machine_part
  320. {
  321.   my ($name);
  322.   return $opt_machine if (length($opt_machine)); # Specified by user
  323.   $name=machine();
  324.   $name="win9$1" if ($name =~ /win.*9(d)/i);
  325.   $name="NT_$1" if ($name =~ /Windows NT.*(d+.d+)/i);
  326.   $name =~ s/s+/_/g; # Make the filenames easier to parse
  327.   $name =~ s/-/_/g;
  328.   $name =~ s///_/g;
  329.   return $name;
  330. }
  331. sub machine
  332. {
  333.   $name= `uname -s -r -m`;
  334.   if ($?)
  335.   {
  336.     $name= `uname -s -m`;
  337.   }
  338.   if ($?)
  339.   {
  340.     $name= `uname -s`;
  341.   }
  342.   if ($?)
  343.   {
  344.     $name= `uname`;
  345.   }
  346.   if ($?)
  347.   {
  348.     $name="unknown";
  349.   }
  350.   chomp($name); $name =~ s/[nr]//g;
  351.   return $name;
  352. }
  353. #
  354. # Usage
  355. #
  356. sub usage
  357. {
  358.     print <<EOF;
  359. The MySQL benchmarks Ver $benchmark_version
  360. All benchmarks takes the following options:
  361. --comments
  362.   Add a comment to the benchmark output.  Comments should contain
  363.   extra information that 'uname -a' doesn't give and if the database was
  364.   stared with some specific, non default, options.
  365. --cmp=server[,server...]
  366.   Run the test with limits from the given servers.  If you run all servers
  367.   with the same --cmp, you will get a test that is comparable between
  368.   the different sql servers.
  369. --create-options=#
  370.   Extra argument to all create statements.  If you for example want to
  371.   create all MySQL tables as BDB tables use:
  372.   --create-options=TYPE=BDB
  373. --database (Default $opt_database)
  374.   In which database the test tables are created.
  375. --debug
  376.   This is a test specific option that is only used when debugging a test.
  377.   Print out debugging information.
  378. --dir (Default $opt_dir)
  379.   Option to 'run-all-tests' to where the test results should be stored.
  380. --fast
  381.   Allow the database to use non standard ANSI SQL commands to make the
  382.   test go faster.
  383. --fast-insert
  384.   Use "insert into table_name values(...)" instead of
  385.   "insert into table_name (....) values(...)"
  386.   If the database supports it, some tests uses multiple value lists.
  387. --field-count
  388.   This is a test specific option that is only used when debugging a test.
  389.   This usually means how many fields there should be in the test table.
  390. --force
  391.   This is a test specific option that is only used when debugging a test.
  392.   Continue the test even if there is some error.
  393.   Delete tables before creating new ones.
  394. --groups (Default $opt_groups)
  395.   This is a test specific option that is only used when debugging a test.
  396.   This usually means how many different groups there should be in the test.
  397. --lock-tables
  398.   Allow the database to use table locking to get more speed.
  399. --log
  400.   Option to 'run-all-tests' to save the result to the '--dir' directory.
  401. --loop-count (Default $opt_loop_count)
  402.   This is a test specific option that is only used when debugging a test.
  403.   This usually means how many times times each test loop is executed.
  404. --help
  405.   Shows this help
  406. --host='host name' (Default $opt_host)
  407.   Host name where the database server is located.
  408. --machine="machine or os_name"
  409.   The machine/os name that is added to the benchmark output filename.
  410.   The default is the OS name + version.
  411. --odbc
  412.   Use the ODBC DBI driver to connect to the database.
  413. --password='password'
  414.   Password for the current user.
  415. --regions
  416.   This is a test specific option that is only used when debugging a test.
  417.   This usually means how AND levels should be tested.
  418. --old-headers
  419.   Get the old benchmark headers from the old RUN- file.
  420. --server='server name'  (Default $opt_server)
  421.   Run the test on the given SQL server.
  422.   Known servers names are: Access, Adabas, AdabasD, Empress, Oracle,
  423.   Informix, DB2, mSQL, MS-SQL, MySQL, Pg, Solid and Sybase
  424. --silent
  425.   Don't print info about the server when starting test.
  426. --skip-delete
  427.   This is a test specific option that is only used when debugging a test.
  428.   This will keep the test tables after the test is run.
  429. --skip-test=test1[,test2,...]
  430.   For run-all-programs;  Don't execute the named tests.
  431. --small-test
  432.   This runs some tests with smaller limits to get a faster test.
  433.   Can be used if you just want to verify that the database works, but
  434.   don't have time to run a full test.
  435. --small-tables
  436.   This runs some tests that generate big tables with fewer rows.
  437.   This can be used with databases that can't handle that many rows
  438.   because of pre-sized partitions.
  439. --suffix (Default $opt_suffix)
  440.   The suffix that is added to the database name in the benchmark output
  441.   filename.  This can be used to run the benchmark multiple times with
  442.   different server options without overwritten old files.
  443.   When using --fast the suffix is automaticly set to '_fast'.
  444. --tcpip
  445.   Inform test suite that we are using TCP/IP to connect to the server. In
  446.   this case we cant do many new connections in a row as we in this case may
  447.   fill the TCP/IP stack
  448. --time-limit (Default $opt_time_limit)
  449.   How long a test loop is allowed to take, in seconds, before the end result
  450.   is 'estimated'.
  451. --use-old-results
  452.   Option to 'run-all-tests' to use the old results from the  '--dir' directory
  453.   instead of running the tests.
  454. --user='user_name'
  455.   User name to log into the SQL server.
  456. --verbose
  457.   This is a test specific option that is only used when debugging a test.
  458.   Print more information about what is going on.
  459. EOF
  460.   exit(0);
  461. }
  462. ####
  463. #### The end of the base file ...
  464. ####
  465. 1;