Fix documentation typo.
[sqlite.git] / test / speed1p.test
blobfc05067e3c1e9875c3e4f0c797002b8218b47046
1 # 2008 March 21
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 $
19 catch { db close }
20 sqlite3_shutdown
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
26 reset_db
27 speed_trial_init speed1
29 sqlite3_memdebug_vfs_oom_test 0
31 # Set a uniform random seed
32 expr srand(0)
34 # The number_name procedure below converts its argment (an integer)
35 # into a string which is the English-language name for that number.
37 # Example:
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
43           eighteen nineteen}
44 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
45 proc number_name {n} {
46   if {$n>=1000} {
47     set txt "[number_name [expr {$n/1000}]] thousand"
48     set n [expr {$n%1000}]
49   } else {
50     set txt {}
51   }
52   if {$n>=100} {
53     append txt " [lindex $::ones [expr {$n/100}]] hundred"
54     set n [expr {$n%100}]
55   }
56   if {$n>=20} {
57     append txt " [lindex $::tens [expr {$n/10}]]"
58     set n [expr {$n%10}]
59   }
60   if {$n>0} {
61     append txt " [lindex $::ones $n]"
62   }
63   set txt [string trim $txt]
64   if {$txt==""} {set txt zero}
65   return $txt
68 # Create a database schema.
70 do_test speed1p-1.0 {
71   execsql {
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);
79   }
80   execsql {
81     SELECT name FROM sqlite_master ORDER BY 1;
82   }
83 } {i2a i2b t1 t2}
85 # 50000 INSERTs on an unindexed table
87 set list {}
88 for {set i 1} {$i<=50000} {incr i} {
89   set r [expr {int(rand()*500000)}]
90   set x [number_name $r]
91   lappend list $i $r $x
93 set script {
94   foreach {i r x} $::list {
95     db eval {INSERT INTO t1 VALUES($i,$r,$x)}
96   }
98 db eval BEGIN
99 speed_trial_tcl speed1p-insert1 50000 row $script
100 db eval COMMIT
102 # 50000 INSERTs on an indexed table
104 set list {}
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
110 set script {
111   foreach {i r x} $::list {
112     db eval {INSERT INTO t2 VALUES($i,$r,$x)}
113   }
115 db eval BEGIN
116 speed_trial_tcl speed1p-insert2 50000 row $script
117 db eval COMMIT
121 # 50 SELECTs on an integer comparison.  There is no index so
122 # a full table scan is required.
124 set list {}
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
130 set script {
131   foreach {lwr upr} $::list {
132     db eval  {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
133   }
135 db eval BEGIN
136 speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
137 db eval COMMIT
139 # 50 SELECTs on an LIKE comparison.  There is no index so a full
140 # table scan is required.
142 set list {}
143 for {set i 0} {$i<50} {incr i} {
144   lappend list "%[number_name $i]%"
146 set script {
147   foreach pattern $::list {
148     db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
149   }
151 db eval BEGIN
152 speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
153 db eval COMMIT
155 # Create indices
157 db eval BEGIN
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);
163 db eval COMMIT
165 # 5000 SELECTs on an integer comparison where the integer is
166 # indexed.
168 set list {}
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
174 set script {
175   foreach {lwr upr} $::list {
176     db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
177   }
179 db eval BEGIN
180 speed_trial_tcl speed1p-select3 5000 stmt $script
181 db eval COMMIT
183 # 100000 random SELECTs against rowid.
185 set list {}
186 for {set i 1} {$i<=100000} {incr i} {
187   set id [expr {int(rand()*50000)+1}]
188   lappend list $id
190 set script {
191   foreach id $::list {
192     db eval {SELECT c FROM t1 WHERE rowid=$id}
193   }
195 db eval BEGIN
196 speed_trial_tcl speed1p-select4 100000 row $script
197 db eval COMMIT
199 # 100000 random SELECTs against a unique indexed column.
201 set list {}
202 for {set i 1} {$i<=100000} {incr i} {
203   set id [expr {int(rand()*50000)+1}]
204   lappend list $id
206 set script {
207   foreach id $::list {
208     db eval {SELECT c FROM t1 WHERE a=$id}
209   }
211 db eval BEGIN
212 speed_trial_tcl speed1p-select5 100000 row $script
213 db eval COMMIT
215 # 50000 random SELECTs against an indexed column text column
217 set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
218 set script {
219   foreach c $::list {
220     db eval {SELECT c FROM t1 WHERE c=$c}
221   }
223 db eval BEGIN
224 speed_trial_tcl speed1p-select6 50000 row $script
225 db eval COMMIT
228 # Vacuum
229 speed_trial speed1p-vacuum 100000 row VACUUM
231 # 5000 updates of ranges where the field being compared is indexed.
233 set list {}
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
239 set script {
240   foreach {lwr upr} $::list {
241     db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
242   }
244 db eval BEGIN
245 speed_trial_tcl speed1p-update1 5000 stmt $script
246 db eval COMMIT
248 # 50000 single-row updates.  An index is used to find the row quickly.
250 set list {}
251 for {set i 0} {$i<50000} {incr i} {
252   set r [expr {int(rand()*500000)}]
253   lappend list $i $r
255 set script {
256   foreach {i r} $::list {
257     db eval {UPDATE t1 SET b=$r WHERE a=$i}
258   }
260 db eval BEGIN
261 speed_trial_tcl speed1p-update2 50000 row $script
262 db eval COMMIT
264 # 1 big text update that touches every row in the table.
266 speed_trial speed1p-update3 50000 row {
267   UPDATE t1 SET c=a;
270 # Many individual text updates.  Each row in the table is
271 # touched through an index.
273 set list {}
274 for {set i 1} {$i<=50000} {incr i} {
275   set r [expr {int(rand()*500000)}]
276   lappend list $i [number_name $r]
278 set script {
279   foreach {i x} $::list {
280     db eval {UPDATE t1 SET c=$x WHERE a=$i}
281   }
283 db eval BEGIN
284 speed_trial_tcl speed1p-update4 50000 row $script
285 db eval COMMIT
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.
305 db eval BEGIN
306 speed_trial speed1p-drop1 50000 row {
307    DROP TABLE t1;
308    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
310 db eval COMMIT
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}
331 } 30000
332     
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}
341 } 10000
342 speed_trial_summary speed1
344 db close
345 sqlite3_shutdown
346 eval sqlite3_config_lookaside $old_lookaside
347 sqlite3_initialize
348 autoinstall_test_functions
349 finish_test