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 / test-connect < prev    next >
Text File  |  2003-05-17  |  10KB  |  331 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. # This test is for testing the speed of connections and sending
  20. # data to the client.
  21. #
  22. # By changing the variable '$opt_loop_count' value you can make this test
  23. # easier/harderto your computer to execute. You can also change this value
  24. # by using option --loop_value='what_ever_you_like'.
  25. ##################### Standard benchmark inits ##############################
  26.  
  27. use DBI;
  28. use Benchmark;
  29.  
  30. $opt_loop_count=100000;    # Change this to make test harder/easier
  31. $str_length=65000;    # This is the length of blob strings in PART:5
  32. $max_test=20;        # How many times to test if the server is busy
  33.  
  34. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  35. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  36.  
  37. # This is the length of blob strings in PART:5
  38. $str_length=min($limits->{'max_text_size'},$limits->{'query_size'}-30,$str_length);
  39.  
  40. if ($opt_small_test)
  41. {
  42.   $opt_loop_count/=100;
  43. }
  44.  
  45. $opt_loop_count=min(1000, $opt_loop_count) if ($opt_tcpip);
  46. $small_loop_count=$opt_loop_count/10; # For connect tests
  47.  
  48. print "Testing the speed of connecting to the server and sending of data\n";
  49. print "Connect tests are done $small_loop_count times and other tests $opt_loop_count times\n\n";
  50.  
  51. ################################# PART:1 ###################################
  52. ####
  53. ####  Start timeing and start connect test..
  54. ####
  55.  
  56. $start_time=new Benchmark;
  57.  
  58. print "Testing connection/disconnect\n";
  59.  
  60. $loop_time=new Benchmark;
  61. $errors=0;
  62.  
  63. for ($i=0 ; $i < $small_loop_count ; $i++)
  64. {
  65.   print "$i " if (($opt_debug));
  66.   for ($j=0; $j < $max_test ; $j++)
  67.   {
  68.     if ($dbh = DBI->connect($server->{'data_source'}, $opt_user,
  69.                 $opt_password))
  70.     {
  71.       $dbh->disconnect;
  72.       last;
  73.     }
  74.     select(undef, undef, undef, 0.01*$j);
  75.     print "$errors " if (($opt_debug));
  76.     $errors++;
  77.   }
  78.   die "Got error '$DBI::errstr' after $i connects" if ($j == $max_test);
  79.   $dbh->disconnect;
  80.   undef($dbh);
  81. }
  82. $end_time=new Benchmark;
  83. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  84. print "Time to connect ($small_loop_count): " .
  85.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  86.  
  87. ################################# PART:2 ###################################
  88. #### Now we shall do first one connect, then simple select
  89. #### (select 1..) and then close connection. This will be
  90. #### done $small_loop_count times.
  91.  
  92. if ($limits->{'select_without_from'})
  93. {
  94.   print "Test connect/simple select/disconnect\n";
  95.   $loop_time=new Benchmark;
  96.  
  97.   for ($i=0; $i < $small_loop_count; $i++)
  98.   {
  99.     $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password) || die $DBI::errstr;
  100.     $sth = $dbh->do("select $i") or die $DBI::errstr;
  101.     $dbh->disconnect;
  102.   }
  103.   $end_time=new Benchmark;
  104.   print "Time for connect+select_simple ($small_loop_count): " .
  105.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  106. }
  107.  
  108. ################################# PART:3 ###################################
  109. ####
  110. #### Okay..Next thing we'll do is a simple select $opt_loop_count times.
  111. ####
  112.  
  113. $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password,
  114.             { PrintError => 0}) || die $DBI::errstr;
  115.  
  116. if ($limits->{'select_without_from'})
  117. {
  118.   print "Test simple select\n";
  119.   $loop_time=new Benchmark;
  120.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  121.   {
  122.     $sth = $dbh->do("select $i") or die $DBI::errstr;
  123.   }
  124.   $end_time=new Benchmark;
  125.   print "Time for select_simple ($opt_loop_count): " .
  126.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  127. }
  128.  
  129. ###########################################################################
  130. #### The same as the previous test, but always execute the same select
  131. #### This is done to test the query cache for real simple selects.
  132.  
  133. if ($limits->{'select_without_from'})
  134. {
  135.   print "Test simple select\n";
  136.   $loop_time=new Benchmark;
  137.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  138.   {
  139.     $sth = $dbh->do("select 10000") or die $DBI::errstr;
  140.   }
  141.   $end_time=new Benchmark;
  142.   print "Time for select_simple_cache ($opt_loop_count): " .
  143.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  144. }
  145.  
  146. ##########################################################################
  147. #### First, we'll create a simple table 'bench1'
  148. #### Then we shall do $opt_loop_count selects from this table.
  149. #### Table will contain very simple data.
  150.  
  151. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  152. do_many($dbh,$server->create("bench1",
  153.                  ["a int NOT NULL",
  154.                   "i int",
  155.                   "s char(10)"],
  156.                  ["primary key (a)"]));
  157. $sth = $dbh->do("insert into bench1 values(1,100,'AAA')") or die $DBI::errstr;
  158.  
  159. if ($opt_fast && defined($server->{vacuum}))
  160. {
  161.   $server->vacuum(0,\$dbh);
  162. }
  163. $dbh->disconnect;
  164.  
  165. #
  166. # First test connect/select/disconnect
  167. #
  168. print "Testing connect/select 1 row from table/disconnect\n";
  169.  
  170. $loop_time=new Benchmark;
  171. $errors=0;
  172.  
  173. for ($i=0 ; $i < $small_loop_count ; $i++)
  174. {
  175.   for ($j=0; $j < $max_test ; $j++)
  176.   {
  177.     last if ($dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password));
  178.     $errors++;
  179.   }
  180.   die $DBI::errstr if ($j == $max_test);
  181.  
  182.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 1 record
  183.     or die $DBI::errstr;
  184.   $dbh->disconnect;
  185. }
  186.  
  187. $end_time=new Benchmark;
  188. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  189. print "Time to connect+select_1_row ($small_loop_count): " .
  190.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  191.  
  192. #
  193. # The same test, but without connect/disconnect
  194. #
  195. print "Testing select 1 row from table\n";
  196.  
  197. $dbh = $server->connect();
  198. $loop_time=new Benchmark;
  199.  
  200. for ($i=0 ; $i < $opt_loop_count ; $i++)
  201. {
  202.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 1 record
  203.     or die $DBI::errstr;
  204. }
  205.  
  206. $end_time=new Benchmark;
  207. print "Time to select_1_row ($opt_loop_count): " .
  208.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  209.  
  210. #
  211. # Same test (as with one row) but now with a cacheable query
  212. #
  213.  
  214. $loop_time=new Benchmark;
  215.  
  216. for ($i=0 ; $i < $opt_loop_count ; $i++)
  217. {
  218.   $sth = $dbh->do("select a,i,s from bench1") # Select * from table with 1 record
  219.     or die $DBI::errstr;
  220. }
  221. $end_time=new Benchmark;
  222. print "Time to select_1_row_cache ($opt_loop_count): " .
  223.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  224.  
  225. #
  226. # The same test, but with 2 rows (not cacheable).
  227. #
  228.  
  229. print "Testing select 2 rows from table\n";
  230.  
  231. $sth = $dbh->do("insert into bench1 values(2,200,'BBB')")
  232.   or die $DBI::errstr;
  233.  
  234. $loop_time=new Benchmark;
  235.  
  236. for ($i=0 ; $i < $opt_loop_count ; $i++)
  237. {
  238.   $sth = $dbh->do("select a,i,s,$i from bench1") # Select * from table with 2 record
  239.     or die $DBI::errstr;
  240. }
  241.  
  242. $end_time=new Benchmark;
  243. print "Time to select_2_rows ($opt_loop_count): " .
  244.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  245.  
  246. #
  247. # Simple test to test speed of functions.
  248. #
  249.  
  250. if ($limits->{'functions'})
  251. {
  252.   print "Test select with aritmetic (+)\n";
  253.   $loop_time=new Benchmark;
  254.  
  255.   for ($i=0; $i < $opt_loop_count; $i++)
  256.   {
  257.     $sth = $dbh->do("select a+a+a+a+a+a+a+a+a+$i from bench1") or die $DBI::errstr;
  258.   }
  259.   $end_time=new Benchmark;
  260.   print "Time for select_column+column ($opt_loop_count): " .
  261.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  262. }
  263.  
  264. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  265.   or die $DBI::errstr;
  266.  
  267. if ($opt_fast && defined($server->{vacuum}))
  268. {
  269.   $server->vacuum(0,\$dbh);
  270. }
  271.  
  272. ################################# PART:5 ###################################
  273. #### We'll create one table with a single blob field,but with a
  274. #### huge record in it and then we'll do $opt_loop_count selects
  275. #### from it.
  276.  
  277. goto skip_blob_test if (!$limits->{'working_blobs'});
  278.  
  279. print "Testing retrieval of big records ($str_length bytes)\n";
  280.  
  281. do_many($dbh,$server->create("bench1", ["b blob"], []));
  282. $dbh->{LongReadLen}= $str_length; # Set retrieval buffer
  283.  
  284. my $string=(A) x ($str_length); # This will make a string $str_length long.
  285. $sth = $dbh->prepare("insert into bench1 values(?)") or die $dbh->errstr;
  286. $sth->execute($string) or die $sth->errstr;
  287. undef($string);
  288. if ($opt_fast && defined($server->{vacuum}))
  289. {
  290.   $server->vacuum(0,\$dbh);
  291. }
  292.  
  293. $loop_time=new Benchmark;
  294.  
  295. for ($i=0 ; $i < $small_loop_count ; $i++)
  296. {
  297.   $sth = $dbh->prepare("select b,$i from bench1");
  298.   if (!$sth->execute || !(@row = $sth->fetchrow_array) ||
  299.       length($row[0]) != $str_length)
  300.   {
  301.     warn "$DBI::errstr - ".length($row[0])." - $str_length **\n";
  302.   }
  303.   $sth->finish;
  304. }
  305.  
  306. $end_time=new Benchmark;
  307. print "Time to select_big_str ($small_loop_count): " .
  308.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  309.  
  310. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  311.   or do
  312. {
  313.     # Fix for Access 2000
  314.     die $dbh->errstr if (!$server->abort_if_fatal_error());
  315. };
  316.  
  317. if ($opt_fast && defined($server->{vacuum}))
  318. {
  319.   $server->vacuum(0,\$dbh);
  320. }
  321.  
  322. skip_blob_test:
  323.  
  324. ################################ END ###################################
  325. ####
  326. #### End of the test...Finally print time used to execute the
  327. #### whole test.
  328.  
  329. $dbh->disconnect;
  330. end_benchmark($start_time);
  331.