home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / sql-bench / test-create < prev    next >
Encoding:
Text File  |  2004-10-28  |  6.9 KB  |  269 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 how long it takes to create tables,
  20. # make a count(*) from them and finally drop the tables. These
  21. # commands will be done in different ways in this test.
  22. # Using option --fast will drop all the tables in the end
  23. # of this test with one command instead of making own
  24. # 'drop' command for each and every table.
  25. # By changing the variable '$table_amount' value you can make
  26. # this test a lot harder/easier for your computer to drive.
  27. # Note that when using value bigger than 64 for this variable
  28. # will do 'drop table'-command    in totally different way because of that
  29. # how MySQL handles these commands.
  30.  
  31. ##################### Standard benchmark inits ##############################
  32.  
  33. use Cwd;
  34. use DBI;
  35. use Benchmark;
  36.  
  37. $opt_loop_count=10000; # Change this to make test harder/easier
  38. # This is the default value for the amount of tables used in this test.
  39.  
  40. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  41. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  42.  
  43. $create_loop_count=$opt_loop_count;
  44. if ($opt_small_test)
  45. {
  46.   $opt_loop_count/=100;
  47.   $create_loop_count/=1000;
  48. }
  49.  
  50. $max_tables=min($limits->{'max_tables'},$opt_loop_count);
  51.  
  52. if ($opt_small_test)
  53. {
  54.   $max_tables=10;
  55. }
  56.  
  57.  
  58. print "Testing the speed of creating and dropping tables\n";
  59. print "Testing with $max_tables tables and $opt_loop_count loop count\n\n";
  60.  
  61. ####
  62. ####  Connect and start timeing
  63. ####
  64.  
  65. $dbh = $server->connect();
  66.  
  67. ### Test how the database can handle many tables
  68. ### Create $max_tables ; Access all off them with a simple query
  69. ### and then drop the tables
  70.  
  71. if ($opt_force) # If tables used in this test exist, drop 'em
  72. {
  73.   print "Okay..Let's make sure that our tables don't exist yet.\n\n";
  74.   for ($i=1 ; $i <= $max_tables ; $i++)
  75.   {
  76.     $dbh->do("drop table bench_$i" . $server->{'drop_attr'});
  77.   }
  78. }
  79.  
  80. if ($opt_fast && defined($server->{vacuum}))
  81. {
  82.   $server->vacuum(1,\$dbh);
  83. }
  84.  
  85. print "Testing create of tables\n";
  86.  
  87. $loop_time=$start_time=new Benchmark;
  88.  
  89. for ($i=1 ; $i <= $max_tables ; $i++)
  90. {
  91.   if (do_many($dbh,$server->create("bench_$i",
  92.                    ["i int NOT NULL",
  93.                     "d double",
  94.                     "f float",
  95.                     "s char(10)",
  96.                     "v varchar(100)"],
  97.                    ["primary key (i)"])))
  98.   {
  99.     # Got an error; Do cleanup
  100.     for ($i=1 ; $i <= $max_tables ; $i++)
  101.     {
  102.       $dbh->do("drop table bench_$i" . $server->{'drop_attr'});
  103.     }
  104.     die "Test aborted";
  105.   }
  106. }
  107.  
  108. $end_time=new Benchmark;
  109. print "Time for create_MANY_tables ($max_tables): " .
  110.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  111.  
  112. if ($opt_fast && defined($server->{vacuum}))
  113. {
  114.   $server->vacuum(1,\$dbh);
  115. }
  116.  
  117. #### Here comes $max_tables couples of cont(*) to the tables.
  118. #### We'll check how long it will take...
  119. ####
  120.  
  121. print "Accessing tables\n";
  122.  
  123. if ($limits->{'group_functions'})
  124. {
  125.   $query="select count(*) from ";
  126.   $type="select_group_when_MANY_tables";
  127. }
  128. else
  129. {
  130.   $query="select * from ";
  131.   $type="select_when_MANY_tables";
  132. }
  133.  
  134. $loop_time=new Benchmark;
  135. for ($i=1 ; $i <= $max_tables ; $i++)
  136. {
  137.   $sth = $dbh->do("$query bench_$i") or die $DBI::errstr;
  138. }
  139.  
  140. $end_time=new Benchmark;
  141. print "Time to $type ($max_tables): " .
  142.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  143.  
  144. ####
  145. #### Now we are going to drop $max_tables tables;
  146. ####
  147.  
  148. print "Testing drop\n";
  149.  
  150. $loop_time=new Benchmark;
  151.  
  152. if ($opt_fast && $server->{'limits'}->{'multi_drop'} &&
  153.     $server->{'limits'}->{'query_size'} > 11+$max_tables*10)
  154. {
  155.   my $query="drop table bench_1";
  156.   for ($i=2 ; $i <= $max_tables ; $i++)
  157.   {
  158.     $query.=",bench_$i";
  159.   }
  160.   $sth = $dbh->do($query . $server->{'drop_attr'}) or die $DBI::errstr;
  161. }
  162. else
  163. {
  164.   for ($i=1 ; $i <= $max_tables ; $i++)
  165.   {
  166.     $sth = $dbh->do("drop table bench_$i" . $server->{'drop_attr'})
  167.       or die $DBI::errstr;
  168.   }
  169. }
  170.  
  171.  
  172. $end_time=new Benchmark;
  173. print "Time for drop_table_when_MANY_tables ($max_tables): " .
  174.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  175.  
  176. if ($opt_fast && defined($server->{vacuum}))
  177. {
  178.   $server->vacuum(1,\$dbh);
  179. }
  180.  
  181. #### We'll do first one 'create table' and then we'll drop it
  182. #### away immediately. This loop shall be executed $opt_loop_count
  183. #### times.
  184.  
  185. print "Testing create+drop\n";
  186.  
  187. $loop_time=new Benchmark;
  188.  
  189. for ($i=1 ; $i <= $create_loop_count ; $i++)
  190. {
  191.   do_many($dbh,$server->create("bench_$i",
  192.                    ["i int NOT NULL",
  193.                 "d double",
  194.                 "f float",
  195.                 "s char(10)",
  196.                 "v varchar(100)"],
  197.                    ["primary key (i)"]));
  198.   $sth = $dbh->do("drop table bench_$i" . $server->{'drop_attr'}) or die $DBI::errstr;
  199. }
  200.  
  201. $end_time=new Benchmark;
  202. print "Time for create+drop ($create_loop_count): " .
  203.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  204.  
  205. if ($opt_fast && defined($server->{vacuum}))
  206. {
  207.   $server->vacuum(1,\$dbh);
  208. }
  209.  
  210. #
  211. # Same test, but with a table with many keys
  212. #
  213.  
  214. my @fields=(); my @keys=();
  215. $keys=min($limits->{'max_index'},16);        # 16 is more than enough
  216. $seg= min($limits->{'max_index_parts'},$keys,16);    # 16 is more than enough
  217.  
  218. # Make keys on the most important types
  219. @types=(0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1);    # A 1 for each char field
  220. push(@fields,"field1 tinyint not null");
  221. push(@fields,"field2 mediumint not null");
  222. push(@fields,"field3 smallint not null");
  223. push(@fields,"field4 char(16) not null");
  224. push(@fields,"field5 integer not null");
  225. push(@fields,"field6 float not null");
  226. push(@fields,"field7 double not null");
  227. for ($i=8 ; $i <= $keys ; $i++)
  228. {
  229.   push(@fields,"field$i char(5) not null");    # Should be relatively fair
  230. }
  231.  
  232. # Let first key contain many segments
  233. my $query="primary key (";
  234. for ($i= 1 ; $i <= $seg ; $i++)
  235. {
  236.   $query.= "field$i,";
  237. }
  238. substr($query,-1)=")";
  239. push (@keys,$query);
  240.  
  241. #Create other keys
  242. for ($i=2 ; $i <= $keys ; $i++)
  243. {
  244.   push(@keys,"index index$i (field$i)");
  245. }
  246.  
  247. $loop_time=new Benchmark;
  248. for ($i=1 ; $i <= $opt_loop_count ; $i++)
  249. {
  250.   do_many($dbh,$server->create("bench_$i", \@fields, \@index));
  251.   $dbh->do("drop table bench_$i" . $server->{'drop_attr'}) or die $DBI::errstr;
  252. }
  253.  
  254. $end_time=new Benchmark;
  255. print "Time for create_key+drop ($opt_loop_count): " .
  256.     timestr(timediff($end_time, $loop_time),"all") . "\n";
  257.  
  258. if ($opt_fast && defined($server->{vacuum}))
  259. {
  260.   $server->vacuum(1,\$dbh);
  261. }
  262.  
  263. ####
  264. #### End of benchmark
  265. ####
  266.  
  267. $dbh->disconnect;                # close connection
  268. end_benchmark($start_time);
  269.