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.

316 lines
6.9 KiB

  1. #!@PERL@
  2. # Test of table elimination feature
  3. use Cwd;
  4. use DBI;
  5. use Getopt::Long;
  6. use Benchmark;
  7. $opt_loop_count=100000;
  8. $opt_medium_loop_count=10000;
  9. $opt_small_loop_count=100;
  10. $pwd = cwd(); $pwd = "." if ($pwd eq '');
  11. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  12. if ($opt_small_test)
  13. {
  14. $opt_loop_count/=10;
  15. $opt_medium_loop_count/=10;
  16. $opt_small_loop_count/=10;
  17. }
  18. print "Testing table elimination feature\n";
  19. print "The test table has $opt_loop_count rows.\n\n";
  20. # A query to get the recent versions of all attributes:
  21. $select_current_full_facts="
  22. select
  23. F.id, A1.attr1, A2.attr2
  24. from
  25. elim_facts F
  26. left join elim_attr1 A1 on A1.id=F.id
  27. left join elim_attr2 A2 on A2.id=F.id and
  28. A2.fromdate=(select MAX(fromdate) from
  29. elim_attr2 where id=A2.id);
  30. ";
  31. $select_current_full_facts="
  32. select
  33. F.id, A1.attr1, A2.attr2
  34. from
  35. elim_facts F
  36. left join elim_attr1 A1 on A1.id=F.id
  37. left join elim_attr2 A2 on A2.id=F.id and
  38. A2.fromdate=(select MAX(fromdate) from
  39. elim_attr2 where id=F.id);
  40. ";
  41. # TODO: same as above but for some given date also?
  42. # TODO:
  43. ####
  44. #### Connect and start timeing
  45. ####
  46. $dbh = $server->connect();
  47. $start_time=new Benchmark;
  48. ####
  49. #### Create needed tables
  50. ####
  51. goto select_test if ($opt_skip_create);
  52. print "Creating tables\n";
  53. $dbh->do("drop table elim_facts" . $server->{'drop_attr'});
  54. $dbh->do("drop table elim_attr1" . $server->{'drop_attr'});
  55. $dbh->do("drop table elim_attr2" . $server->{'drop_attr'});
  56. # The facts table
  57. do_many($dbh,$server->create("elim_facts",
  58. ["id integer"],
  59. ["primary key (id)"]));
  60. # Attribute1, non-versioned
  61. do_many($dbh,$server->create("elim_attr1",
  62. ["id integer",
  63. "attr1 integer"],
  64. ["primary key (id)",
  65. "key (attr1)"]));
  66. # Attribute2, time-versioned
  67. do_many($dbh,$server->create("elim_attr2",
  68. ["id integer",
  69. "attr2 integer",
  70. "fromdate date"],
  71. ["primary key (id, fromdate)",
  72. "key (attr2,fromdate)"]));
  73. #NOTE: ignoring: if ($limits->{'views'})
  74. $dbh->do("drop view elim_current_facts");
  75. $dbh->do("create view elim_current_facts as $select_current_full_facts");
  76. if ($opt_lock_tables)
  77. {
  78. do_query($dbh,"LOCK TABLES elim_facts, elim_attr1, elim_attr2 WRITE");
  79. }
  80. if ($opt_fast && defined($server->{vacuum}))
  81. {
  82. $server->vacuum(1,\$dbh);
  83. }
  84. ####
  85. #### Fill the facts table
  86. ####
  87. $n_facts= $opt_loop_count;
  88. if ($opt_fast && $server->{transactions})
  89. {
  90. $dbh->{AutoCommit} = 0;
  91. }
  92. print "Inserting $n_facts rows into facts table\n";
  93. $loop_time=new Benchmark;
  94. $query="insert into elim_facts values (";
  95. for ($id=0; $id < $n_facts ; $id++)
  96. {
  97. do_query($dbh,"$query $id)");
  98. }
  99. if ($opt_fast && $server->{transactions})
  100. {
  101. $dbh->commit;
  102. $dbh->{AutoCommit} = 1;
  103. }
  104. $end_time=new Benchmark;
  105. print "Time to insert ($n_facts): " .
  106. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  107. ####
  108. #### Fill attr1 table
  109. ####
  110. if ($opt_fast && $server->{transactions})
  111. {
  112. $dbh->{AutoCommit} = 0;
  113. }
  114. print "Inserting $n_facts rows into attr1 table\n";
  115. $loop_time=new Benchmark;
  116. $query="insert into elim_attr1 values (";
  117. for ($id=0; $id < $n_facts ; $id++)
  118. {
  119. $attr1= ceil(rand($n_facts));
  120. do_query($dbh,"$query $id, $attr1)");
  121. }
  122. if ($opt_fast && $server->{transactions})
  123. {
  124. $dbh->commit;
  125. $dbh->{AutoCommit} = 1;
  126. }
  127. $end_time=new Benchmark;
  128. print "Time to insert ($n_facts): " .
  129. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  130. ####
  131. #### Fill attr2 table
  132. ####
  133. if ($opt_fast && $server->{transactions})
  134. {
  135. $dbh->{AutoCommit} = 0;
  136. }
  137. print "Inserting $n_facts rows into attr2 table\n";
  138. $loop_time=new Benchmark;
  139. for ($id=0; $id < $n_facts ; $id++)
  140. {
  141. # Two values for each $id - current one and obsolete one.
  142. $attr1= ceil(rand($n_facts));
  143. $query="insert into elim_attr2 values ($id, $attr1, now())";
  144. do_query($dbh,$query);
  145. $query="insert into elim_attr2 values ($id, $attr1, '2009-01-01')";
  146. do_query($dbh,$query);
  147. }
  148. if ($opt_fast && $server->{transactions})
  149. {
  150. $dbh->commit;
  151. $dbh->{AutoCommit} = 1;
  152. }
  153. $end_time=new Benchmark;
  154. print "Time to insert ($n_facts): " .
  155. timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  156. ####
  157. #### Finalize the database population
  158. ####
  159. if ($opt_lock_tables)
  160. {
  161. do_query($dbh,"UNLOCK TABLES");
  162. }
  163. if ($opt_fast && defined($server->{vacuum}))
  164. {
  165. $server->vacuum(0,\$dbh,["elim_facts", "elim_attr1", "elim_attr2"]);
  166. }
  167. if ($opt_lock_tables)
  168. {
  169. do_query($dbh,"LOCK TABLES elim_facts, elim_attr1, elim_attr2 WRITE");
  170. }
  171. ####
  172. #### Do some selects on the table
  173. ####
  174. select_test:
  175. #
  176. # The selects will be:
  177. # - N pk-lookups with all attributes
  178. # - pk-attribute-based lookup
  179. # - latest-attribute value based lookup.
  180. ###
  181. ### Bare facts select:
  182. ###
  183. print "testing bare facts facts table\n";
  184. $loop_time=new Benchmark;
  185. $rows=0;
  186. for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  187. {
  188. $val= ceil(rand($n_facts));
  189. $rows+=fetch_all_rows($dbh,"select * from elim_facts where id=$val");
  190. }
  191. $count=$i;
  192. $end_time=new Benchmark;
  193. print "time for select_bare_facts ($count:$rows): " .
  194. timestr(timediff($end_time, $loop_time),"all") . "\n";
  195. ###
  196. ### Full facts select, no elimination:
  197. ###
  198. print "testing full facts facts table\n";
  199. $loop_time=new Benchmark;
  200. $rows=0;
  201. for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  202. {
  203. $val= rand($n_facts);
  204. $rows+=fetch_all_rows($dbh,"select * from elim_current_facts where id=$val");
  205. }
  206. $count=$i;
  207. $end_time=new Benchmark;
  208. print "time for select_two_attributes ($count:$rows): " .
  209. timestr(timediff($end_time, $loop_time),"all") . "\n";
  210. ###
  211. ### Now with elimination: select only only one fact
  212. ###
  213. print "testing selection of one attribute\n";
  214. $loop_time=new Benchmark;
  215. $rows=0;
  216. for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  217. {
  218. $val= rand($n_facts);
  219. $rows+=fetch_all_rows($dbh,"select id, attr1 from elim_current_facts where id=$val");
  220. }
  221. $count=$i;
  222. $end_time=new Benchmark;
  223. print "time for select_one_attribute ($count:$rows): " .
  224. timestr(timediff($end_time, $loop_time),"all") . "\n";
  225. ###
  226. ### Now with elimination: select only only one fact
  227. ###
  228. print "testing selection of one attribute\n";
  229. $loop_time=new Benchmark;
  230. $rows=0;
  231. for ($i=0 ; $i < $opt_medium_loop_count ; $i++)
  232. {
  233. $val= rand($n_facts);
  234. $rows+=fetch_all_rows($dbh,"select id, attr2 from elim_current_facts where id=$val");
  235. }
  236. $count=$i;
  237. $end_time=new Benchmark;
  238. print "time for select_one_attribute ($count:$rows): " .
  239. timestr(timediff($end_time, $loop_time),"all") . "\n";
  240. ;
  241. ####
  242. #### End of benchmark
  243. ####
  244. if ($opt_lock_tables)
  245. {
  246. do_query($dbh,"UNLOCK TABLES");
  247. }
  248. if (!$opt_skip_delete)
  249. {
  250. do_query($dbh,"drop table elim_facts, elim_attr1, elim_attr2" . $server->{'drop_attr'});
  251. }
  252. if ($opt_fast && defined($server->{vacuum}))
  253. {
  254. $server->vacuum(0,\$dbh);
  255. }
  256. $dbh->disconnect; # close connection
  257. end_benchmark($start_time);