speed4.test 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # 2007 October 23
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #*************************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this script is measuring executing speed. More specifically,
  13. # the focus is on the speed of:
  14. #
  15. # * joins
  16. # * views
  17. # * sub-selects
  18. # * triggers
  19. #
  20. # $Id: speed4.test,v 1.2 2008/07/12 14:52:20 drh Exp $
  21. #
  22. set testdir [file dirname $argv0]
  23. source $testdir/tester.tcl
  24. speed_trial_init speed1
  25. # Set a uniform random seed
  26. expr srand(0)
  27. set sqlout [open speed1.txt w]
  28. proc tracesql {sql} {
  29. puts $::sqlout $sql\;
  30. }
  31. #db trace tracesql
  32. # The number_name procedure below converts its argment (an integer)
  33. # into a string which is the English-language name for that number.
  34. #
  35. # Example:
  36. #
  37. # puts [number_name 123] -> "one hundred twenty three"
  38. #
  39. set ones {zero one two three four five six seven eight nine
  40. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  41. eighteen nineteen}
  42. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  43. proc number_name {n} {
  44. if {$n>=1000} {
  45. set txt "[number_name [expr {$n/1000}]] thousand"
  46. set n [expr {$n%1000}]
  47. } else {
  48. set txt {}
  49. }
  50. if {$n>=100} {
  51. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  52. set n [expr {$n%100}]
  53. }
  54. if {$n>=20} {
  55. append txt " [lindex $::tens [expr {$n/10}]]"
  56. set n [expr {$n%10}]
  57. }
  58. if {$n>0} {
  59. append txt " [lindex $::ones $n]"
  60. }
  61. set txt [string trim $txt]
  62. if {$txt==""} {set txt zero}
  63. return $txt
  64. }
  65. # Summary of tests:
  66. #
  67. # speed4-join1: Join three tables using IPK index.
  68. # speed4-join2: Join three tables using an index.
  69. # speed4-join3: Join two tables without an index.
  70. #
  71. # speed4-view1: Querying a view.
  72. # speed4-table1: Same queries as in speed4-view1, but run directly against
  73. # the tables for comparison purposes.
  74. #
  75. # speed4-subselect1: A SELECT statement that uses many sub-queries..
  76. #
  77. # speed4-trigger1: An INSERT statement that fires a trigger.
  78. # speed4-trigger2: An UPDATE statement that fires a trigger.
  79. # speed4-trigger3: A DELETE statement that fires a trigger.
  80. # speed4-notrigger1: Same operation as trigger1, but without the trigger.
  81. # speed4-notrigger2: " trigger2 "
  82. # speed4-notrigger3: " trigger3 "
  83. #
  84. # Set up the schema. Each of the tables t1, t2 and t3 contain 50,000 rows.
  85. # This creates a database of around 16MB.
  86. execsql {
  87. BEGIN;
  88. CREATE TABLE t1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  89. CREATE TABLE t2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  90. CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  91. CREATE VIEW v1 AS SELECT rowid, i, t FROM t1;
  92. CREATE VIEW v2 AS SELECT rowid, i, t FROM t2;
  93. CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;
  94. }
  95. for {set jj 1} {$jj <= 3} {incr jj} {
  96. set stmt [string map "%T% t$jj" {INSERT INTO %T% VALUES(NULL, $i, $t)}]
  97. for {set ii 0} {$ii < 50000} {incr ii} {
  98. set i [expr {int(rand()*50000)}]
  99. set t [number_name $i]
  100. execsql $stmt
  101. }
  102. }
  103. execsql {
  104. CREATE INDEX i1 ON t1(t);
  105. CREATE INDEX i2 ON t2(t);
  106. CREATE INDEX i3 ON t3(t);
  107. COMMIT;
  108. }
  109. # Before running these tests, disable the compiled statement cache built into
  110. # the Tcl interface. This is because we want to test the speed of SQL
  111. # compilation as well as execution.
  112. #
  113. db cache size 0
  114. # Join t1, t2, t3 on IPK.
  115. set sql "SELECT * FROM t1, t2, t3 WHERE t1.oid = t2.oid AND t2.oid = t3.oid"
  116. speed_trial speed4-join1 50000 row $sql
  117. # Join t1, t2, t3 on the non-IPK index.
  118. set sql "SELECT * FROM t1, t2, t3 WHERE t1.t = t2.t AND t2.t = t3.t"
  119. speed_trial speed4-join2 50000 row $sql
  120. # Run 10000 simple queries against the views.
  121. set sql ""
  122. for {set ii 1} {$ii < 10000} {incr ii} {
  123. append sql "SELECT * FROM v[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
  124. }
  125. speed_trial speed4-view1 10000 stmt $sql
  126. # Run the same 10000 simple queries as in the previous test case against
  127. # the underlying tables. The compiled vdbe programs should be identical, so
  128. # the only difference in running time is the extra time taken to compile
  129. # the view definitions.
  130. #
  131. set sql ""
  132. for {set ii 1} {$ii < 10000} {incr ii} {
  133. append sql "SELECT t FROM t[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
  134. }
  135. speed_trial speed4-table1 10000 stmt $sql
  136. # Run a SELECT that uses sub-queries 10000 times. A total of 30000 sub-selects.
  137. #
  138. set sql ""
  139. for {set ii 1} {$ii < 10000} {incr ii} {
  140. append sql "
  141. SELECT (SELECT t FROM t1 WHERE rowid = [expr {$ii*3}]),
  142. (SELECT t FROM t2 WHERE rowid = [expr {$ii*3}]),
  143. (SELECT t FROM t3 WHERE rowid = [expr {$ii*3}])
  144. ;"
  145. }
  146. speed_trial speed4-subselect1 10000 stmt $sql
  147. # The following block tests the speed of some DML statements that cause
  148. # triggers to fire.
  149. #
  150. execsql {
  151. CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);
  152. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  153. CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN
  154. INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);
  155. END;
  156. CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN
  157. INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);
  158. END;
  159. CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN
  160. INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);
  161. END;
  162. BEGIN;
  163. }
  164. set sql ""
  165. for {set ii 1} {$ii < 10000} {incr ii} {
  166. append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
  167. }
  168. speed_trial speed4-trigger1 10000 stmt $sql
  169. set sql ""
  170. for {set ii 1} {$ii < 20000} {incr ii 2} {
  171. set ii2 [expr {$ii*2}]
  172. append sql "
  173. UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
  174. "
  175. }
  176. speed_trial speed4-trigger2 10000 stmt $sql
  177. set sql ""
  178. for {set ii 1} {$ii < 20000} {incr ii 2} {
  179. append sql "DELETE FROM t4 WHERE rowid = $ii;"
  180. }
  181. speed_trial speed4-trigger3 10000 stmt $sql
  182. execsql {COMMIT}
  183. # The following block contains the same tests as the above block that
  184. # tests triggers, with one crucial difference: no triggers are defined.
  185. # So the difference in speed between these tests and the preceding ones
  186. # is the amount of time taken to compile and execute the trigger programs.
  187. #
  188. execsql {
  189. DROP TABLE t4;
  190. DROP TABLE log;
  191. VACUUM;
  192. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  193. BEGIN;
  194. }
  195. set sql ""
  196. for {set ii 1} {$ii < 10000} {incr ii} {
  197. append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
  198. }
  199. speed_trial speed4-notrigger1 10000 stmt $sql
  200. set sql ""
  201. for {set ii 1} {$ii < 20000} {incr ii 2} {
  202. set ii2 [expr {$ii*2}]
  203. append sql "
  204. UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
  205. "
  206. }
  207. speed_trial speed4-notrigger2 10000 stmt $sql
  208. set sql ""
  209. for {set ii 1} {$ii < 20000} {incr ii 2} {
  210. append sql "DELETE FROM t4 WHERE rowid = $ii;"
  211. }
  212. speed_trial speed4-notrigger3 10000 stmt $sql
  213. execsql {COMMIT}
  214. speed_trial_summary speed4
  215. finish_test