speed1.test 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. # 2006 November 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.
  13. #
  14. # $Id: speed1.test,v 1.11 2009/04/09 01:23:49 drh Exp $
  15. #
  16. sqlite3_shutdown
  17. #sqlite3_config_scratch 29000 1
  18. set old_lookaside [sqlite3_config_lookaside 1000 300]
  19. #sqlite3_config_pagecache 1024 10000
  20. set testdir [file dirname $argv0]
  21. source $testdir/tester.tcl
  22. speed_trial_init speed1
  23. # Set a uniform random seed
  24. expr srand(0)
  25. set sqlout [open speed1.txt w]
  26. proc tracesql {sql} {
  27. puts $::sqlout $sql\;
  28. }
  29. #db trace tracesql
  30. # The number_name procedure below converts its argment (an integer)
  31. # into a string which is the English-language name for that number.
  32. #
  33. # Example:
  34. #
  35. # puts [number_name 123] -> "one hundred twenty three"
  36. #
  37. set ones {zero one two three four five six seven eight nine
  38. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  39. eighteen nineteen}
  40. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  41. proc number_name {n} {
  42. if {$n>=1000} {
  43. set txt "[number_name [expr {$n/1000}]] thousand"
  44. set n [expr {$n%1000}]
  45. } else {
  46. set txt {}
  47. }
  48. if {$n>=100} {
  49. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  50. set n [expr {$n%100}]
  51. }
  52. if {$n>=20} {
  53. append txt " [lindex $::tens [expr {$n/10}]]"
  54. set n [expr {$n%10}]
  55. }
  56. if {$n>0} {
  57. append txt " [lindex $::ones $n]"
  58. }
  59. set txt [string trim $txt]
  60. if {$txt==""} {set txt zero}
  61. return $txt
  62. }
  63. # Create a database schema.
  64. #
  65. do_test speed1-1.0 {
  66. execsql {
  67. PRAGMA page_size=1024;
  68. PRAGMA cache_size=8192;
  69. PRAGMA locking_mode=EXCLUSIVE;
  70. CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
  71. CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
  72. CREATE INDEX i2a ON t2(a);
  73. CREATE INDEX i2b ON t2(b);
  74. }
  75. execsql {
  76. SELECT name FROM sqlite_master ORDER BY 1;
  77. }
  78. } {i2a i2b t1 t2}
  79. # 50000 INSERTs on an unindexed table
  80. #
  81. set sql {}
  82. for {set i 1} {$i<=50000} {incr i} {
  83. set r [expr {int(rand()*500000)}]
  84. append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
  85. }
  86. db eval BEGIN
  87. speed_trial speed1-insert1 50000 row $sql
  88. db eval COMMIT
  89. # 50000 INSERTs on an indexed table
  90. #
  91. set sql {}
  92. for {set i 1} {$i<=50000} {incr i} {
  93. set r [expr {int(rand()*500000)}]
  94. append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
  95. }
  96. db eval BEGIN
  97. speed_trial speed1-insert2 50000 row $sql
  98. db eval COMMIT
  99. # 50 SELECTs on an integer comparison. There is no index so
  100. # a full table scan is required.
  101. #
  102. set sql {}
  103. for {set i 0} {$i<50} {incr i} {
  104. set lwr [expr {$i*100}]
  105. set upr [expr {($i+10)*100}]
  106. append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
  107. }
  108. db eval BEGIN
  109. speed_trial speed1-select1 [expr {50*50000}] row $sql
  110. db eval COMMIT
  111. # 50 SELECTs on an LIKE comparison. There is no index so a full
  112. # table scan is required.
  113. #
  114. set sql {}
  115. for {set i 0} {$i<50} {incr i} {
  116. append sql \
  117. "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
  118. }
  119. db eval BEGIN
  120. speed_trial speed1-select2 [expr {50*50000}] row $sql
  121. db eval COMMIT
  122. # Create indices
  123. #
  124. db eval BEGIN
  125. speed_trial speed1-createidx 150000 row {
  126. CREATE INDEX i1a ON t1(a);
  127. CREATE INDEX i1b ON t1(b);
  128. CREATE INDEX i1c ON t1(c);
  129. }
  130. db eval COMMIT
  131. # 5000 SELECTs on an integer comparison where the integer is
  132. # indexed.
  133. #
  134. set sql {}
  135. for {set i 0} {$i<5000} {incr i} {
  136. set lwr [expr {$i*100}]
  137. set upr [expr {($i+10)*100}]
  138. append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
  139. }
  140. db eval BEGIN
  141. speed_trial speed1-select3 5000 stmt $sql
  142. db eval COMMIT
  143. # 100000 random SELECTs against rowid.
  144. #
  145. set sql {}
  146. for {set i 1} {$i<=100000} {incr i} {
  147. set id [expr {int(rand()*50000)+1}]
  148. append sql "SELECT c FROM t1 WHERE rowid=$id;"
  149. }
  150. db eval BEGIN
  151. speed_trial speed1-select4 100000 row $sql
  152. db eval COMMIT
  153. # 100000 random SELECTs against a unique indexed column.
  154. #
  155. set sql {}
  156. for {set i 1} {$i<=100000} {incr i} {
  157. set id [expr {int(rand()*50000)+1}]
  158. append sql "SELECT c FROM t1 WHERE a=$id;"
  159. }
  160. db eval BEGIN
  161. speed_trial speed1-select5 100000 row $sql
  162. db eval COMMIT
  163. # 50000 random SELECTs against an indexed column text column
  164. #
  165. set sql {}
  166. db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
  167. append sql "SELECT c FROM t1 WHERE c='$c';"
  168. }
  169. db eval BEGIN
  170. speed_trial speed1-select6 50000 row $sql
  171. db eval COMMIT
  172. # Vacuum
  173. speed_trial speed1-vacuum 100000 row VACUUM
  174. # 5000 updates of ranges where the field being compared is indexed.
  175. #
  176. set sql {}
  177. for {set i 0} {$i<5000} {incr i} {
  178. set lwr [expr {$i*2}]
  179. set upr [expr {($i+1)*2}]
  180. append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
  181. }
  182. db eval BEGIN
  183. speed_trial speed1-update1 5000 stmt $sql
  184. db eval COMMIT
  185. # 50000 single-row updates. An index is used to find the row quickly.
  186. #
  187. set sql {}
  188. for {set i 0} {$i<50000} {incr i} {
  189. set r [expr {int(rand()*500000)}]
  190. append sql "UPDATE t1 SET b=$r WHERE a=$i;"
  191. }
  192. db eval BEGIN
  193. speed_trial speed1-update2 50000 row $sql
  194. db eval COMMIT
  195. # 1 big text update that touches every row in the table.
  196. #
  197. speed_trial speed1-update3 50000 row {
  198. UPDATE t1 SET c=a;
  199. }
  200. # Many individual text updates. Each row in the table is
  201. # touched through an index.
  202. #
  203. set sql {}
  204. for {set i 1} {$i<=50000} {incr i} {
  205. set r [expr {int(rand()*500000)}]
  206. append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
  207. }
  208. db eval BEGIN
  209. speed_trial speed1-update4 50000 row $sql
  210. db eval COMMIT
  211. # Delete all content in a table.
  212. #
  213. speed_trial speed1-delete1 50000 row {DELETE FROM t1}
  214. # Copy one table into another
  215. #
  216. speed_trial speed1-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
  217. # Delete all content in a table, one row at a time.
  218. #
  219. speed_trial speed1-delete2 50000 row {DELETE FROM t1 WHERE 1}
  220. # Refill the table yet again
  221. #
  222. speed_trial speed1-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
  223. # Drop the table and recreate it without its indices.
  224. #
  225. db eval BEGIN
  226. speed_trial speed1-drop1 50000 row {
  227. DROP TABLE t1;
  228. CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
  229. }
  230. db eval COMMIT
  231. # Refill the table yet again. This copy should be faster because
  232. # there are no indices to deal with.
  233. #
  234. speed_trial speed1-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
  235. # Select 20000 rows from the table at random.
  236. #
  237. speed_trial speed1-random1 50000 row {
  238. SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
  239. }
  240. # Delete 20000 random rows from the table.
  241. #
  242. speed_trial speed1-random-del1 20000 row {
  243. DELETE FROM t1 WHERE rowid IN
  244. (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
  245. }
  246. do_test speed1-1.1 {
  247. db one {SELECT count(*) FROM t1}
  248. } 30000
  249. # Delete 20000 more rows at random from the table.
  250. #
  251. speed_trial speed1-random-del2 20000 row {
  252. DELETE FROM t1 WHERE rowid IN
  253. (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
  254. }
  255. do_test speed1-1.2 {
  256. db one {SELECT count(*) FROM t1}
  257. } 10000
  258. speed_trial_summary speed1
  259. db close
  260. sqlite3_shutdown
  261. eval sqlite3_config_lookaside $old_lookaside
  262. sqlite3_initialize
  263. autoinstall_test_functions
  264. finish_test