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 $
21 #sqlite3_config_scratch 29000 1
22 set old_lookaside [sqlite3_config_lookaside 2048 300]
23 #sqlite3_config_pagecache 1024 11000
24 set testdir [file dirname $argv0]
25 source $testdir/tester.tcl
27 speed_trial_init speed1
29 sqlite3_memdebug_vfs_oom_test 0
31 # Set a uniform random seed
34 # The number_name procedure below converts its argment (an integer)
35 # into a string which is the English-language name for that number.
39 # puts [number_name 123] -> "one hundred twenty three"
41 set ones {zero one two three four five six seven eight nine
42 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
44 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
45 proc number_name {n} {
47 set txt "[number_name [expr {$n/1000}]] thousand"
48 set n [expr {$n%1000}]
53 append txt " [lindex $::ones [expr {$n/100}]] hundred"
57 append txt " [lindex $::tens [expr {$n/10}]]"
61 append txt " [lindex $::ones $n]"
63 set txt [string trim $txt]
64 if {$txt==""} {set txt zero}
68 # Create a database schema.
72 PRAGMA page_size=1024;
73 PRAGMA cache_size=500;
74 PRAGMA locking_mode=EXCLUSIVE;
75 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
76 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
77 CREATE INDEX i2a ON t2(a);
78 CREATE INDEX i2b ON t2(b);
81 SELECT name FROM sqlite_master ORDER BY 1;
85 # 50000 INSERTs on an unindexed table
88 for {set i 1} {$i<=50000} {incr i} {
89 set r [expr {int(rand()*500000)}]
90 set x [number_name $r]
94 foreach {i r x} $::list {
95 db eval {INSERT INTO t1 VALUES($i,$r,$x)}
99 speed_trial_tcl speed1p-insert1 50000 row $script
102 # 50000 INSERTs on an indexed table
105 for {set i 1} {$i<=50000} {incr i} {
106 set r [expr {int(rand()*500000)}]
107 set x [number_name $r]
108 lappend list $i $r $x
111 foreach {i r x} $::list {
112 db eval {INSERT INTO t2 VALUES($i,$r,$x)}
116 speed_trial_tcl speed1p-insert2 50000 row $script
121 # 50 SELECTs on an integer comparison. There is no index so
122 # a full table scan is required.
125 for {set i 0} {$i<50} {incr i} {
126 set lwr [expr {$i*100}]
127 set upr [expr {($i+10)*100}]
128 lappend list $lwr $upr
131 foreach {lwr upr} $::list {
132 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
136 speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
139 # 50 SELECTs on an LIKE comparison. There is no index so a full
140 # table scan is required.
143 for {set i 0} {$i<50} {incr i} {
144 lappend list "%[number_name $i]%"
147 foreach pattern $::list {
148 db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
152 speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
158 speed_trial speed1p-createidx 150000 row {
159 CREATE INDEX i1a ON t1(a);
160 CREATE INDEX i1b ON t1(b);
161 CREATE INDEX i1c ON t1(c);
165 # 5000 SELECTs on an integer comparison where the integer is
169 for {set i 0} {$i<5000} {incr i} {
170 set lwr [expr {$i*100}]
171 set upr [expr {($i+10)*100}]
172 lappend list $lwr $upr
175 foreach {lwr upr} $::list {
176 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
180 speed_trial_tcl speed1p-select3 5000 stmt $script
183 # 100000 random SELECTs against rowid.
186 for {set i 1} {$i<=100000} {incr i} {
187 set id [expr {int(rand()*50000)+1}]
192 db eval {SELECT c FROM t1 WHERE rowid=$id}
196 speed_trial_tcl speed1p-select4 100000 row $script
199 # 100000 random SELECTs against a unique indexed column.
202 for {set i 1} {$i<=100000} {incr i} {
203 set id [expr {int(rand()*50000)+1}]
208 db eval {SELECT c FROM t1 WHERE a=$id}
212 speed_trial_tcl speed1p-select5 100000 row $script
215 # 50000 random SELECTs against an indexed column text column
217 set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
220 db eval {SELECT c FROM t1 WHERE c=$c}
224 speed_trial_tcl speed1p-select6 50000 row $script
229 speed_trial speed1p-vacuum 100000 row VACUUM
231 # 5000 updates of ranges where the field being compared is indexed.
234 for {set i 0} {$i<5000} {incr i} {
235 set lwr [expr {$i*2}]
236 set upr [expr {($i+1)*2}]
237 lappend list $lwr $upr
240 foreach {lwr upr} $::list {
241 db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
245 speed_trial_tcl speed1p-update1 5000 stmt $script
248 # 50000 single-row updates. An index is used to find the row quickly.
251 for {set i 0} {$i<50000} {incr i} {
252 set r [expr {int(rand()*500000)}]
256 foreach {i r} $::list {
257 db eval {UPDATE t1 SET b=$r WHERE a=$i}
261 speed_trial_tcl speed1p-update2 50000 row $script
264 # 1 big text update that touches every row in the table.
266 speed_trial speed1p-update3 50000 row {
270 # Many individual text updates. Each row in the table is
271 # touched through an index.
274 for {set i 1} {$i<=50000} {incr i} {
275 set r [expr {int(rand()*500000)}]
276 lappend list $i [number_name $r]
279 foreach {i x} $::list {
280 db eval {UPDATE t1 SET c=$x WHERE a=$i}
284 speed_trial_tcl speed1p-update4 50000 row $script
287 # Delete all content in a table.
289 speed_trial speed1p-delete1 50000 row {DELETE FROM t1}
291 # Copy one table into another
293 speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
295 # Delete all content in a table, one row at a time.
297 speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
299 # Refill the table yet again
301 speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
303 # Drop the table and recreate it without its indices.
306 speed_trial speed1p-drop1 50000 row {
308 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
312 # Refill the table yet again. This copy should be faster because
313 # there are no indices to deal with.
315 speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
317 # Select 20000 rows from the table at random.
319 speed_trial speed1p-random1 50000 row {
320 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
323 # Delete 20000 random rows from the table.
325 speed_trial speed1p-random-del1 20000 row {
326 DELETE FROM t1 WHERE rowid IN
327 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
329 do_test speed1p-1.1 {
330 db one {SELECT count(*) FROM t1}
333 # Delete 20000 more rows at random from the table.
335 speed_trial speed1p-random-del2 20000 row {
336 DELETE FROM t1 WHERE rowid IN
337 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
339 do_test speed1p-1.2 {
340 db one {SELECT count(*) FROM t1}
342 speed_trial_summary speed1
346 eval sqlite3_config_lookaside $old_lookaside
348 autoinstall_test_functions