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.

380 lines
11 KiB

26 years ago
26 years ago
26 years ago
  1. #!/usr/bin/perl -w
  2. # Copyright (C) 2000, 2001 MySQL AB
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; version 2 of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. # This is a test for INSERT DELAYED
  17. #
  18. $opt_loop_count=10000; # Change this to make test harder/easier
  19. ##################### Standard benchmark inits ##############################
  20. use DBI;
  21. use Getopt::Long;
  22. use Benchmark;
  23. package main;
  24. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  25. $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  26. $opt_host=$opt_user=$opt_password=""; $opt_db="test";
  27. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  28. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  29. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef; # Ignore warnings from these
  30. print "Testing 8 multiple connections to a server with 1 insert, 2 delayed\n";
  31. print "insert, 1 update, 1 delete, 1 flush tables and 3 select connections.\n";
  32. $firsttable = "bench_f1";
  33. $secondtable = "bench_f2";
  34. ####
  35. #### Start timeing and start test
  36. ####
  37. $start_time=new Benchmark;
  38. if (!$opt_skip_create)
  39. {
  40. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  41. $Mysql::QUIET = 1;
  42. $dbh->do("drop table if exists $firsttable,$secondtable");
  43. $Mysql::QUIET = 0;
  44. print "Creating tables $firsttable and $secondtable in database $opt_db\n";
  45. $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") or die $DBI::errstr;
  46. $dbh->do("create table $secondtable (id int(6) not null, row int(3) not null,value double, primary key(id,row))") or die $DBI::errstr;
  47. $dbh->disconnect;
  48. }
  49. $|= 1; # Autoflush
  50. ####
  51. #### Start the tests
  52. ####
  53. test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
  54. test_delayed_1() if (($pid=fork()) == 0); $work{$pid}="delayed_insert1";
  55. test_delayed_2() if (($pid=fork()) == 0); $work{$pid}="delayed_insert2";
  56. test_2() if (($pid=fork()) == 0); $work{$pid}="update";
  57. test_3() if (($pid=fork()) == 0); $work{$pid}="select1";
  58. test_4() if (($pid=fork()) == 0); $work{$pid}="select2";
  59. test_5() if (($pid=fork()) == 0); $work{$pid}="select3";
  60. test_del() if (($pid=fork()) == 0); $work{$pid}="delete";
  61. test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
  62. $errors=0;
  63. while (($pid=wait()) != -1)
  64. {
  65. $ret=$?/256;
  66. print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  67. $errors++ if ($ret != 0);
  68. }
  69. if (!$opt_skip_delete && !$errors)
  70. {
  71. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  72. $dbh->do("drop table $firsttable");
  73. $dbh->do("drop table $secondtable");
  74. }
  75. print ($errors ? "Test failed\n" :"Test ok\n");
  76. $end_time=new Benchmark;
  77. print "Total time: " .
  78. timestr(timediff($end_time, $start_time),"noc") . "\n";
  79. exit(0);
  80. #
  81. # Insert records in the two tables
  82. #
  83. sub test_1
  84. {
  85. my ($dbh,$tmpvar,$rows,$found,$i);
  86. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  87. $tmpvar=1;
  88. $rows=$found=0;
  89. for ($i=0 ; $i < $opt_loop_count; $i++)
  90. {
  91. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  92. $dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
  93. $row_count=($i % 7)+1;
  94. $rows+=1+$row_count;
  95. for ($j=0 ; $j < $row_count; $j++)
  96. {
  97. $dbh->do("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  98. }
  99. }
  100. $dbh->disconnect;
  101. print "Test_1: Inserted $rows rows\n";
  102. exit(0);
  103. }
  104. sub test_delayed_1
  105. {
  106. my ($dbh,$tmpvar,$rows,$found,$i,$id);
  107. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  108. $tmpvar=1;
  109. $rows=$found=0;
  110. for ($i=0 ; $i < $opt_loop_count; $i++)
  111. {
  112. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  113. $id=$i+$opt_loop_count;
  114. $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  115. $row_count=($i % 7)+1;
  116. $rows+=1+$row_count;
  117. for ($j=0 ; $j < $row_count; $j++)
  118. {
  119. $dbh->do("insert into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  120. }
  121. if (($tmpvar % 100) == 0)
  122. {
  123. $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  124. $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";
  125. $found+=2;
  126. }
  127. }
  128. $dbh->disconnect;
  129. print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  130. exit(0);
  131. }
  132. sub test_delayed_2
  133. {
  134. my ($dbh,$tmpvar,$rows,$found,$i,$id);
  135. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  136. $tmpvar=1;
  137. $rows=$found=0;
  138. for ($i=0 ; $i < $opt_loop_count; $i++)
  139. {
  140. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  141. $id=$i+$opt_loop_count*2;
  142. $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  143. $row_count=($i % 7)+1;
  144. $rows+=1+$row_count;
  145. for ($j=0 ; $j < $row_count; $j++)
  146. {
  147. $dbh->do("insert delayed into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  148. }
  149. if (($tmpvar % 100) == 0)
  150. {
  151. $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  152. $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";
  153. $found+=2;
  154. }
  155. }
  156. $dbh->disconnect;
  157. print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  158. exit(0);
  159. }
  160. #
  161. # Update records in both tables
  162. #
  163. sub test_2
  164. {
  165. my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp,$sth,$count);
  166. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  167. $tmpvar=111111;
  168. $rows=$found=$max_id=$id=0;
  169. for ($i=0 ; $i < $opt_loop_count ; $i++)
  170. {
  171. $tmp=(($tmpvar + 63) + $i)*3;
  172. $tmp=$tmp-int($tmp/100000)*100000;
  173. $tmpvar^= $tmp;
  174. $tmp=$tmpvar - int($tmpvar/10)*10;
  175. if ($max_id*$tmp == 0)
  176. {
  177. $max_id=0;
  178. $sth=$dbh->prepare("select max(id) from $firsttable where marker=''");
  179. $sth->execute() || die "Got error select max: $DBI::errstr\n";
  180. if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  181. {
  182. $found++;
  183. $max_id=$id=$row[0];
  184. }
  185. $sth->finish;
  186. }
  187. else
  188. {
  189. $id= $tmpvar % ($max_id-1)+1;
  190. }
  191. if ($id)
  192. {
  193. ($count=$dbh->do("update $firsttable set marker='x' where id=$id")) || die "Got error update $firsttable: $DBI::errstr\n";
  194. $rows+=$count;
  195. if ($count > 0)
  196. {
  197. $count=$dbh->do("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $DBI::errstr\n";
  198. $rows+=$count;
  199. }
  200. }
  201. }
  202. $dbh->disconnect;
  203. print "Test_2: Found $found rows, Updated $rows rows\n";
  204. exit(0);
  205. }
  206. #
  207. # select records
  208. #
  209. sub test_3
  210. {
  211. my ($dbh,$id,$tmpvar,$rows,$i,$count);
  212. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  213. $tmpvar=222222;
  214. $rows=0;
  215. for ($i=0 ; $i < $opt_loop_count ; $i++)
  216. {
  217. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  218. $id=$tmpvar % $opt_loop_count;
  219. $count=$dbh->do("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $DBI::errstr\n";
  220. $rows+=$count;
  221. }
  222. $dbh->disconnect;
  223. print "Test_3: Found $rows rows\n";
  224. exit(0);
  225. }
  226. #
  227. # Note that this uses row=1 and in some cases won't find any matching
  228. # records
  229. #
  230. sub test_4
  231. {
  232. my ($dbh,$id,$tmpvar,$rows,$i,$count);
  233. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  234. $tmpvar=333333;
  235. $rows=0;
  236. for ($i=0 ; $i < $opt_loop_count; $i++)
  237. {
  238. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  239. $id=$tmpvar % $opt_loop_count;
  240. $count=$dbh->do("select id from $secondtable where id=$id") || die "Got error on select from $secondtable: $DBI::errstr\n";
  241. $rows+=$count;
  242. }
  243. $dbh->disconnect;
  244. print "Test_4: Found $rows rows\n";
  245. exit(0);
  246. }
  247. sub test_5
  248. {
  249. my ($dbh,$id,$tmpvar,$rows,$i,$max_id,$count,$sth);
  250. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  251. $tmpvar=444444;
  252. $rows=$max_id=0;
  253. for ($i=0 ; $i < $opt_loop_count ; $i++)
  254. {
  255. $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  256. if ($max_id == 0 || ($tmpvar % 10 == 0))
  257. {
  258. $sth=$dbh->prepare("select max(id) from $firsttable");
  259. $sth->execute() || die "Got error select max: $DBI::errstr\n";
  260. if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  261. {
  262. $max_id=$id=$row[0];
  263. }
  264. else
  265. {
  266. $id=0;
  267. }
  268. $sth->finish;
  269. }
  270. else
  271. {
  272. $id= $tmpvar % $max_id;
  273. }
  274. $count=$dbh->do("select value from $firsttable,$secondtable where $firsttable.id=$id and $secondtable.id=$firsttable.id") || die "Got error on select from $secondtable: $DBI::errstr\n";
  275. $rows+=$count;
  276. }
  277. $dbh->disconnect;
  278. print "Test_5: Found $rows rows\n";
  279. exit(0);
  280. }
  281. #
  282. # Delete the smallest row
  283. #
  284. sub test_del
  285. {
  286. my ($dbh,$min_id,$i,$sth,$rows);
  287. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  288. $rows=0;
  289. for ($i=0 ; $i < $opt_loop_count/3; $i++)
  290. {
  291. $sth=$dbh->prepare("select min(id) from $firsttable");
  292. $sth->execute() || die "Got error on select from $firsttable: $DBI::errstr\n";
  293. if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  294. {
  295. $min_id=$row[0];
  296. }
  297. $sth->finish;
  298. $dbh->do("delete from $firsttable where id = $min_id") || die "Got error on DELETE from $firsttable: $DBI::errstr\n";
  299. $rows++;
  300. }
  301. $dbh->disconnect;
  302. print "Test_del: Deleted $rows rows\n";
  303. exit(0);
  304. }
  305. #
  306. # Do a flush tables once in a while
  307. #
  308. sub test_flush
  309. {
  310. my ($dbh,$sth,$found1,$last_found1,$i,@row);
  311. $found1=0; $last_found1=-1;
  312. $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  313. $opt_user, $opt_password,
  314. { PrintError => 0}) || die $DBI::errstr;
  315. for ($i=0; $found1 != $last_found1 ; $i++)
  316. {
  317. $sth=$dbh->prepare("flush tables") || die "Got error on prepare: $dbh->errstr\n";
  318. $sth->execute || die $dbh->errstr;
  319. $sth->finish;
  320. $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepare: $dbh->errstr\n";
  321. $sth->execute || die $dbh->errstr;
  322. @row = $sth->fetchrow_array();
  323. $last_found1=$found1;
  324. $found1= $row[0];
  325. $sth->finish;
  326. sleep(5);
  327. }
  328. $dbh->disconnect; $dbh=0;
  329. print "flush: Did $i repair/checks\n";
  330. exit(0);
  331. }