os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed1.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
# 2006 November 23
sl@0
     2
#
sl@0
     3
# The author disclaims copyright to this source code.  In place of
sl@0
     4
# a legal notice, here is a blessing:
sl@0
     5
#
sl@0
     6
#    May you do good and not evil.
sl@0
     7
#    May you find forgiveness for yourself and forgive others.
sl@0
     8
#    May you share freely, never taking more than you give.
sl@0
     9
#
sl@0
    10
#*************************************************************************
sl@0
    11
# This file implements regression tests for SQLite library.  The
sl@0
    12
# focus of this script is measuring executing speed.
sl@0
    13
#
sl@0
    14
# $Id: speed1.test,v 1.9 2008/07/30 13:15:46 drh Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
sqlite3_shutdown
sl@0
    18
#sqlite3_config_scratch 29000 1
sl@0
    19
sqlite3_config_lookaside 1000 300
sl@0
    20
#sqlite3_config_pagecache 1024 10000
sl@0
    21
set testdir [file dirname $argv0]
sl@0
    22
source $testdir/tester.tcl
sl@0
    23
speed_trial_init speed1
sl@0
    24
sl@0
    25
# Set a uniform random seed
sl@0
    26
expr srand(0)
sl@0
    27
sl@0
    28
set sqlout [open speed1.txt w]
sl@0
    29
proc tracesql {sql} {
sl@0
    30
  puts $::sqlout $sql\;
sl@0
    31
}
sl@0
    32
#db trace tracesql
sl@0
    33
sl@0
    34
# The number_name procedure below converts its argment (an integer)
sl@0
    35
# into a string which is the English-language name for that number.
sl@0
    36
#
sl@0
    37
# Example:
sl@0
    38
#
sl@0
    39
#     puts [number_name 123]   ->  "one hundred twenty three"
sl@0
    40
#
sl@0
    41
set ones {zero one two three four five six seven eight nine
sl@0
    42
          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
sl@0
    43
          eighteen nineteen}
sl@0
    44
set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
sl@0
    45
proc number_name {n} {
sl@0
    46
  if {$n>=1000} {
sl@0
    47
    set txt "[number_name [expr {$n/1000}]] thousand"
sl@0
    48
    set n [expr {$n%1000}]
sl@0
    49
  } else {
sl@0
    50
    set txt {}
sl@0
    51
  }
sl@0
    52
  if {$n>=100} {
sl@0
    53
    append txt " [lindex $::ones [expr {$n/100}]] hundred"
sl@0
    54
    set n [expr {$n%100}]
sl@0
    55
  }
sl@0
    56
  if {$n>=20} {
sl@0
    57
    append txt " [lindex $::tens [expr {$n/10}]]"
sl@0
    58
    set n [expr {$n%10}]
sl@0
    59
  }
sl@0
    60
  if {$n>0} {
sl@0
    61
    append txt " [lindex $::ones $n]"
sl@0
    62
  }
sl@0
    63
  set txt [string trim $txt]
sl@0
    64
  if {$txt==""} {set txt zero}
sl@0
    65
  return $txt
sl@0
    66
}
sl@0
    67
sl@0
    68
# Create a database schema.
sl@0
    69
#
sl@0
    70
do_test speed1-1.0 {
sl@0
    71
  execsql {
sl@0
    72
    PRAGMA page_size=1024;
sl@0
    73
    PRAGMA cache_size=8192;
sl@0
    74
    PRAGMA locking_mode=EXCLUSIVE;
sl@0
    75
    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
sl@0
    76
    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
sl@0
    77
    CREATE INDEX i2a ON t2(a);
sl@0
    78
    CREATE INDEX i2b ON t2(b);
sl@0
    79
  }
sl@0
    80
  execsql {
sl@0
    81
    SELECT name FROM sqlite_master ORDER BY 1;
sl@0
    82
  }
sl@0
    83
} {i2a i2b t1 t2}
sl@0
    84
sl@0
    85
sl@0
    86
# 50000 INSERTs on an unindexed table
sl@0
    87
#
sl@0
    88
set sql {}
sl@0
    89
for {set i 1} {$i<=50000} {incr i} {
sl@0
    90
  set r [expr {int(rand()*500000)}]
sl@0
    91
  append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
sl@0
    92
}
sl@0
    93
db eval BEGIN
sl@0
    94
speed_trial speed1-insert1 50000 row $sql
sl@0
    95
db eval COMMIT
sl@0
    96
