home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / sharewar / mysql / data1.cab / Development / bench / bench-init.pl next >
Encoding:
Perl Script  |  2001-11-02  |  13.6 KB  |  556 lines

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