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