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-alter-table < prev    next >
Text File  |  2003-05-17  |  6KB  |  234 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. # Test of alter table
  20. #
  21. ##################### Standard benchmark inits ##############################
  22.  
  23. use DBI;
  24. use Benchmark;
  25.  
  26. $opt_start_field_count=8;    # start with this many fields
  27. $opt_loop_count=20;        # How many tests to do
  28. $opt_row_count=1000;         # Rows in the table
  29. $opt_field_count=1000;        # Add until this many fields.
  30. $opt_time_limit=10*60;        # Don't wait more than 10 min for some tests
  31.  
  32. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  33. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  34.  
  35. $opt_field_count=min($opt_field_count,$limits->{'max_columns'},
  36.              ($limits->{'query_size'}-30)/14);
  37. $opt_start_field_count=min($opt_start_field_count,$limits->{'max_index'});
  38.  
  39. if ($opt_small_test)
  40. {
  41.   $opt_row_count/=10;
  42.   $opt_field_count/=10;
  43. }
  44.  
  45. if (!$limits->{'alter_table'})
  46. {
  47.   print("Some of the servers given with --cmp or --server doesn't support ALTER TABLE\nTest aborted\n\n");
  48.   $start_time=new Benchmark;
  49.   end_benchmark($start_time);
  50.   exit 0;
  51. }
  52.  
  53. print "Testing of ALTER TABLE\n";
  54. print "Testing with $opt_field_count columns and $opt_row_count rows in $opt_loop_count steps\n";
  55.  
  56. ####
  57. #### Create a table and fill it with data
  58. ####
  59.  
  60. $dbh = $server->connect();
  61. @fields=();
  62. @index=();
  63. push(@fields,"i1 int not null");
  64. for ($i=2 ; $i <= $opt_start_field_count ; $i++)
  65. {
  66.   push(@fields,"i${i} int not null");
  67. }
  68. $field_count= $opt_start_field_count;
  69.  
  70. $start_time=new Benchmark;
  71.  
  72. $dbh->do("drop table bench" . $server->{'drop_attr'});
  73. do_many($dbh,$server->create("bench",\@fields,\@index));
  74.  
  75. print "Insert data into the table\n";
  76.  
  77. $loop_time=new Benchmark;
  78.  
  79. if ($opt_fast && $server->{transactions})
  80. {
  81.   $dbh->{AutoCommit} = 0;
  82.   print "Transactions enabled\n" if ($opt_debug);
  83. }
  84.  
  85. for ($i=0 ; $i < $opt_row_count ; $i++)
  86. {
  87.   $query="insert into bench values ( " . ("$i," x ($opt_start_field_count-1)) . "$i)";
  88.   $dbh->do($query) or die $DBI::errstr;
  89. }
  90.  
  91. if ($opt_fast && $server->{transactions})
  92. {
  93.   $dbh->commit;
  94.   $dbh->{AutoCommit} = 1;
  95. }
  96.  
  97. $end_time=new Benchmark;
  98.  
  99. print "Time for insert ($opt_row_count)",
  100.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  101.  
  102.  
  103. ####
  104. #### Add fields to the table.
  105. ####
  106.  
  107. $loop_time=new Benchmark;
  108. $add= int(($opt_field_count-$opt_start_field_count)/$opt_loop_count)+1;
  109.  
  110. $multi_add=$server->{'limits'}->{'alter_add_multi_col'} == 1;
  111. if ($opt_fast)
  112. {
  113.   $add=1 if (!$server->{'limits'}->{'alter_add_multi_col'});
  114. }
  115. else
  116. {
  117.   $add=1 if (!$limits->{'alter_add_multi_col'});
  118. }
  119.  
  120. $count=0;
  121. while ($field_count < $opt_field_count)
  122. {
  123.   $count++;
  124.   $end=min($field_count+$add,$opt_field_count);
  125.   $fields="";
  126.   $tmp="ADD ";
  127.   while ($field_count < $end)
  128.   {
  129.     $field_count++;
  130.     $fields.=",$tmp i${field_count} integer";
  131.     $tmp="" if (!$multi_add);            # Adabas
  132.   }
  133.   do_query($dbh,"ALTER TABLE bench " . substr($fields,1));
  134.   $end_time=new Benchmark;
  135.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  136.                      $opt_field_count/$add+1));
  137. }
  138.  
  139. $end_time=new Benchmark;
  140. if ($estimated)
  141. { print "Estimated time"; }
  142. else
  143. { print "Time"; }
  144. print " for alter_table_add ($count): " .
  145.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  146.  
  147. #
  148. # If estimated, fix table to have known number of fields
  149. #
  150. if ($estimated && $field_count < $opt_field_count)
  151. {
  152.   $fields="";
  153.   $tmp="ADD ";
  154.   while ($field_count < $opt_field_count)
  155.   {
  156.     $field_count++;
  157.     $fields.=",$tmp i${field_count} integer";
  158.     $tmp="" if (!$multi_add);            # Adabas
  159.   }
  160.   do_query($dbh,"ALTER TABLE bench " . substr($fields,1));
  161. }
  162.  
  163. ####
  164. #### Test adding and deleting index on the first $opt_start_fields
  165. ####
  166.  
  167. $loop_time=new Benchmark;
  168.  
  169. $count= 0;
  170. for ($i=1; $i <= $opt_start_field_count ; $i++)
  171. {
  172.   $dbh->do("CREATE INDEX bench_ind$i ON bench (i${i})") || die $DBI::errstr;
  173. }
  174.  
  175. $end_time=new Benchmark;
  176. print "Time for create_index ($opt_start_field_count): " .
  177.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  178.  
  179. $loop_time=new Benchmark;
  180. for ($i=1; $i <= $opt_start_field_count ; $i++)
  181. {
  182.   $dbh->do($server->drop_index("bench","bench_ind$i")) || die $DBI::errstr;
  183. }
  184.  
  185. $end_time=new Benchmark;
  186. print "Time for drop_index ($opt_start_field_count): " .
  187.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  188.  
  189. ####
  190. #### Delete fields from the table
  191. ####
  192.  
  193. goto skip_dropcol if (!$limits->{'alter_table_dropcol'});
  194.  
  195. $loop_time=new Benchmark;
  196.  
  197. $count=0;
  198. while ($field_count > $opt_start_field_count)
  199. {
  200.   $count++;
  201.   $end=max($field_count-$add,$opt_start_field_count);
  202.   $fields="";
  203.   while(--$field_count >= $end)
  204.   {
  205.     $fields.=",DROP i${field_count}";
  206.   }
  207.   $dbh->do("ALTER TABLE bench " . substr($fields,1) . $server->{'drop_attr'})
  208.   || die $DBI::errstr;
  209.   $end_time=new Benchmark;
  210.   last if ($estimated=predict_query_time($loop_time,$end_time,\$count,$count,
  211.                      $opt_field_count/$add+1));
  212. }
  213.  
  214. $end_time=new Benchmark;
  215. if ($estimated)
  216. { print "Estimated time"; }
  217. else
  218. { print "Time"; }
  219. print " for alter_table_drop ($count): " .
  220.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  221.  
  222. skip_dropcol:
  223.  
  224. ################################ END ###################################
  225. ####
  226. #### End of the test...Finally print time used to execute the
  227. #### whole test.
  228.  
  229. $dbh->do("drop table bench" . $server->{'drop_attr'});
  230.  
  231. $dbh->disconnect;
  232.  
  233. end_benchmark($start_time);
  234.