speed4p.test 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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: speed4p.test,v 1.4 2008/04/10 13:32:37 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. # speed4p-join1: Join three tables using IPK index.
  68. # speed4p-join2: Join three tables using an index.
  69. # speed4p-join3: Join two tables without an index.
  70. #
  71. # speed4p-view1: Querying a view.
  72. # speed4p-table1: Same queries as in speed4p-view1, but run directly against
  73. # the tables for comparison purposes.
  74. #
  75. # speed4p-subselect1: A SELECT statement that uses many sub-queries..
  76. #
  77. # speed4p-trigger1: An INSERT statement that fires a trigger.
  78. # speed4p-trigger2: An UPDATE statement that fires a trigger.
  79. # speed4p-trigger3: A DELETE statement that fires a trigger.
  80. # speed4p-notrigger1: Same operation as trigger1, but without the trigger.
  81. # speed4p-notrigger2: " trigger2 "
  82. # speed4p-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. PRAGMA page_size=1024;
  88. PRAGMA cache_size=8192;
  89. PRAGMA locking_mode=EXCLUSIVE;
  90. BEGIN;
  91. CREATE TABLE t1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  92. CREATE TABLE t2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  93. CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  94. CREATE VIEW v1 AS SELECT rowid, i, t FROM t1;
  95. CREATE VIEW v2 AS SELECT rowid, i, t FROM t2;
  96. CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;
  97. }
  98. for {set jj 1} {$jj <= 3} {incr jj} {
  99. set stmt [string map "%T% t$jj" {INSERT INTO %T% VALUES(NULL, $i, $t)}]
  100. for {set ii 0} {$ii < 50000} {incr ii} {
  101. set i [expr {int(rand()*50000)}]
  102. set t [number_name $i]
  103. execsql $stmt
  104. }
  105. }
  106. execsql {
  107. CREATE INDEX i1 ON t1(t);
  108. CREATE INDEX i2 ON t2(t);
  109. CREATE INDEX i3 ON t3(t);
  110. COMMIT;
  111. }
  112. # Join t1, t2, t3 on IPK.
  113. set sql "SELECT * FROM t1, t2, t3 WHERE t1.oid = t2.oid AND t2.oid = t3.oid"
  114. speed_trial speed4p-join1 50000 row $sql
  115. # Join t1, t2, t3 on the non-IPK index.
  116. set sql "SELECT * FROM t1, t2, t3 WHERE t1.t = t2.t AND t2.t = t3.t"
  117. speed_trial speed4p-join2 50000 row $sql
  118. # Run 10000 simple queries against the views.
  119. set script {
  120. for {set ii 1} {$ii < 10000} {incr ii} {
  121. set v [expr {$ii*3}]
  122. set t [expr {$ii%3+1}]
  123. db eval "SELECT * FROM v$t WHERE rowid = \$v"
  124. }
  125. }
  126. speed_trial_tcl speed4p-view1 10000 stmt $script
  127. # Run the same 10000 simple queries as in the previous test case against
  128. # the underlying tables. The compiled vdbe programs should be identical, so
  129. # the only difference in running time is the extra time taken to compile
  130. # the view definitions.
  131. #
  132. set script {
  133. for {set ii 1} {$ii < 10000} {incr ii} {
  134. set v [expr {$ii*3}]
  135. set t [expr {$ii%3+1}]
  136. db eval "SELECT t FROM t$t WHERE rowid = \$v"
  137. }
  138. }
  139. speed_trial_tcl speed4p-table1 10000 stmt $script
  140. # Run a SELECT that uses sub-queries 10000 times. A total of 30000 sub-selects.
  141. #
  142. set script {
  143. for {set ii 1} {$ii < 10000} {incr ii} {
  144. set v [expr {$ii*3}]
  145. db eval {
  146. SELECT (SELECT t FROM t1 WHERE rowid = $v),
  147. (SELECT t FROM t2 WHERE rowid = $v),
  148. (SELECT t FROM t3 WHERE rowid = $v)
  149. }
  150. }
  151. }
  152. speed_trial_tcl speed4p-subselect1 10000 stmt $script
  153. # Single-row updates performance.
  154. #
  155. set script {
  156. db eval BEGIN
  157. for {set ii 1} {$ii < 10000} {incr ii} {
  158. set v [expr {$ii*3}]
  159. db eval {UPDATE t1 SET i=i+1 WHERE rowid=$ii}
  160. }
  161. db eval COMMIT
  162. }
  163. speed_trial_tcl speed4p-rowid-update 10000 stmt $script
  164. db eval {
  165. CREATE TABLE t5(t TEXT PRIMARY KEY, i INTEGER);
  166. }
  167. speed_trial speed4p-insert-ignore 50000 row {
  168. INSERT OR IGNORE INTO t5 SELECT t, i FROM t1;
  169. }
  170. set list [db eval {SELECT t FROM t5}]
  171. set script {
  172. db eval BEGIN
  173. foreach t $::list {
  174. db eval {UPDATE t5 SET i=i+1 WHERE t=$t}
  175. }
  176. db eval COMMIT
  177. }
  178. speed_trial_tcl speed4p-unique-update [llength $list] stmt $script
  179. # The following block tests the speed of some DML statements that cause
  180. # triggers to fire.
  181. #
  182. execsql {
  183. CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);
  184. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  185. CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN
  186. INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);
  187. END;
  188. CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN
  189. INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);
  190. END;
  191. CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN
  192. INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);
  193. END;
  194. BEGIN;
  195. }
  196. set list {}
  197. for {set ii 1} {$ii < 10000} {incr ii} {
  198. lappend list $ii [number_name $ii]
  199. }
  200. set script {
  201. foreach {ii name} $::list {
  202. db eval {INSERT INTO t4 VALUES(NULL, $ii, $name)}
  203. }
  204. }
  205. speed_trial_tcl speed4p-trigger1 10000 stmt $script
  206. set list {}
  207. for {set ii 1} {$ii < 20000} {incr ii 2} {
  208. set ii2 [expr {$ii*2}]
  209. lappend list $ii $ii2 [number_name $ii2]
  210. }
  211. set script {
  212. foreach {ii ii2 name} $::list {
  213. db eval {
  214. UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii;
  215. }
  216. }
  217. }
  218. speed_trial_tcl speed4p-trigger2 10000 stmt $script
  219. set script {
  220. for {set ii 1} {$ii < 20000} {incr ii 2} {
  221. db eval {DELETE FROM t4 WHERE rowid = $ii}
  222. }
  223. }
  224. speed_trial_tcl speed4p-trigger3 10000 stmt $script
  225. execsql {COMMIT}
  226. # The following block contains the same tests as the above block that
  227. # tests triggers, with one crucial difference: no triggers are defined.
  228. # So the difference in speed between these tests and the preceding ones
  229. # is the amount of time taken to compile and execute the trigger programs.
  230. #
  231. execsql {
  232. DROP TABLE t4;
  233. DROP TABLE log;
  234. VACUUM;
  235. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  236. BEGIN;
  237. }
  238. set list {}
  239. for {set ii 1} {$ii < 10000} {incr ii} {
  240. lappend list $ii [number_name $ii]
  241. }
  242. set script {
  243. foreach {ii name} $::list {
  244. db eval {INSERT INTO t4 VALUES(NULL, $ii, $name);}
  245. }
  246. }
  247. speed_trial_tcl speed4p-notrigger1 10000 stmt $script
  248. set list {}
  249. for {set ii 1} {$ii < 20000} {incr ii 2} {
  250. set ii2 [expr {$ii*2}]
  251. lappend list $ii $ii2 [number_name $ii2]
  252. }
  253. set script {
  254. foreach {ii ii2 name} $::list {
  255. db eval {
  256. UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii;
  257. }
  258. }
  259. }
  260. speed_trial_tcl speed4p-notrigger2 10000 stmt $script
  261. set script {
  262. for {set ii 1} {$ii < 20000} {incr ii 2} {
  263. db eval {DELETE FROM t4 WHERE rowid = $ii}
  264. }
  265. }
  266. speed_trial_tcl speed4p-notrigger3 10000 stmt $script
  267. execsql {COMMIT}
  268. speed_trial_summary speed4
  269. finish_test