3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is measuring executing speed.
14 # This is a copy of speed1.test modified to user prepared statements.
16 # $Id: speed1p.test,v 1.7 2009/04/09 01:23:49 drh Exp $
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
27 sqlite3_memdebug_vfs_oom_test 0
29 # Set a uniform random seed
32 # The number_name procedure below converts its argment (an integer)
33 # into a string which is the English-language name for that number.
37 # puts [number_name 123] -> "one hundred twenty three"
39 set ones {zero one two three four five six seven eight nine
40 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
42 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
43 proc number_name {n} {
45 set txt "[number_name [expr {$n/1000}]] thousand"
46 set n [expr {$n%1000}]
51 append txt " [lindex $::ones [expr {$n/100}]] hundred"
55 append txt " [lindex $::tens [expr {$n/10}]]"
59 append txt " [lindex $::ones $n]"
61 set txt [string trim $txt]
62 if {$txt==""} {set txt zero}
66 # Create a database schema.
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);
79 SELECT name FROM sqlite_master ORDER BY 1;
83 # 50000 INSERTs on an unindexed table
86 for {set i 1} {$i<=50000} {incr i} {
87 set r [expr {int(rand()*500000)}]
88 set x [number_name $r]
92 foreach {i r x} $::list {
93 db eval {INSERT INTO t1 VALUES($i,$r,$x)}
97 speed_trial_tcl speed1p-insert1 50000 row $script
100 # 50000 INSERTs on an indexed table
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
109 foreach {i r x} $::list {
110 db eval {INSERT INTO t2 VALUES($i,$r,$x)}
114 speed_trial_tcl speed1p-insert2 50000 row $script
119 # 50 SELECTs on an integer comparison. There is no index so
120 # a full table scan is required.
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
129 foreach {lwr upr} $::list {
130 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
134 speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
137 # 50 SELECTs on an LIKE comparison. There is no index so a full
138 # table scan is required.
141 for {set i 0} {$i<50} {incr i} {
142 lappend list "%[number_name $i]%"
145 foreach pattern $::list {
146 db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
150 speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
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);
163 # 5000 SELECTs on an integer comparison where the integer is
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
173 foreach {lwr upr} $::list {
174 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
178 speed_trial_tcl speed1p-select3 5000 stmt $script
181 # 100000 random SELECTs against rowid.
184 for {set i 1} {$i<=100000} {incr i} {
185 set id [expr {int(rand()*50000)+1}]
190 db eval {SELECT c FROM t1 WHERE rowid=$id}
194 speed_trial_tcl speed1p-select4 100000 row $script
197 # 100000 random SELECTs against a unique indexed column.
200 for {set i 1} {$i<=100000} {incr i} {
201 set id [expr {int(rand()*50000)+1}]
206 db eval {SELECT c FROM t1 WHERE a=$id}
210 speed_trial_tcl speed1p-select5 100000 row $script
213 # 50000 random SELECTs against an indexed column text column
215 set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
218 db eval {SELECT c FROM t1 WHERE c=$c}
222 speed_trial_tcl speed1p-select6 50000 row $script
227 speed_trial speed1p-vacuum 100000 row VACUUM
229 # 5000 updates of ranges where the field being compared is indexed.
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
238 foreach {lwr upr} $::list {
239 db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
243 speed_trial_tcl speed1p-update1 5000 stmt $script
246 # 50000 single-row updates. An index is used to find the row quickly.
249 for {set i 0} {$i<50000} {incr i} {
250 set r [expr {int(rand()*500000)}]
254 foreach {i r} $::list {
255 db eval {UPDATE t1 SET b=$r WHERE a=$i}
259 speed_trial_tcl speed1p-update2 50000 row $script
262 # 1 big text update that touches every row in the table.
264 speed_trial speed1p-update3 50000 row {
268 # Many individual text updates. Each row in the table is
269 # touched through an index.
272 for {set i 1} {$i<=50000} {incr i} {
273 set r [expr {int(rand()*500000)}]
274 lappend list $i [number_name $r]
277 foreach {i x} $::list {
278 db eval {UPDATE t1 SET c=$x WHERE a=$i}
282 speed_trial_tcl speed1p-update4 50000 row $script
285 # Delete all content in a table.
287 speed_trial speed1p-delete1 50000 row {DELETE FROM t1}
289 # Copy one table into another
291 speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
293 # Delete all content in a table, one row at a time.
295 speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
297 # Refill the table yet again
299 speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
301 # Drop the table and recreate it without its indices.
304 speed_trial speed1p-drop1 50000 row {
306 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
310 # Refill the table yet again. This copy should be faster because
311 # there are no indices to deal with.
313 speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
315 # Select 20000 rows from the table at random.
317 speed_trial speed1p-random1 50000 row {
318 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
321 # Delete 20000 random rows from the table.
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)
327 do_test speed1p-1.1 {
328 db one {SELECT count(*) FROM t1}
331 # Delete 20000 more rows at random from the table.
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)
337 do_test speed1p-1.2 {
338 db one {SELECT count(*) FROM t1}
340 speed_trial_summary speed1
344 eval sqlite3_config_lookaside $old_lookaside
346 autoinstall_test_functions