sl@0
    97
# 50000 INSERTs on an indexed table
sl@0
    98
#
sl@0
    99
set sql {}
sl@0
   100
for {set i 1} {$i<=50000} {incr i} {
sl@0
   101
  set r [expr {int(rand()*500000)}]
sl@0
   102
  append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
sl@0
   103
}
sl@0
   104
db eval BEGIN
sl@0
   105
speed_trial speed1-insert2 50000 row $sql
sl@0
   106
db eval COMMIT
sl@0
   107
sl@0
   108
sl@0
   109
sl@0
   110
# 50 SELECTs on an integer comparison.  There is no index so
sl@0
   111
# a full table scan is required.
sl@0
   112
#
sl@0
   113
set sql {}
sl@0
   114
for {set i 0} {$i<50} {incr i} {
sl@0
   115
  set lwr [expr {$i*100}]
sl@0
   116
  set upr [expr {($i+10)*100}]
sl@0
   117
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   118
}
sl@0
   119
db eval BEGIN
sl@0
   120
speed_trial speed1-select1 [expr {50*50000}] row $sql
sl@0
   121
db eval COMMIT
sl@0
   122
sl@0
   123
# 50 SELECTs on an LIKE comparison.  There is no index so a full
sl@0
   124
# table scan is required.
sl@0
   125
#
sl@0
   126
set sql {}
sl@0
   127
for {set i 0} {$i<50} {incr i} {
sl@0
   128
  append sql \
sl@0
   129
    "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
sl@0
   130
}
sl@0
   131
db eval BEGIN
sl@0
   132
speed_trial speed1-select2 [expr {50*50000}] row $sql
sl@0
   133
db eval COMMIT
sl@0
   134
sl@0
   135
# Create indices
sl@0
   136
#
sl@0
   137
db eval BEGIN
sl@0
   138
speed_trial speed1-createidx 150000 row {
sl@0
   139
  CREATE INDEX i1a ON t1(a);
sl@0
   140
  CREATE INDEX i1b ON t1(b);
sl@0
   141
  CREATE INDEX i1c ON t1(c);
sl@0
   142
}
sl@0
   143
db eval COMMIT
sl@0
   144
sl@0
   145
# 5000 SELECTs on an integer comparison where the integer is
sl@0
   146
# indexed.
sl@0
   147
#
sl@0
   148
set sql {}
sl@0
   149
for {set i 0} {$i<5000} {incr i} {
sl@0
   150
  set lwr [expr {$i*100}]
sl@0
   151
  set upr [expr {($i+10)*100}]
sl@0
   152
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   153
}
sl@0
   154
db eval BEGIN
sl@0
   155
speed_trial speed1-select3 5000 stmt $sql
sl@0
   156
db eval COMMIT
sl@0
   157
sl@0
   158
# 100000 random SELECTs against rowid.
sl@0
   159
#
sl@0
   160
set sql {}
sl@0
   161
for {set i 1} {$i<=100000} {incr i} {
sl@0
   162
  set id [expr {int(rand()*50000)+1}]
sl@0
   163
  append sql "SELECT c FROM t1 WHERE rowid=$id;"
sl@0
   164
}
sl@0
   165
db eval BEGIN
sl@0
   166
speed_trial speed1-select4 100000 row $sql
sl@0
   167
db eval COMMIT
sl@0
   168
sl@0
   169
# 100000 random SELECTs against a unique indexed column.
sl@0
   170
#
sl@0
   171
set sql {}
sl@0
   172
for {set i 1} {$i<=100000} {incr i} {
sl@0
   173
  set id [expr {int(rand()*50000)+1}]
sl@0
   174
  append sql "SELECT c FROM t1 WHERE a=$id;"
sl@0
   175
}
sl@0
   176
db eval BEGIN
sl@0
   177
speed_trial speed1-select5 100000 row $sql
sl@0
   178
db eval COMMIT
sl@0
   179
sl@0
   180
# 50000 random SELECTs against an indexed column text column
sl@0
   181
#
sl@0
   182
set sql {}
sl@0
   183
db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
sl@0
   184
  append sql "SELECT c FROM t1 WHERE c='$c';"
sl@0
   185
}
sl@0
   186
db eval BEGIN
sl@0
   187
speed_trial speed1-select6 50000 row $sql
sl@0
   188
db eval COMMIT
sl@0
   189
sl@0
   190
sl@0
   191
# Vacuum
sl@0
   192
