os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed2.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: speed2.test,v 1.7 2007/04/16 15:02:20 drh Exp $
sl@0
    15
#
sl@0
    16
sl@0
    17
set testdir [file dirname $argv0]
sl@0
    18
source $testdir/tester.tcl
sl@0
    19
speed_trial_init speed2
sl@0
    20
sl@0
    21
# Set a uniform random seed
sl@0
    22
expr srand(0)
sl@0
    23
sl@0
    24
set sqlout [open speed2.txt w]
sl@0
    25
proc tracesql {sql} {
sl@0
    26
  puts $::sqlout $sql\;
sl@0
    27
}
sl@0
    28
#db trace tracesql
sl@0
    29
sl@0
    30
# The number_name procedure below converts its argment (an integer)
sl@0
    31
# into a string which is the English-language name for that number.
sl@0
    32
#
sl@0
    33
# Example:
sl@0
    34
#
sl@0
    35
#     puts [number_name 123]   ->  "one hundred twenty three"
sl@0
    36
#
sl@0
    37
set ones {zero one two three four five six seven eight nine
sl@0
    38
          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
sl@0
    39
          eighteen nineteen}
sl@0
    40
set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
sl@0
    41
proc number_name {n} {
sl@0
    42
  if {$n>=1000} {
sl@0
    43
    set txt "[number_name [expr {$n/1000}]] thousand"
sl@0
    44
    set n [expr {$n%1000}]
sl@0
    45
  } else {
sl@0
    46
    set txt {}
sl@0
    47
  }
sl@0
    48
  if {$n>=100} {
sl@0
    49
    append txt " [lindex $::ones [expr {$n/100}]] hundred"
sl@0
    50
    set n [expr {$n%100}]
sl@0
    51
  }
sl@0
    52
  if {$n>=20} {
sl@0
    53
    append txt " [lindex $::tens [expr {$n/10}]]"
sl@0
    54
    set n [expr {$n%10}]
sl@0
    55
  }
sl@0
    56
  if {$n>0} {
sl@0
    57
    append txt " [lindex $::ones $n]"
sl@0
    58
  }
sl@0
    59
  set txt [string trim $txt]
sl@0
    60
  if {$txt==""} {set txt zero}
sl@0
    61
  return $txt
sl@0
    62
}
sl@0
    63
sl@0
    64
# Create a database schema.
sl@0
    65
#
sl@0
    66
do_test speed2-1.0 {
sl@0
    67
  execsql {
sl@0
    68
    PRAGMA page_size=1024;
sl@0
    69
    PRAGMA cache_size=8192;
sl@0
    70
    PRAGMA locking_mode=EXCLUSIVE;
sl@0
    71
    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
sl@0
    72
    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
sl@0
    73
    CREATE INDEX i2a ON t2(a);
sl@0
    74
    CREATE INDEX i2b ON t2(b);
sl@0
    75
  }
sl@0
    76
  execsql {
sl@0
    77
    SELECT name FROM sqlite_master ORDER BY 1;
sl@0
    78
  }
sl@0
    79
} {i2a i2b t1 t2}
sl@0
    80
sl@0
    81
sl@0
    82
# 50000 INSERTs on an unindexed table
sl@0
    83
#
sl@0
    84
set sql {}
sl@0
    85
for {set i 1} {$i<=50000} {incr i} {
sl@0
    86
  set r [expr {int(rand()*500000)}]
sl@0
    87
  append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
sl@0
    88
}
sl@0
    89
db eval BEGIN
sl@0
    90
speed_trial speed2-insert1 50000 row $sql
sl@0
    91
db eval COMMIT
sl@0
    92
sl@0
    93
# 50000 INSERTs on an indexed table
sl@0
    94
#
sl@0
    95
set sql {}
sl@0
    96
for {set i 1} {$i<=50000} {incr i} {
sl@0
    97
  set r [expr {int(rand()*500000)}]
sl@0
    98
  append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
sl@0
    99
}
sl@0
   100
db eval BEGIN
sl@0
   101
speed_trial speed2-insert2 50000 row $sql
sl@0
   102
db eval COMMIT
sl@0
   103
sl@0
   104
sl@0
   105
sl@0
   106
# 50 SELECTs on an integer comparison.  There is no index so
sl@0
   107
# a full table scan is required.
sl@0
   108
#
sl@0
   109
set sql {}
sl@0
   110
for {set i 0} {$i<50} {incr i} {
sl@0
   111
  set lwr [expr {$i*100}]
sl@0
   112
  set upr [expr {($i+10)*100}]
sl@0
   113
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   114
}
sl@0
   115
speed_trial speed2-select1a [expr {50*50000}] row $sql
sl@0
   116
sl@0
   117
# 50 SELECTs on an LIKE comparison.  There is no index so a full
sl@0
   118
# table scan is required.
sl@0
   119
#
sl@0
   120
set sql {}
sl@0
   121
