home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 July & August / PCWorld_2003-07-08_cd.bin / Software / Vyzkuste / mysql / data1.cab / Development / bench / bench-init.pl next >
Perl Script  |  2003-05-17  |  16KB  |  614 lines

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