speed_trial speed1-vacuum 100000 row VACUUM
sl@0
   193
sl@0
   194
# 5000 updates of ranges where the field being compared is indexed.
sl@0
   195
#
sl@0
   196
set sql {}
sl@0
   197
for {set i 0} {$i<5000} {incr i} {
sl@0
   198
  set lwr [expr {$i*2}]
sl@0
   199
  set upr [expr {($i+1)*2}]
sl@0
   200
  append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
sl@0
   201
}
sl@0
   202
db eval BEGIN
sl@0
   203
speed_trial speed1-update1 5000 stmt $sql
sl@0
   204
db eval COMMIT
sl@0
   205
sl@0
   206
# 50000 single-row updates.  An index is used to find the row quickly.
sl@0
   207
#
sl@0
   208
set sql {}
sl@0
   209
for {set i 0} {$i<50000} {incr i} {
sl@0
   210
  set r [expr {int(rand()*500000)}]
sl@0
   211
  append sql "UPDATE t1 SET b=$r WHERE a=$i;"
sl@0
   212
}
sl@0
   213
db eval BEGIN
sl@0
   214
speed_trial speed1-update2 50000 row $sql
sl@0
   215
db eval COMMIT
sl@0
   216
sl@0
   217
# 1 big text update that touches every row in the table.
sl@0
   218
#
sl@0
   219
speed_trial speed1-update3 50000 row {
sl@0
   220
  UPDATE t1 SET c=a;
sl@0
   221
}
sl@0
   222
sl@0
   223
# Many individual text updates.  Each row in the table is
sl@0
   224
# touched through an index.
sl@0
   225
#
sl@0
   226
set sql {}
sl@0
   227
for {set i 1} {$i<=50000} {incr i} {
sl@0
   228
  set r [expr {int(rand()*500000)}]
sl@0
   229
  append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
sl@0
   230
}
sl@0
   231
db eval BEGIN
sl@0
   232
speed_trial speed1-update4 50000 row $sql
sl@0
   233
db eval COMMIT
sl@0
   234
sl@0
   235
# Delete all content in a table.
sl@0
   236
#
sl@0
   237
speed_trial speed1-delete1 50000 row {DELETE FROM t1}
sl@0
   238
sl@0
   239
# Copy one table into another
sl@0
   240
#
sl@0
   241
speed_trial speed1-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   242
sl@0
   243
# Delete all content in a table, one row at a time.
sl@0
   244
#
sl@0
   245
speed_trial speed1-delete2 50000 row {DELETE FROM t1 WHERE 1}
sl@0
   246
sl@0
   247
# Refill the table yet again
sl@0
   248
#
sl@0
   249
speed_trial speed1-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   250
sl@0
   251
# Drop the table and recreate it without its indices.
sl@0
   252
#
sl@0
   253
db eval BEGIN
sl@0
   254
speed_trial speed1-drop1 50000 row {
sl@0
   255
   DROP TABLE t1;
sl@0
   256
   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
sl@0
   257
}
sl@0
   258
db eval COMMIT
sl@0
   259
sl@0
   260
# Refill the table yet again.  This copy should be faster because
sl@0
   261
# there are no indices to deal with.
sl@0
   262
#
sl@0
   263
speed_trial speed1-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   264
sl@0
   265
# Select 20000 rows from the table at random.
sl@0
   266
#
sl@0
   267
speed_trial speed1-random1 50000 row {
sl@0
   268
  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
sl@0
   269
}
sl@0
   270
sl@0
   271
# Delete 20000 random rows from the table.
sl@0
   272
#
sl@0
   273
speed_trial speed1-random-del1 20000 row {
sl@0
   274
  DELETE FROM t1 WHERE rowid IN
sl@0
   275
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   276
}
sl@0
   277
do_test speed1-1.1 {
sl@0
   278
  db one {SELECT count(*) FROM t1}
sl@0
   279
} 30000
sl@0
   280
sl@0
   281
    
sl@0
   282
# Delete 20000 more rows at random from the table.
sl@0
   283
#
sl@0
   284
speed_trial speed1-random-del2 20000 row {
sl@0
   285
  DELETE FROM t1 WHERE rowid IN
sl@0
   286
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   287
}
sl@0
   288
do_test speed1-1.2 {
sl@0
   289
  db one {SELECT count(*) FROM t1}
sl@0
   290
} 10000
sl@0
   291
speed_trial_summary speed1
sl@0
   292
sl@0
   293
finish_test