test-connect
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:9k
源码类别:

模拟服务器

开发平台:

C/C++

  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. use DBI;
  27. use Benchmark;
  28. $opt_loop_count=10000; # Change this to make test harder/easier
  29. $str_length=65000; # This is the length of blob strings in PART:5
  30. $max_test=20; # How many times to test if the server is busy
  31. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  32. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!n";
  33. # This is the length of blob strings in PART:5
  34. $str_length=min($limits->{'max_text_size'},$limits->{'query_size'}-30,$str_length);
  35. if ($opt_small_test)
  36. {
  37.   $opt_loop_count/=100;
  38. }
  39. $opt_loop_count=min(1000, $opt_loop_count) if ($opt_tcpip);
  40. print "Testing the speed of connecting to the server and sending of datan";
  41. print "All tests are done $opt_loop_count timesnn";
  42. ################################# PART:1 ###################################
  43. ####
  44. ####  Start timeing and start connect test..
  45. ####
  46. $start_time=new Benchmark;
  47. print "Testing connection/disconnectn";
  48. $loop_time=new Benchmark;
  49. $errors=0;
  50. for ($i=0 ; $i < $opt_loop_count ; $i++)
  51. {
  52.   print "$i " if (($opt_debug));
  53.   for ($j=0; $j < $max_test ; $j++)
  54.   {
  55.     if ($dbh = DBI->connect($server->{'data_source'}, $opt_user,
  56.     $opt_password))
  57.     {
  58.       $dbh->disconnect;
  59.       last;
  60.     }
  61.     select(undef, undef, undef, 0.01*$j);
  62.     print "$errors " if (($opt_debug));
  63.     $errors++;
  64.   }
  65.   die "Got error '$DBI::errstr' after $i connects" if ($j == $max_test);
  66.   $dbh->disconnect;
  67.   undef($dbh);
  68. }
  69. $end_time=new Benchmark;
  70. print "Warning: $errors connections didn't work without a time delayn" if ($errors);
  71. print "Time to connect ($opt_loop_count): " .
  72.   timestr(timediff($end_time, $loop_time),"all") . "nn";
  73. ################################# PART:2 ###################################
  74. #### Now we shall do first one connect, then simple select
  75. #### (select 1..) and then close connection. This will be
  76. #### done $opt_loop_count times.
  77. if ($limits->{'select_without_from'})
  78. {
  79.   print "Test connect/simple select/disconnectn";
  80.   $loop_time=new Benchmark;
  81.   for ($i=0; $i<$opt_loop_count; $i++)
  82.   {
  83.     $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password) || die $DBI::errstr;
  84.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  85.     $dbh->disconnect;
  86.   }
  87.   $end_time=new Benchmark;
  88.   print "Time for connect+select_simple ($opt_loop_count): " .
  89.     timestr(timediff($end_time, $loop_time),"all") . "nn";
  90. }
  91. ################################# PART:3 ###################################
  92. ####
  93. #### Okay..Next thing we'll do is a simple select $opt_loop_count times.
  94. ####
  95. $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password,
  96.     { PrintError => 0}) || die $DBI::errstr;
  97. if ($limits->{'select_without_from'})
  98. {
  99.   print "Test simple selectn";
  100.   $loop_time=new Benchmark;
  101.   for ($i=0 ; $i<$opt_loop_count ; $i++)
  102.   {
  103.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  104.   }
  105.   $end_time=new Benchmark;
  106.   print "Time for select_simple ($opt_loop_count): " .
  107.     timestr(timediff($end_time, $loop_time),"all") . "nn";
  108. }
  109. ################################# PART:4 ###################################
  110. #### First, we'll create a simple table 'bench1'
  111. #### Then we shall do $opt_loop_count selects from this table.
  112. #### Table will contain very simple data.
  113. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'});
  114. do_many($dbh,$server->create("bench1",
  115.      ["a int NOT NULL",
  116.       "i int",
  117.       "s char(10)"],
  118.      ["primary key (a)"]));
  119. $sth = $dbh->do("insert into bench1 values(1,100,'AAA')") or die $DBI::errstr;
  120. if ($opt_fast && defined($server->{vacuum}))
  121. {
  122.   $server->vacuum(0,$dbh);
  123. }
  124. $dbh->disconnect;
  125. #
  126. # First test connect/select/disconnect
  127. #
  128. print "Testing connect/select 1 row from table/disconnectn";
  129. $loop_time=new Benchmark;
  130. $errors=0;
  131. for ($i=0 ; $i<$opt_loop_count ; $i++)
  132. {
  133.   for ($j=0; $j < $max_test ; $j++)
  134.   {
  135.     last if ($dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password));
  136.     $errors++;
  137.   }
  138.   die $DBI::errstr if ($j == $max_test);
  139.   $sth = $dbh->do("select * from bench1") #Select * from table with 1 record
  140.     or die $DBI::errstr;
  141.   $dbh->disconnect;
  142. }
  143. $end_time=new Benchmark;
  144. print "Warning: $errors connections didn't work without a time delayn" if ($errors);
  145. print "Time to connect+select_1_row ($opt_loop_count): " .
  146.   timestr(timediff($end_time, $loop_time),"all") . "nn";
  147. #
  148. # The same test, but without connect/disconnect
  149. #
  150. print "Testing select 1 row from tablen";
  151. $dbh = $server->connect();
  152. $loop_time=new Benchmark;
  153. for ($i=0 ; $i<$opt_loop_count ; $i++)
  154. {
  155.   $sth = $dbh->do("select * from bench1") # Select * from table with 1 record
  156.     or die $DBI::errstr;
  157. }
  158. $end_time=new Benchmark;
  159. print "Time to select_1_row ($opt_loop_count): " .
  160.   timestr(timediff($end_time, $loop_time),"all") . "nn";
  161. #
  162. # The same test, but with 2 rows.
  163. #
  164. print "Testing select 2 rows from tablen";
  165. $sth = $dbh->do("insert into bench1 values(2,200,'BBB')")
  166.   or die $DBI::errstr;
  167. $loop_time=new Benchmark;
  168. for ($i=0 ; $i<$opt_loop_count ; $i++)
  169. {
  170.   $sth = $dbh->do("select * from bench1") # Select * from table with 2 record
  171.     or die $DBI::errstr;
  172. }
  173. $end_time=new Benchmark;
  174. print "Time to select_2_rows ($opt_loop_count): " .
  175.   timestr(timediff($end_time, $loop_time),"all") . "nn";
  176. if ($limits->{'functions'})
  177. {
  178.   print "Test select with aritmetic (+)n";
  179.   $loop_time=new Benchmark;
  180.   for ($i=0; $i<$opt_loop_count; $i++)
  181.   {
  182.     $sth = $dbh->do("select a+a+a+a+a+a+a+a+a+a from bench1") or die $DBI::errstr;
  183.   }
  184.   $end_time=new Benchmark;
  185.   print "Time for select_column+column ($opt_loop_count): " .
  186.     timestr(timediff($end_time, $loop_time),"all") . "nn";
  187. }
  188. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  189.   or die $DBI::errstr;
  190. if ($opt_fast && defined($server->{vacuum}))
  191. {
  192.   $server->vacuum(0,$dbh);
  193. }
  194. ################################# PART:5 ###################################
  195. #### We'll create one table with a single blob field,but with a
  196. #### huge record in it and then we'll do $opt_loop_count selects
  197. #### from it.
  198. goto skip_blob_test if (!$limits->{'working_blobs'});
  199. print "Testing retrieval of big records ($str_length bytes)n";
  200. do_many($dbh,$server->create("bench1", ["b blob"], []));
  201. $dbh->{LongReadLen}= $str_length; # Set retrieval buffer
  202. my $string=(A) x ($str_length); # This will make a string $str_length long.
  203. $sth = $dbh->prepare("insert into bench1 values(?)") or die $dbh->errstr;
  204. $sth->execute($string) or die $sth->errstr;
  205. undef($string);
  206. if ($opt_fast && defined($server->{vacuum}))
  207. {
  208.   $server->vacuum(0,$dbh);
  209. }
  210. $loop_time=new Benchmark;
  211. for ($i=0 ; $i < $opt_loop_count ; $i++)
  212. {
  213.   $sth = $dbh->prepare("select * from bench1");
  214.   if (!$sth->execute || !(@row = $sth->fetchrow_array) ||
  215.       length($row[0]) != $str_length)
  216.   {
  217.     warn "$DBI::errstr - ".length($row[0])." - $str_length **n";
  218.   }
  219.   $sth->finish;
  220. }
  221. $end_time=new Benchmark;
  222. print "Time to select_big_str ($opt_loop_count): " .
  223.   timestr(timediff($end_time, $loop_time),"all") . "nn";
  224. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'})
  225.   or do
  226. {
  227.     # Fix for Access 2000
  228.     die $dbh->errstr if (!$dbh->abort_if_fatal_error());
  229. };
  230. if ($opt_fast && defined($server->{vacuum}))
  231. {
  232.   $server->vacuum(0,$dbh);
  233. }
  234. skip_blob_test:
  235. ################################ END ###################################
  236. ####
  237. #### End of the test...Finally print time used to execute the
  238. #### whole test.
  239. $dbh->disconnect;
  240. end_benchmark($start_time);