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 19 sqlite3_shutdown 20 #sqlite3_config_scratch 29000 1 21 set old_lookaside [sqlite3_config_lookaside 2048 300] 22 #sqlite3_config_pagecache 1024 11000 23 set testdir [file dirname $argv0] 24 source $testdir/tester.tcl 25 speed_trial_init speed1 26 27 # Set a uniform random seed 28 expr srand(0) 29 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 64 # Create a database schema. 65 # 66 do_test speed1p-1.0 { 67 execsql { 68 PRAGMA page_size=1024; 69 PRAGMA cache_size=500; 70 PRAGMA locking_mode=EXCLUSIVE; 71 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT); 72 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT); 73 CREATE INDEX i2a ON t2(a); 74 CREATE INDEX i2b ON t2(b); 75 } 76 execsql { 77 SELECT name FROM sqlite_master ORDER BY 1; 78 } 79 } {i2a i2b t1 t2} 80 81 82 # 50000 INSERTs on an unindexed table 83 # 84 set list {} 85 for {set i 1} {$i<=50000} {incr i} { 86 set r [expr {int(rand()*500000)}] 87 set x [number_name $r] 88 lappend list $i $r $x 89 } 90 set script { 91 foreach {i r x} $::list { 92 db eval {INSERT INTO t1 VALUES($i,$r,$x)} 93 } 94 } 95 db eval BEGIN 96 speed_trial_tcl speed1p-insert1 50000 row $script 97 db eval COMMIT 98 99 # 50000 INSERTs on an indexed table 100 # 101 set list {} 102 for {set i 1} {$i<=50000} {incr i} { 103 set r [expr {int(rand()*500000)}] 104 set x [number_name $r] 105 lappend list $i $r $x 106 } 107 set script { 108 foreach {i r x} $::list { 109 db eval {INSERT INTO t2 VALUES($i,$r,$x)} 110 } 111 } 112 db eval BEGIN 113 speed_trial_tcl speed1p-insert2 50000 row $script 114 db eval COMMIT 115 116 117 118 # 50 SELECTs on an integer comparison. There is no index so 119 # a full table scan is required. 120 # 121 set list {} 122 for {set i 0} {$i<50} {incr i} { 123 set lwr [expr {$i*100}] 124 set upr [expr {($i+10)*100}] 125 lappend list $lwr $upr 126 } 127 set script { 128 foreach {lwr upr} $::list { 129 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr} 130 } 131 } 132 db eval BEGIN 133 speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script 134 db eval COMMIT 135 136 # 50 SELECTs on an LIKE comparison. There is no index so a full 137 # table scan is required. 138 # 139 set list {} 140 for {set i 0} {$i<50} {incr i} { 141 lappend list "%[number_name $i]%" 142 } 143 set script { 144 foreach pattern $::list { 145 db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern} 146 } 147 } 148 db eval BEGIN 149 speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script 150 db eval COMMIT 151 152 # Create indices 153 # 154 db eval BEGIN 155 speed_trial speed1p-createidx 150000 row { 156 CREATE INDEX i1a ON t1(a); 157 CREATE INDEX i1b ON t1(b); 158 CREATE INDEX i1c ON t1(c); 159 } 160 db eval COMMIT 161 162 # 5000 SELECTs on an integer comparison where the integer is 163 # indexed. 164 # 165 set list {} 166 for {set i 0} {$i<5000} {incr i} { 167 set lwr [expr {$i*100}] 168 set upr [expr {($i+10)*100}] 169 lappend list $lwr $upr 170 } 171 set script { 172 foreach {lwr upr} $::list { 173 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr} 174 } 175 } 176 db eval BEGIN 177 speed_trial_tcl speed1p-select3 5000 stmt $script 178 db eval COMMIT 179 180 # 100000 random SELECTs against rowid. 181 # 182 set list {} 183 for {set i 1} {$i<=100000} {incr i} { 184 set id [expr {int(rand()*50000)+1}] 185 lappend list $id 186 } 187 set script { 188 foreach id $::list { 189 db eval {SELECT c FROM t1 WHERE rowid=$id} 190 } 191 } 192 db eval BEGIN 193 speed_trial_tcl speed1p-select4 100000 row $script 194 db eval COMMIT 195 196 # 100000 random SELECTs against a unique indexed column. 197 # 198 set list {} 199 for {set i 1} {$i<=100000} {incr i} { 200 set id [expr {int(rand()*50000)+1}] 201 lappend list $id 202 } 203 set script { 204 foreach id $::list { 205 db eval {SELECT c FROM t1 WHERE a=$id} 206 } 207 } 208 db eval BEGIN 209 speed_trial_tcl speed1p-select5 100000 row $script 210 db eval COMMIT 211 212 # 50000 random SELECTs against an indexed column text column 213 # 214 set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}] 215 set script { 216 foreach c $::list { 217 db eval {SELECT c FROM t1 WHERE c=$c} 218 } 219 } 220 db eval BEGIN 221 speed_trial_tcl speed1p-select6 50000 row $script 222 db eval COMMIT 223 224 225 # Vacuum 226 speed_trial speed1p-vacuum 100000 row VACUUM 227 228 # 5000 updates of ranges where the field being compared is indexed. 229 # 230 set list {} 231 for {set i 0} {$i<5000} {incr i} { 232 set lwr [expr {$i*2}] 233 set upr [expr {($i+1)*2}] 234 lappend list $lwr $upr 235 } 236 set script { 237 foreach {lwr upr} $::list { 238 db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr} 239 } 240 } 241 db eval BEGIN 242 speed_trial_tcl speed1p-update1 5000 stmt $script 243 db eval COMMIT 244 245 # 50000 single-row updates. An index is used to find the row quickly. 246 # 247 set list {} 248 for {set i 0} {$i<50000} {incr i} { 249 set r [expr {int(rand()*500000)}] 250 lappend list $i $r 251 } 252 set script { 253 foreach {i r} $::list { 254 db eval {UPDATE t1 SET b=$r WHERE a=$i} 255 } 256 } 257 db eval BEGIN 258 speed_trial_tcl speed1p-update2 50000 row $script 259 db eval COMMIT 260 261 # 1 big text update that touches every row in the table. 262 # 263 speed_trial speed1p-update3 50000 row { 264 UPDATE t1 SET c=a; 265 } 266 267 # Many individual text updates. Each row in the table is 268 # touched through an index. 269 # 270 set list {} 271 for {set i 1} {$i<=50000} {incr i} { 272 set r [expr {int(rand()*500000)}] 273 lappend list $i [number_name $r] 274 } 275 set script { 276 foreach {i x} $::list { 277 db eval {UPDATE t1 SET c=$x WHERE a=$i} 278 } 279 } 280 db eval BEGIN 281 speed_trial_tcl speed1p-update4 50000 row $script 282 db eval COMMIT 283 284 # Delete all content in a table. 285 # 286 speed_trial speed1p-delete1 50000 row {DELETE FROM t1} 287 288 # Copy one table into another 289 # 290 speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2} 291 292 # Delete all content in a table, one row at a time. 293 # 294 speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1} 295 296 # Refill the table yet again 297 # 298 speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2} 299 300 # Drop the table and recreate it without its indices. 301 # 302 db eval BEGIN 303 speed_trial speed1p-drop1 50000 row { 304 DROP TABLE t1; 305 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT); 306 } 307 db eval COMMIT 308 309 # Refill the table yet again. This copy should be faster because 310 # there are no indices to deal with. 311 # 312 speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2} 313 314 # Select 20000 rows from the table at random. 315 # 316 speed_trial speed1p-random1 50000 row { 317 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000 318 } 319 320 # Delete 20000 random rows from the table. 321 # 322 speed_trial speed1p-random-del1 20000 row { 323 DELETE FROM t1 WHERE rowid IN 324 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000) 325 } 326 do_test speed1p-1.1 { 327 db one {SELECT count(*) FROM t1} 328 } 30000 329 330 # Delete 20000 more rows at random from the table. 331 # 332 speed_trial speed1p-random-del2 20000 row { 333 DELETE FROM t1 WHERE rowid IN 334 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000) 335 } 336 do_test speed1p-1.2 { 337 db one {SELECT count(*) FROM t1} 338 } 10000 339 speed_trial_summary speed1 340 341 db close 342 sqlite3_shutdown 343 eval sqlite3_config_lookaside $old_lookaside 344 sqlite3_initialize 345 autoinstall_test_functions 346 finish_test 347