1
0

speed2.test 8.4 KB

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