1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed2.test Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,339 @@
1.4 +# 2006 November 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.
1.16 +#
1.17 +# $Id: speed2.test,v 1.7 2007/04/16 15:02:20 drh Exp $
1.18 +#
1.19 +
1.20 +set testdir [file dirname $argv0]
1.21 +source $testdir/tester.tcl
1.22 +speed_trial_init speed2
1.23 +
1.24 +# Set a uniform random seed
1.25 +expr srand(0)
1.26 +
1.27 +set sqlout [open speed2.txt w]
1.28 +proc tracesql {sql} {
1.29 + puts $::sqlout $sql\;
1.30 +}
1.31 +#db trace tracesql
1.32 +
1.33 +# The number_name procedure below converts its argment (an integer)
1.34 +# into a string which is the English-language name for that number.
1.35 +#
1.36 +# Example:
1.37 +#
1.38 +# puts [number_name 123] -> "one hundred twenty three"
1.39 +#
1.40 +set ones {zero one two three four five six seven eight nine
1.41 + ten eleven twelve thirteen fourteen fifteen sixteen seventeen
1.42 + eighteen nineteen}
1.43 +set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
1.44 +proc number_name {n} {
1.45 + if {$n>=1000} {
1.46 + set txt "[number_name [expr {$n/1000}]] thousand"
1.47 + set n [expr {$n%1000}]
1.48 + } else {
1.49 + set txt {}
1.50 + }
1.51 + if {$n>=100} {
1.52 + append txt " [lindex $::ones [expr {$n/100}]] hundred"
1.53 + set n [expr {$n%100}]
1.54 + }
1.55 + if {$n>=20} {
1.56 + append txt " [lindex $::tens [expr {$n/10}]]"
1.57 + set n [expr {$n%10}]
1.58 + }
1.59 + if {$n>0} {
1.60 + append txt " [lindex $::ones $n]"
1.61 + }
1.62 + set txt [string trim $txt]
1.63 + if {$txt==""} {set txt zero}
1.64 + return $txt
1.65 +}
1.66 +
1.67 +# Create a database schema.
1.68 +#
1.69 +do_test speed2-1.0 {
1.70 + execsql {
1.71 + PRAGMA page_size=1024;
1.72 + PRAGMA cache_size=8192;
1.73 + PRAGMA locking_mode=EXCLUSIVE;
1.74 + CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
1.75 + CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
1.76 + CREATE INDEX i2a ON t2(a);
1.77 + CREATE INDEX i2b ON t2(b);
1.78 + }
1.79 + execsql {
1.80 + SELECT name FROM sqlite_master ORDER BY 1;
1.81 + }
1.82 +} {i2a i2b t1 t2}
1.83 +
1.84 +
1.85 +# 50000 INSERTs on an unindexed table
1.86 +#
1.87 +set sql {}
1.88 +for {set i 1} {$i<=50000} {incr i} {
1.89 + set r [expr {int(rand()*500000)}]
1.90 + append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
1.91 +}
1.92 +db eval BEGIN
1.93 +speed_trial speed2-insert1 50000 row $sql
1.94 +db eval COMMIT
1.95 +
1.96 +# 50000 INSERTs on an indexed table
1.97 +#
1.98 +set sql {}
1.99 +for {set i 1} {$i<=50000} {incr i} {
1.100 + set r [expr {int(rand()*500000)}]
1.101 + append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
1.102 +}
1.103 +db eval BEGIN
1.104 +speed_trial speed2-insert2 50000 row $sql
1.105 +db eval COMMIT
1.106 +
1.107 +
1.108 +
1.109 +# 50 SELECTs on an integer comparison. There is no index so
1.110 +# a full table scan is required.
1.111 +#
1.112 +set sql {}
1.113 +for {set i 0} {$i<50} {incr i} {
1.114 + set lwr [expr {$i*100}]
1.115 + set upr [expr {($i+10)*100}]
1.116 + append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
1.117 +}
1.118 +speed_trial speed2-select1a [expr {50*50000}] row $sql
1.119 +
1.120 +# 50 SELECTs on an LIKE comparison. There is no index so a full
1.121 +# table scan is required.
1.122 +#
1.123 +set sql {}
1.124 +for {set i 0} {$i<50} {incr i} {
1.125 + append sql \
1.126 + "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
1.127 +}
1.128 +speed_trial speed2-select2a [expr {50*50000}] row $sql
1.129 +
1.130 +# Vacuum
1.131 +speed_trial speed2-vacuum1 100000 row VACUUM
1.132 +
1.133 +# 50 SELECTs on an integer comparison. There is no index so
1.134 +# a full table scan is required.
1.135 +#
1.136 +set sql {}
1.137 +for {set i 0} {$i<50} {incr i} {
1.138 + set lwr [expr {$i*100}]
1.139 + set upr [expr {($i+10)*100}]
1.140 + append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
1.141 +}
1.142 +speed_trial speed2-select1b [expr {50*50000}] row $sql
1.143 +
1.144 +# 50 SELECTs on an LIKE comparison. There is no index so a full
1.145 +# table scan is required.
1.146 +#
1.147 +set sql {}
1.148 +for {set i 0} {$i<50} {incr i} {
1.149 + append sql \
1.150 + "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
1.151 +}
1.152 +speed_trial speed2-select2b [expr {50*50000}] row $sql
1.153 +
1.154 +# Create indices
1.155 +#
1.156 +db eval BEGIN
1.157 +speed_trial speed2-createidx 150000 row {
1.158 + CREATE INDEX i1a ON t1(a);
1.159 + CREATE INDEX i1b ON t1(b);
1.160 + CREATE INDEX i1c ON t1(c);
1.161 +}
1.162 +db eval COMMIT
1.163 +
1.164 +# 5000 SELECTs on an integer comparison where the integer is
1.165 +# indexed.
1.166 +#
1.167 +set sql {}
1.168 +for {set i 0} {$i<5000} {incr i} {
1.169 + set lwr [expr {$i*100}]
1.170 + set upr [expr {($i+10)*100}]
1.171 + append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
1.172 +}
1.173 +speed_trial speed2-select3a 5000 stmt $sql
1.174 +
1.175 +# 100000 random SELECTs against rowid.
1.176 +#
1.177 +set sql {}
1.178 +for {set i 1} {$i<=100000} {incr i} {
1.179 + set id [expr {int(rand()*50000)+1}]
1.180 + append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
1.181 +}
1.182 +speed_trial speed2-select4a 100000 row $sql
1.183 +
1.184 +# 100000 random SELECTs against a unique indexed column.
1.185 +#
1.186 +set sql {}
1.187 +for {set i 1} {$i<=100000} {incr i} {
1.188 + set id [expr {int(rand()*50000)+1}]
1.189 + append sql "SELECT c FROM t1 WHERE a=$id;"
1.190 +}
1.191 +speed_trial speed2-select5a 100000 row $sql
1.192 +
1.193 +# 50000 random SELECTs against an indexed column text column
1.194 +#
1.195 +set sql {}
1.196 +db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
1.197 + append sql "SELECT c FROM t1 WHERE c='$c';"
1.198 +}
1.199 +speed_trial speed2-select6a 50000 row $sql
1.200 +
1.201 +# Vacuum
1.202 +speed_trial speed2-vacuum2 100000 row VACUUM
1.203 +
1.204 +
1.205 +# 5000 SELECTs on an integer comparison where the integer is
1.206 +# indexed.
1.207 +#
1.208 +set sql {}
1.209 +for {set i 0} {$i<5000} {incr i} {
1.210 + set lwr [expr {$i*100}]
1.211 + set upr [expr {($i+10)*100}]
1.212 + append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
1.213 +}
1.214 +speed_trial speed2-select3b 5000 stmt $sql
1.215 +
1.216 +# 100000 random SELECTs against rowid.
1.217 +#
1.218 +set sql {}
1.219 +for {set i 1} {$i<=100000} {incr i} {
1.220 + set id [expr {int(rand()*50000)+1}]
1.221 + append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
1.222 +}
1.223 +speed_trial speed2-select4b 100000 row $sql
1.224 +
1.225 +# 100000 random SELECTs against a unique indexed column.
1.226 +#
1.227 +set sql {}
1.228 +for {set i 1} {$i<=100000} {incr i} {
1.229 + set id [expr {int(rand()*50000)+1}]
1.230 + append sql "SELECT c FROM t1 WHERE a=$id;"
1.231 +}
1.232 +speed_trial speed2-select5b 100000 row $sql
1.233 +
1.234 +# 50000 random SELECTs against an indexed column text column
1.235 +#
1.236 +set sql {}
1.237 +db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
1.238 + append sql "SELECT c FROM t1 WHERE c='$c';"
1.239 +}
1.240 +speed_trial speed2-select6b 50000 row $sql
1.241 +
1.242 +# 5000 updates of ranges where the field being compared is indexed.
1.243 +#
1.244 +set sql {}
1.245 +for {set i 0} {$i<5000} {incr i} {
1.246 + set lwr [expr {$i*2}]
1.247 + set upr [expr {($i+1)*2}]
1.248 + append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
1.249 +}
1.250 +db eval BEGIN
1.251 +speed_trial speed2-update1 5000 stmt $sql
1.252 +db eval COMMIT
1.253 +
1.254 +# 50000 single-row updates. An index is used to find the row quickly.
1.255 +#
1.256 +set sql {}
1.257 +for {set i 0} {$i<50000} {incr i} {
1.258 + set r [expr {int(rand()*500000)}]
1.259 + append sql "UPDATE t1 SET b=$r WHERE a=$i;"
1.260 +}
1.261 +db eval BEGIN
1.262 +speed_trial speed2-update2 50000 row $sql
1.263 +db eval COMMIT
1.264 +
1.265 +# 1 big text update that touches every row in the table.
1.266 +#
1.267 +speed_trial speed2-update3 50000 row {
1.268 + UPDATE t1 SET c=a;
1.269 +}
1.270 +
1.271 +# Many individual text updates. Each row in the table is
1.272 +# touched through an index.
1.273 +#
1.274 +set sql {}
1.275 +for {set i 1} {$i<=50000} {incr i} {
1.276 + set r [expr {int(rand()*500000)}]
1.277 + append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
1.278 +}
1.279 +db eval BEGIN
1.280 +speed_trial speed2-update4 50000 row $sql
1.281 +db eval COMMIT
1.282 +
1.283 +# Delete all content in a table.
1.284 +#
1.285 +speed_trial speed2-delete1 50000 row {DELETE FROM t1}
1.286 +
1.287 +# Copy one table into another
1.288 +#
1.289 +speed_trial speed2-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
1.290 +
1.291 +# Delete all content in a table, one row at a time.
1.292 +#
1.293 +speed_trial speed2-delete2 50000 row {DELETE FROM t1 WHERE 1}
1.294 +
1.295 +# Refill the table yet again
1.296 +#
1.297 +speed_trial speed2-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
1.298 +
1.299 +# Drop the table and recreate it without its indices.
1.300 +#
1.301 +db eval BEGIN
1.302 +speed_trial speed2-drop1 50000 row {
1.303 + DROP TABLE t1;
1.304 + CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
1.305 +}
1.306 +db eval COMMIT
1.307 +
1.308 +# Refill the table yet again. This copy should be faster because
1.309 +# there are no indices to deal with.
1.310 +#
1.311 +speed_trial speed2-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
1.312 +
1.313 +# Select 20000 rows from the table at random.
1.314 +#
1.315 +speed_trial speed2-random1 50000 row {
1.316 + SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
1.317 +}
1.318 +
1.319 +# Delete 20000 random rows from the table.
1.320 +#
1.321 +speed_trial speed2-random-del1 20000 row {
1.322 + DELETE FROM t1 WHERE rowid IN
1.323 + (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
1.324 +}
1.325 +do_test speed2-1.1 {
1.326 + db one {SELECT count(*) FROM t1}
1.327 +} 30000
1.328 +
1.329 +
1.330 +# Delete 20000 more rows at random from the table.
1.331 +#
1.332 +speed_trial speed2-random-del2 20000 row {
1.333 + DELETE FROM t1 WHERE rowid IN
1.334 + (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
1.335 +}
1.336 +do_test speed2-1.2 {
1.337 + db one {SELECT count(*) FROM t1}
1.338 +} 10000
1.339 +speed_trial_summary speed2
1.340 +
1.341 +
1.342 +finish_test