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. More specifically,
13 # the focus is on the speed of:
20 # $Id: speed4.test,v 1.2 2008/07/12 14:52:20 drh Exp $
23 set testdir [file dirname $argv0]
24 source $testdir/tester.tcl
25 speed_trial_init speed1
27 # Set a uniform random seed
30 set sqlout [open speed1.txt w]
36 # The number_name procedure below converts its argment (an integer)
37 # into a string which is the English-language name for that number.
41 # puts [number_name 123] -> "one hundred twenty three"
43 set ones {zero one two three four five six seven eight nine
44 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
46 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
47 proc number_name {n} {
49 set txt "[number_name [expr {$n/1000}]] thousand"
50 set n [expr {$n%1000}]
55 append txt " [lindex $::ones [expr {$n/100}]] hundred"
59 append txt " [lindex $::tens [expr {$n/10}]]"
63 append txt " [lindex $::ones $n]"
65 set txt [string trim $txt]
66 if {$txt==""} {set txt zero}
72 # speed4-join1: Join three tables using IPK index.
73 # speed4-join2: Join three tables using an index.
74 # speed4-join3: Join two tables without an index.
76 # speed4-view1: Querying a view.
77 # speed4-table1: Same queries as in speed4-view1, but run directly against
78 # the tables for comparison purposes.
80 # speed4-subselect1: A SELECT statement that uses many sub-queries..
82 # speed4-trigger1: An INSERT statement that fires a trigger.
83 # speed4-trigger2: An UPDATE statement that fires a trigger.
84 # speed4-trigger3: A DELETE statement that fires a trigger.
85 # speed4-notrigger1: Same operation as trigger1, but without the trigger.
86 # speed4-notrigger2: " trigger2 "
87 # speed4-notrigger3: " trigger3 "
90 # Set up the schema. Each of the tables t1, t2 and t3 contain 50,000 rows.
91 # This creates a database of around 16MB.
94 CREATE TABLE t1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
95 CREATE TABLE t2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
96 CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
98 CREATE VIEW v1 AS SELECT rowid, i, t FROM t1;
99 CREATE VIEW v2 AS SELECT rowid, i, t FROM t2;
100 CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;
102 for {set jj 1} {$jj <= 3} {incr jj} {
103 set stmt [string map "%T% t$jj" {INSERT INTO %T% VALUES(NULL, $i, $t)}]
104 for {set ii 0} {$ii < 50000} {incr ii} {
105 set i [expr {int(rand()*50000)}]
106 set t [number_name $i]
111 CREATE INDEX i1 ON t1(t);
112 CREATE INDEX i2 ON t2(t);
113 CREATE INDEX i3 ON t3(t);
117 # Before running these tests, disable the compiled statement cache built into
118 # the Tcl interface. This is because we want to test the speed of SQL
119 # compilation as well as execution.
123 # Join t1, t2, t3 on IPK.
124 set sql "SELECT * FROM t1, t2, t3 WHERE t1.oid = t2.oid AND t2.oid = t3.oid"
125 speed_trial speed4-join1 50000 row $sql
127 # Join t1, t2, t3 on the non-IPK index.
128 set sql "SELECT * FROM t1, t2, t3 WHERE t1.t = t2.t AND t2.t = t3.t"
129 speed_trial speed4-join2 50000 row $sql
131 # Run 10000 simple queries against the views.
133 for {set ii 1} {$ii < 10000} {incr ii} {
134 append sql "SELECT * FROM v[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
136 speed_trial speed4-view1 10000 stmt $sql
138 # Run the same 10000 simple queries as in the previous test case against
139 # the underlying tables. The compiled vdbe programs should be identical, so
140 # the only difference in running time is the extra time taken to compile
141 # the view definitions.
144 for {set ii 1} {$ii < 10000} {incr ii} {
145 append sql "SELECT t FROM t[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
147 speed_trial speed4-table1 10000 stmt $sql
149 # Run a SELECT that uses sub-queries 10000 times. A total of 30000 sub-selects.
152 for {set ii 1} {$ii < 10000} {incr ii} {
154 SELECT (SELECT t FROM t1 WHERE rowid = [expr {$ii*3}]),
155 (SELECT t FROM t2 WHERE rowid = [expr {$ii*3}]),
156 (SELECT t FROM t3 WHERE rowid = [expr {$ii*3}])
159 speed_trial speed4-subselect1 10000 stmt $sql
161 # The following block tests the speed of some DML statements that cause
165 CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);
166 CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
167 CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN
168 INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);
170 CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN
171 INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);
173 CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN
174 INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);
179 for {set ii 1} {$ii < 10000} {incr ii} {
180 append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
182 speed_trial speed4-trigger1 10000 stmt $sql
184 for {set ii 1} {$ii < 20000} {incr ii 2} {
185 set ii2 [expr {$ii*2}]
187 UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
190 speed_trial speed4-trigger2 10000 stmt $sql
192 for {set ii 1} {$ii < 20000} {incr ii 2} {
193 append sql "DELETE FROM t4 WHERE rowid = $ii;"
195 speed_trial speed4-trigger3 10000 stmt $sql
198 # The following block contains the same tests as the above block that
199 # tests triggers, with one crucial difference: no triggers are defined.
200 # So the difference in speed between these tests and the preceding ones
201 # is the amount of time taken to compile and execute the trigger programs.
207 CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
211 for {set ii 1} {$ii < 10000} {incr ii} {
212 append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
214 speed_trial speed4-notrigger1 10000 stmt $sql
216 for {set ii 1} {$ii < 20000} {incr ii 2} {
217 set ii2 [expr {$ii*2}]
219 UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
222 speed_trial speed4-notrigger2 10000 stmt $sql
224 for {set ii 1} {$ii < 20000} {incr ii 2} {
225 append sql "DELETE FROM t4 WHERE rowid = $ii;"
227 speed_trial speed4-notrigger3 10000 stmt $sql
230 speed_trial_summary speed4