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