1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed4.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,231 @@
1.4 +# 2007 October 23
1.5 +#
1.6 +# The author disclaims copyright to this source code. In place of
1.7 +# a legal notice, here is a blessing:
1.8 +#
1.9 +# May you do good and not evil.
1.10 +# May you find forgiveness for yourself and forgive others.
1.11 +# May you share freely, never taking more than you give.
1.12 +#
1.13 +#*************************************************************************
1.14 +# This file implements regression tests for SQLite library. The
1.15 +# focus of this script is measuring executing speed. More specifically,
1.16 +# the focus is on the speed of:
1.17 +#
1.18 +# * joins
1.19 +# * views
1.20 +# * sub-selects
1.21 +# * triggers
1.22 +#
1.23 +# $Id: speed4.test,v 1.2 2008/07/12 14:52:20 drh Exp $
1.24 +#
1.25 +
1.26 +set testdir [file dirname $argv0]
1.27 +source $testdir/tester.tcl
1.28 +speed_trial_init speed1
1.29 +
1.30 +# Set a uniform random seed
1.31 +expr srand(0)
1.32 +
1.33 +set sqlout [open speed1.txt w]
1.34 +proc tracesql {sql} {
1.35 + puts $::sqlout $sql\;
1.36 +}
1.37 +#db trace tracesql
1.38 +
1.39 +# The number_name procedure below converts its argment (an integer)
1.40 +# into a string which is the English-language name for that number.
1.41 +#
1.42 +# Example:
1.43 +#
1.44 +# puts [number_name 123] -> "one hundred twenty three"
1.45 +#
1.46 +set ones {zero one two three four five six seven eight nine
1.47 + ten eleven twelve thirteen fourteen fifteen sixteen seventeen
1.48 + eighteen nineteen}
1.49 +set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
1.50 +proc number_name {n} {
1.51 + if {$n>=1000} {
1.52 + set txt "[number_name [expr {$n/1000}]] thousand"
1.53 + set n [expr {$n%1000}]
1.54 + } else {
1.55 + set txt {}
1.56 + }
1.57 + if {$n>=100} {
1.58 + append txt " [lindex $::ones [expr {$n/100}]] hundred"
1.59 + set n [expr {$n%100}]
1.60 + }
1.61 + if {$n>=20} {
1.62 + append txt " [lindex $::tens [expr {$n/10}]]"
1.63 + set n [expr {$n%10}]
1.64 + }
1.65 + if {$n>0} {
1.66 + append txt " [lindex $::ones $n]"
1.67 + }
1.68 + set txt [string trim $txt]
1.69 + if {$txt==""} {set txt zero}
1.70 + return $txt
1.71 +}
1.72 +
1.73 +# Summary of tests:
1.74 +#
1.75 +# speed4-join1: Join three tables using IPK index.
1.76 +# speed4-join2: Join three tables using an index.
1.77 +# speed4-join3: Join two tables without an index.
1.78 +#
1.79 +# speed4-view1: Querying a view.
1.80 +# speed4-table1: Same queries as in speed4-view1, but run directly against
1.81 +# the tables for comparison purposes.
1.82 +#
1.83 +# speed4-subselect1: A SELECT statement that uses many sub-queries..
1.84 +#
1.85 +# speed4-trigger1: An INSERT statement that fires a trigger.
1.86 +# speed4-trigger2: An UPDATE statement that fires a trigger.
1.87 +# speed4-trigger3: A DELETE statement that fires a trigger.
1.88 +# speed4-notrigger1: Same operation as trigger1, but without the trigger.
1.89 +# speed4-notrigger2: " trigger2 "
1.90 +# speed4-notrigger3: " trigger3 "
1.91 +#
1.92 +
1.93 +# Set up the schema. Each of the tables t1, t2 and t3 contain 50,000 rows.
1.94 +# This creates a database of around 16MB.
1.95 +execsql {
1.96 + BEGIN;
1.97 + CREATE TABLE t1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
1.98 + CREATE TABLE t2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
1.99 + CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
1.100 +
1.101 + CREATE VIEW v1 AS SELECT rowid, i, t FROM t1;
1.102 + CREATE VIEW v2 AS SELECT rowid, i, t FROM t2;
1.103 + CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;
1.104 +}
1.105 +for {set jj 1} {$jj <= 3} {incr jj} {
1.106 + set stmt [string map "%T% t$jj" {INSERT INTO %T% VALUES(NULL, $i, $t)}]
1.107 + for {set ii 0} {$ii < 50000} {incr ii} {
1.108 + set i [expr {int(rand()*50000)}]
1.109 + set t [number_name $i]
1.110 + execsql $stmt
1.111 + }
1.112 +}
1.113 +execsql {
1.114 + CREATE INDEX i1 ON t1(t);
1.115 + CREATE INDEX i2 ON t2(t);
1.116 + CREATE INDEX i3 ON t3(t);
1.117 + COMMIT;
1.118 +}
1.119 +
1.120 +# Before running these tests, disable the compiled statement cache built into
1.121 +# the Tcl interface. This is because we want to test the speed of SQL
1.122 +# compilation as well as execution.
1.123 +#
1.124 +db cache size 0
1.125 +
1.126 +# Join t1, t2, t3 on IPK.
1.127 +set sql "SELECT * FROM t1, t2, t3 WHERE t1.oid = t2.oid AND t2.oid = t3.oid"
1.128 +speed_trial speed4-join1 50000 row $sql
1.129 +
1.130 +# Join t1, t2, t3 on the non-IPK index.
1.131 +set sql "SELECT * FROM t1, t2, t3 WHERE t1.t = t2.t AND t2.t = t3.t"
1.132 +speed_trial speed4-join2 50000 row $sql
1.133 +
1.134 +# Run 10000 simple queries against the views.
1.135 +set sql ""
1.136 +for {set ii 1} {$ii < 10000} {incr ii} {
1.137 + append sql "SELECT * FROM v[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
1.138 +}
1.139 +speed_trial speed4-view1 10000 stmt $sql
1.140 +
1.141 +# Run the same 10000 simple queries as in the previous test case against
1.142 +# the underlying tables. The compiled vdbe programs should be identical, so
1.143 +# the only difference in running time is the extra time taken to compile
1.144 +# the view definitions.
1.145 +#
1.146 +set sql ""
1.147 +for {set ii 1} {$ii < 10000} {incr ii} {
1.148 + append sql "SELECT t FROM t[expr {($ii%3)+1}] WHERE rowid = [expr {$ii*3}];"
1.149 +}
1.150 +speed_trial speed4-table1 10000 stmt $sql
1.151 +
1.152 +# Run a SELECT that uses sub-queries 10000 times. A total of 30000 sub-selects.
1.153 +#
1.154 +set sql ""
1.155 +for {set ii 1} {$ii < 10000} {incr ii} {
1.156 + append sql "
1.157 + SELECT (SELECT t FROM t1 WHERE rowid = [expr {$ii*3}]),
1.158 + (SELECT t FROM t2 WHERE rowid = [expr {$ii*3}]),
1.159 + (SELECT t FROM t3 WHERE rowid = [expr {$ii*3}])
1.160 + ;"
1.161 +}
1.162 +speed_trial speed4-subselect1 10000 stmt $sql
1.163 +
1.164 +# The following block tests the speed of some DML statements that cause
1.165 +# triggers to fire.
1.166 +#
1.167 +execsql {
1.168 + CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);
1.169 + CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
1.170 + CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN
1.171 + INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);
1.172 + END;
1.173 + CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN
1.174 + INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);
1.175 + END;
1.176 + CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN
1.177 + INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);
1.178 + END;
1.179 + BEGIN;
1.180 +}
1.181 +set sql ""
1.182 +for {set ii 1} {$ii < 10000} {incr ii} {
1.183 + append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
1.184 +}
1.185 +speed_trial speed4-trigger1 10000 stmt $sql
1.186 +set sql ""
1.187 +for {set ii 1} {$ii < 20000} {incr ii 2} {
1.188 + set ii2 [expr {$ii*2}]
1.189 + append sql "
1.190 + UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
1.191 + "
1.192 +}
1.193 +speed_trial speed4-trigger2 10000 stmt $sql
1.194 +set sql ""
1.195 +for {set ii 1} {$ii < 20000} {incr ii 2} {
1.196 + append sql "DELETE FROM t4 WHERE rowid = $ii;"
1.197 +}
1.198 +speed_trial speed4-trigger3 10000 stmt $sql
1.199 +execsql {COMMIT}
1.200 +
1.201 +# The following block contains the same tests as the above block that
1.202 +# tests triggers, with one crucial difference: no triggers are defined.
1.203 +# So the difference in speed between these tests and the preceding ones
1.204 +# is the amount of time taken to compile and execute the trigger programs.
1.205 +#
1.206 +execsql {
1.207 + DROP TABLE t4;
1.208 + DROP TABLE log;
1.209 + VACUUM;
1.210 + CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
1.211 + BEGIN;
1.212 +}
1.213 +set sql ""
1.214 +for {set ii 1} {$ii < 10000} {incr ii} {
1.215 + append sql "INSERT INTO t4 VALUES(NULL, $ii, '[number_name $ii]');"
1.216 +}
1.217 +speed_trial speed4-notrigger1 10000 stmt $sql
1.218 +set sql ""
1.219 +for {set ii 1} {$ii < 20000} {incr ii 2} {
1.220 + set ii2 [expr {$ii*2}]
1.221 + append sql "
1.222 + UPDATE t4 SET i = $ii2, t = '[number_name $ii2]' WHERE rowid = $ii;
1.223 + "
1.224 +}
1.225 +speed_trial speed4-notrigger2 10000 stmt $sql
1.226 +set sql ""
1.227 +for {set ii 1} {$ii < 20000} {incr ii 2} {
1.228 + append sql "DELETE FROM t4 WHERE rowid = $ii;"
1.229 +}
1.230 +speed_trial speed4-notrigger3 10000 stmt $sql
1.231 +execsql {COMMIT}
1.232 +
1.233 +speed_trial_summary speed4
1.234 +finish_test