speed1p.test 8.0 KB

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