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.

365 lines
10 KiB

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