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