for {set i 0} {$i<50} {incr i} {
sl@0
   122
  append sql \
sl@0
   123
    "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
sl@0
   124
}
sl@0
   125
speed_trial speed2-select2a [expr {50*50000}] row $sql
sl@0
   126
sl@0
   127
# Vacuum
sl@0
   128
speed_trial speed2-vacuum1 100000 row VACUUM
sl@0
   129
sl@0
   130
# 50 SELECTs on an integer comparison.  There is no index so
sl@0
   131
# a full table scan is required.
sl@0
   132
#
sl@0
   133
set sql {}
sl@0
   134
for {set i 0} {$i<50} {incr i} {
sl@0
   135
  set lwr [expr {$i*100}]
sl@0
   136
  set upr [expr {($i+10)*100}]
sl@0
   137
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   138
}
sl@0
   139
speed_trial speed2-select1b [expr {50*50000}] row $sql
sl@0
   140
sl@0
   141
# 50 SELECTs on an LIKE comparison.  There is no index so a full
sl@0
   142
# table scan is required.
sl@0
   143
#
sl@0
   144
set sql {}
sl@0
   145
for {set i 0} {$i<50} {incr i} {
sl@0
   146
  append sql \
sl@0
   147
    "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
sl@0
   148
}
sl@0
   149
speed_trial speed2-select2b [expr {50*50000}] row $sql
sl@0
   150
sl@0
   151
# Create indices
sl@0
   152
#
sl@0
   153
db eval BEGIN
sl@0
   154
speed_trial speed2-createidx 150000 row {
sl@0
   155
  CREATE INDEX i1a ON t1(a);
sl@0
   156
  CREATE INDEX i1b ON t1(b);
sl@0
   157
  CREATE INDEX i1c ON t1(c);
sl@0
   158
}
sl@0
   159
db eval COMMIT
sl@0
   160
sl@0
   161
# 5000 SELECTs on an integer comparison where the integer is
sl@0
   162
# indexed.
sl@0
   163
#
sl@0
   164
set sql {}
sl@0
   165
for {set i 0} {$i<5000} {incr i} {
sl@0
   166
  set lwr [expr {$i*100}]
sl@0
   167
  set upr [expr {($i+10)*100}]
sl@0
   168
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   169
}
sl@0
   170
speed_trial speed2-select3a 5000 stmt $sql
sl@0
   171
sl@0
   172
# 100000 random SELECTs against rowid.
sl@0
   173
#
sl@0
   174
set sql {}
sl@0
   175
for {set i 1} {$i<=100000} {incr i} {
sl@0
   176
  set id [expr {int(rand()*50000)+1}]
sl@0
   177
  append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
sl@0
   178
}
sl@0
   179
speed_trial speed2-select4a 100000 row $sql
sl@0
   180
sl@0
   181
# 100000 random SELECTs against a unique indexed column.
sl@0
   182
#
sl@0
   183
set sql {}
sl@0
   184
for {set i 1} {$i<=100000} {incr i} {
sl@0
   185
  set id [expr {int(rand()*50000)+1}]
sl@0
   186
  append sql "SELECT c FROM t1 WHERE a=$id;"
sl@0
   187
}
sl@0
   188
speed_trial speed2-select5a 100000 row $sql
sl@0
   189
sl@0
   190
# 50000 random SELECTs against an indexed column text column
sl@0
   191
#
sl@0
   192
set sql {}
sl@0
   193
db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
sl@0
   194
  append sql "SELECT c FROM t1 WHERE c='$c';"
sl@0
   195
}
sl@0
   196
speed_trial speed2-select6a 50000 row $sql
sl@0
   197
sl@0
   198
# Vacuum
sl@0
   199
speed_trial speed2-vacuum2 100000 row VACUUM
sl@0
   200
sl@0
   201
sl@0
   202
# 5000 SELECTs on an integer comparison where the integer is
sl@0
   203
# indexed.
sl@0
   204
#
sl@0
   205
set sql {}
sl@0
   206
for {set i 0} {$i<5000} {incr i} {
sl@0
   207
  set lwr [expr {$i*100}]
sl@0
   208
  set upr [expr {($i+10)*100}]
sl@0
   209
  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
sl@0
   210
}
sl@0
   211
speed_trial speed2-select3b 5000 stmt $sql
sl@0
   212
sl@0
   213
# 100000 random SELECTs against rowid.
sl@0
   214
#
sl@0
   215
set sql {}
sl@0
   216
for {set i 1} {$i<=100000} {incr i} {
sl@0
   217
  set id [expr {int(rand()*50000)+1}]
sl@0
   218
  append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
sl@0
   219
}
sl@0
   220
speed_trial speed2-select4b 100000 row $sql
sl@0
   221
sl@0
   222
# 100000 random SELECTs against a unique indexed column.
sl@0
   223
#
sl@0
   224
set sql {}
sl@0
   225
for {set i 1} {$i<=100000} {incr i} {
sl@0
   226
  set id [expr {int(rand()*50000)+1}]
sl@0
   227
  append sql "SELECT c FROM t1 WHERE a=$id;"
sl@0
   228
}
sl@0
   229
