You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
7.0 KiB

  1. #!/usr/bin/perl
  2. # Copyright (c) 2001, 2003, 2006 MySQL AB, 2009 Sun Microsystems, Inc.
  3. # Use is subject to license terms.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; version 2
  8. # of the License.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the Free
  17. # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. # MA 02110-1301, USA
  19. #
  20. # Test of transactions performance.
  21. #
  22. ##################### Standard benchmark inits ##############################
  23. use Cwd;
  24. use DBI;
  25. use Benchmark;
  26. #use warnings;
  27. $opt_groups=27; # Characters are 'A' -> Z
  28. $opt_loop_count=10000; # Change this to make test harder/easier
  29. $opt_medium_loop_count=100; # Change this to make test harder/easier
  30. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  31. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  32. # Avoid warnings for variables in bench-init.pl
  33. # (Only works with perl 5.6)
  34. #our ($opt_small_test, $opt_small_tables, $opt_debug, $opt_force);
  35. if ($opt_small_test || $opt_small_tables)
  36. {
  37. $opt_loop_count/=100;
  38. $opt_medium_loop_count/=10;
  39. }
  40. if (!$server->{transactions} && !$opt_force)
  41. {
  42. print "Test skipped because the database doesn't support transactions\n";
  43. exit(0);
  44. }
  45. ####
  46. #### Connect and start timeing
  47. ####
  48. $start_time=new Benchmark;
  49. $dbh = $server->connect();
  50. ###
  51. ### Create Table
  52. ###
  53. print "Creating tables\n";
  54. $dbh->do("drop table bench1");
  55. $dbh->do("drop table bench2");
  56. do_many($dbh,$server->create("bench1",
  57. ["idn int NOT NULL",
  58. "rev_idn int NOT NULL",
  59. "region char(1) NOT NULL",
  60. "grp int NOT NULL",
  61. "updated tinyint NOT NULL"],
  62. ["primary key (idn)",
  63. "unique (region,grp)"]));
  64. do_many($dbh,$server->create("bench2",
  65. ["idn int NOT NULL",
  66. "rev_idn int NOT NULL",
  67. "region char(1) NOT NULL",
  68. "grp int NOT NULL",
  69. "updated tinyint NOT NULL"],
  70. ["primary key (idn)",
  71. "unique (region,grp)"]));
  72. $dbh->{AutoCommit} = 0;
  73. ###
  74. ### Test insert perfomance
  75. ###
  76. test_insert("bench1","insert_commit",0);
  77. test_insert("bench2","insert_autocommit",1);
  78. sub test_insert
  79. {
  80. my ($table, $test_name, $auto_commit)= @_;
  81. my ($loop_time,$end_time,$id,$rev_id,$grp,$region);
  82. $dbh->{AutoCommit}= $auto_commit;
  83. $loop_time=new Benchmark;
  84. for ($id=0,$rev_id=$opt_loop_count-1 ; $id < $opt_loop_count ;
  85. $id++,$rev_id--)
  86. {
  87. $grp=$id/$opt_groups;
  88. $region=chr(65+$id%$opt_groups);
  89. do_query($dbh,"insert into $table values ($id,$rev_id,'$region',$grp,0)");
  90. }
  91. $dbh->commit if (!$auto_commit);
  92. $end_time=new Benchmark;
  93. print "Time for $test_name ($opt_loop_count): " .
  94. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  95. }
  96. ###
  97. ### Test rollback performance
  98. ###
  99. print "Test transactions rollback performance\n" if($opt_debug);
  100. ##
  101. ## Insert rollback test
  102. ##
  103. #
  104. # Test is done by inserting 100 rows in a table with lots of rows and
  105. # then doing a rollback on these
  106. #
  107. {
  108. my ($id,$rev_id,$grp,$region,$end,$loop_time,$end_time,$commit_loop,$count);
  109. $dbh->{AutoCommit} = 0;
  110. $loop_time=new Benchmark;
  111. $end=$opt_loop_count*2;
  112. $count=0;
  113. for ($commit_loop=1, $id=$opt_loop_count ; $id < $end ;
  114. $id++, $commit_loop++)
  115. {
  116. $rev_id=$end-$id;
  117. $grp=$id/$opt_groups;
  118. $region=chr(65+$id%$opt_groups);
  119. do_query($dbh,"insert into bench1 values ($id,$rev_id,'$region',$grp,0)");
  120. if ($commit_loop >= $opt_medium_loop_count)
  121. {
  122. $dbh->rollback;
  123. $commit_loop=0;
  124. $count++;
  125. }
  126. }
  127. if ($commit_loop > 1)
  128. {
  129. $dbh->rollback;
  130. $count++;
  131. }
  132. $end_time=new Benchmark;
  133. print "Time for insert_rollback ($count:$opt_loop_count): " .
  134. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  135. }
  136. ##
  137. ## Update rollback test
  138. ##
  139. #
  140. # Test is done by updating 100 rows in a table with lots of rows and
  141. # then doing a rollback on these
  142. #
  143. {
  144. my ($id,$loop_time,$end_time,$commit_loop,$count);
  145. $dbh->{AutoCommit} = 0;
  146. $loop_time=new Benchmark;
  147. $end=$opt_loop_count*2;
  148. $count=0;
  149. for ($commit_loop=1, $id=0 ; $id < $opt_loop_count ; $id++, $commit_loop++)
  150. {
  151. do_query($dbh,"update bench1 set updated=2 where idn=$id");
  152. if ($commit_loop >= $opt_medium_loop_count)
  153. {
  154. $dbh->rollback;
  155. $commit_loop=0;
  156. $count++;
  157. }
  158. }
  159. if ($commit_loop > 1)
  160. {
  161. $dbh->rollback;
  162. $count++;
  163. }
  164. $end_time=new Benchmark;
  165. print "Time for update_rollback ($count:$opt_loop_count): " .
  166. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  167. }
  168. ##
  169. ## Delete rollback test
  170. ##
  171. #
  172. # Test is done by deleting 100 rows in a table with lots of rows and
  173. # then doing a rollback on these
  174. #
  175. {
  176. my ($id,$loop_time,$end_time,$commit_loop,$count);
  177. $dbh->{AutoCommit} = 0;
  178. $loop_time=new Benchmark;
  179. $end=$opt_loop_count*2;
  180. $count=0;
  181. for ($commit_loop=1, $id=0 ; $id < $opt_loop_count ; $id++, $commit_loop++)
  182. {
  183. do_query($dbh,"delete from bench1 where idn=$id");
  184. if ($commit_loop >= $opt_medium_loop_count)
  185. {
  186. $dbh->rollback;
  187. $commit_loop=0;
  188. $count++;
  189. }
  190. }
  191. if ($commit_loop > 1)
  192. {
  193. $dbh->rollback;
  194. $count++;
  195. }
  196. $end_time=new Benchmark;
  197. print "Time for delete_rollback ($count:$opt_loop_count): " .
  198. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  199. }
  200. ###
  201. ### Test update perfomance
  202. ###
  203. test_update("bench1","update_commit",0);
  204. test_update("bench2","update_autocommit",1);
  205. sub test_update
  206. {
  207. my ($table, $test_name, $auto_commit)= @_;
  208. my ($loop_time,$end_time,$id);
  209. $dbh->{AutoCommit}= $auto_commit;
  210. $loop_time=new Benchmark;
  211. for ($id=0 ; $id < $opt_loop_count ; $id++)
  212. {
  213. do_query($dbh,"update $table set updated=1 where idn=$id");
  214. }
  215. $dbh->commit if (!$auto_commit);
  216. $end_time=new Benchmark;
  217. print "Time for $test_name ($opt_loop_count): " .
  218. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  219. }
  220. ###
  221. ### Test delete perfomance
  222. ###
  223. test_delete("bench1","delete_commit",0);
  224. test_delete("bench2","delete_autocommit",1);
  225. sub test_delete
  226. {
  227. my ($table, $test_name, $auto_commit)= @_;
  228. my ($loop_time,$end_time,$id);
  229. $dbh->{AutoCommit}= $auto_commit;
  230. $loop_time=new Benchmark;
  231. for ($id=0 ; $id < $opt_loop_count ; $id++)
  232. {
  233. do_query($dbh,"delete from $table where idn=$id");
  234. }
  235. $dbh->commit if (!$auto_commit);
  236. $end_time=new Benchmark;
  237. print "Time for $test_name ($opt_loop_count): " .
  238. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  239. }
  240. ####
  241. #### End of benchmark
  242. ####
  243. $sth = $dbh->do("drop table bench1" . $server->{'drop_attr'}) or die $DBI::errstr;
  244. $sth = $dbh->do("drop table bench2" . $server->{'drop_attr'}) or die $DBI::errstr;
  245. $dbh->disconnect; # close connection
  246. end_benchmark($start_time);