speed_trial speed2-select5b 100000 row $sql
sl@0
   230
sl@0
   231
# 50000 random SELECTs against an indexed column text column
sl@0
   232
#
sl@0
   233
set sql {}
sl@0
   234
db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
sl@0
   235
  append sql "SELECT c FROM t1 WHERE c='$c';"
sl@0
   236
}
sl@0
   237
speed_trial speed2-select6b 50000 row $sql
sl@0
   238
sl@0
   239
# 5000 updates of ranges where the field being compared is indexed.
sl@0
   240
#
sl@0
   241
set sql {}
sl@0
   242
for {set i 0} {$i<5000} {incr i} {
sl@0
   243
  set lwr [expr {$i*2}]
sl@0
   244
  set upr [expr {($i+1)*2}]
sl@0
   245
  append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
sl@0
   246
}
sl@0
   247
db eval BEGIN
sl@0
   248
speed_trial speed2-update1 5000 stmt $sql
sl@0
   249
db eval COMMIT
sl@0
   250
sl@0
   251
# 50000 single-row updates.  An index is used to find the row quickly.
sl@0
   252
#
sl@0
   253
set sql {}
sl@0
   254
for {set i 0} {$i<50000} {incr i} {
sl@0
   255
  set r [expr {int(rand()*500000)}]
sl@0
   256
  append sql "UPDATE t1 SET b=$r WHERE a=$i;"
sl@0
   257
}
sl@0
   258
db eval BEGIN
sl@0
   259
speed_trial speed2-update2 50000 row $sql
sl@0
   260
db eval COMMIT
sl@0
   261
sl@0
   262
# 1 big text update that touches every row in the table.
sl@0
   263
#
sl@0
   264
speed_trial speed2-update3 50000 row {
sl@0
   265
  UPDATE t1 SET c=a;
sl@0
   266
}
sl@0
   267
sl@0
   268
# Many individual text updates.  Each row in the table is
sl@0
   269
# touched through an index.
sl@0
   270
#
sl@0
   271
set sql {}
sl@0
   272
for {set i 1} {$i<=50000} {incr i} {
sl@0
   273
  set r [expr {int(rand()*500000)}]
sl@0
   274
  append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
sl@0
   275
}
sl@0
   276
db eval BEGIN
sl@0
   277
speed_trial speed2-update4 50000 row $sql
sl@0
   278
db eval COMMIT
sl@0
   279
sl@0
   280
# Delete all content in a table.
sl@0
   281
#
sl@0
   282
speed_trial speed2-delete1 50000 row {DELETE FROM t1}
sl@0
   283
sl@0
   284
# Copy one table into another
sl@0
   285
#
sl@0
   286
speed_trial speed2-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   287
sl@0
   288
# Delete all content in a table, one row at a time.
sl@0
   289
#
sl@0
   290
speed_trial speed2-delete2 50000 row {DELETE FROM t1 WHERE 1}
sl@0
   291
sl@0
   292
# Refill the table yet again
sl@0
   293
#
sl@0
   294
speed_trial speed2-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   295
sl@0
   296
# Drop the table and recreate it without its indices.
sl@0
   297
#
sl@0
   298
db eval BEGIN
sl@0
   299
speed_trial speed2-drop1 50000 row {
sl@0
   300
   DROP TABLE t1;
sl@0
   301
   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
sl@0
   302
}
sl@0
   303
db eval COMMIT
sl@0
   304
sl@0
   305
# Refill the table yet again.  This copy should be faster because
sl@0
   306
# there are no indices to deal with.
sl@0
   307
#
sl@0
   308
speed_trial speed2-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   309
sl@0
   310
# Select 20000 rows from the table at random.
sl@0
   311
#
sl@0
   312
speed_trial speed2-random1 50000 row {
sl@0
   313
  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
sl@0
   314
}
sl@0
   315
sl@0
   316
# Delete 20000 random rows from the table.
sl@0
   317
#
sl@0
   318
speed_trial speed2-random-del1 20000 row {
sl@0
   319
  DELETE FROM t1 WHERE rowid IN
sl@0
   320
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   321
}
sl@0
   322
do_test speed2-1.1 {
sl@0
   323
  db one {SELECT count(*) FROM t1}
sl@0
   324
} 30000
sl@0
   325
sl@0
   326
    
sl@0
   327
# Delete 20000 more rows at random from the table.
sl@0
   328
#
sl@0
   329
speed_trial speed2-random-del2 20000 row {
sl@0
   330
  DELETE FROM t1 WHERE rowid IN
sl@0
   331
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   332
}
sl@0
   333
do_test speed2-1.2 {
sl@0
   334
  db one {SELECT count(*) FROM t1}
sl@0
   335
} 10000
sl@0
   336
speed_trial_summary speed2
sl@0
   337
sl@0
   338
sl@0
   339
finish_test