os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed1p.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
# 2008 March 21
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
# This is a copy of speed1.test modified to user prepared statements.
sl@0
    15
#
sl@0
    16
# $Id: speed1p.test,v 1.5 2008/07/30 13:15:46 drh Exp $
sl@0
    17
#
sl@0
    18
sl@0
    19
sqlite3_shutdown
sl@0
    20
#sqlite3_config_scratch 29000 1
sl@0
    21
sqlite3_config_lookaside 2048 300
sl@0
    22
#sqlite3_config_pagecache 1024 11000
sl@0
    23
set testdir [file dirname $argv0]
sl@0
    24
source $testdir/tester.tcl
sl@0
    25
speed_trial_init speed1
sl@0
    26
sl@0
    27
# Set a uniform random seed
sl@0
    28
expr srand(0)
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 speed1p-1.0 {
sl@0
    67
  execsql {
sl@0
    68
    PRAGMA page_size=1024;
sl@0
    69
    PRAGMA cache_size=500;
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 list {}
sl@0
    85
for {set i 1} {$i<=50000} {incr i} {
sl@0
    86
  set r [expr {int(rand()*500000)}]
sl@0
    87
  set x [number_name $r]
sl@0
    88
  lappend list $i $r $x
sl@0
    89
}
sl@0
    90
set script {
sl@0
    91
  foreach {i r x} $::list {
sl@0
    92
    db eval {INSERT INTO t1 VALUES($i,$r,$x)}
sl@0
    93
  }
sl@0
    94
}
sl@0
    95
db eval BEGIN
sl@0
    96
speed_trial_tcl speed1p-insert1 50000 row $script
sl@0
    97
db eval COMMIT
sl@0
    98
sl@0
    99
# 50000 INSERTs on an indexed table
sl@0
   100
#
sl@0
   101
set list {}
sl@0
   102
for {set i 1} {$i<=50000} {incr i} {
sl@0
   103
  set r [expr {int(rand()*500000)}]
sl@0
   104
  set x [number_name $r]
sl@0
   105
  lappend list $i $r $x
sl@0
   106
}
sl@0
   107
set script {
sl@0
   108
  foreach {i r x} $::list {
sl@0
   109
    db eval {INSERT INTO t2 VALUES($i,$r,$x)}
sl@0
   110
  }
sl@0
   111
}
sl@0
   112
db eval BEGIN
sl@0
   113
speed_trial_tcl speed1p-insert2 50000 row $script
sl@0
   114
db eval COMMIT
sl@0
   115
sl@0
   116
sl@0
   117
sl@0
   118
# 50 SELECTs on an integer comparison.  There is no index so
sl@0
   119
# a full table scan is required.
sl@0
   120
#
sl@0
   121
set list {}
sl@0
   122
for {set i 0} {$i<50} {incr i} {
sl@0
   123
  set lwr [expr {$i*100}]
sl@0
   124
  set upr [expr {($i+10)*100}]
sl@0
   125
  lappend list $lwr $upr
sl@0
   126
}
sl@0
   127
set script {
sl@0
   128
  foreach {lwr upr} $::list {
sl@0
   129
    db eval  {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
sl@0
   130
  }
sl@0
   131
}
sl@0
   132
db eval BEGIN
sl@0
   133
speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
sl@0
   134
db eval COMMIT
sl@0
   135
sl@0
   136
# 50 SELECTs on an LIKE comparison.  There is no index so a full
sl@0
   137
# table scan is required.
sl@0
   138
#
sl@0
   139
set list {}
sl@0
   140
for {set i 0} {$i<50} {incr i} {
sl@0
   141
  lappend list "%[number_name $i]%"
sl@0
   142
}
sl@0
   143
set script {
sl@0
   144
  foreach pattern $::list {
sl@0
   145
    db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
sl@0
   146
  }
sl@0
   147
}
sl@0
   148
db eval BEGIN
sl@0
   149
speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
sl@0
   150
db eval COMMIT
sl@0
   151
sl@0
   152
# Create indices
sl@0
   153
#
sl@0
   154
db eval BEGIN
sl@0
   155
speed_trial speed1p-createidx 150000 row {
sl@0
   156
  CREATE INDEX i1a ON t1(a);
sl@0
   157
  CREATE INDEX i1b ON t1(b);
sl@0
   158
  CREATE INDEX i1c ON t1(c);
sl@0
   159
}
sl@0
   160
db eval COMMIT
sl@0
   161
sl@0
   162
# 5000 SELECTs on an integer comparison where the integer is
sl@0
   163
# indexed.
sl@0
   164
#
sl@0
   165
set list {}
sl@0
   166
for {set i 0} {$i<5000} {incr i} {
sl@0
   167
  set lwr [expr {$i*100}]
sl@0
   168
  set upr [expr {($i+10)*100}]
sl@0
   169
  lappend list $lwr $upr
sl@0
   170
}
sl@0
   171
set script {
sl@0
   172
  foreach {lwr upr} $::list {
sl@0
   173
    db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
sl@0
   174
  }
sl@0
   175
}
sl@0
   176
db eval BEGIN
sl@0
   177
speed_trial_tcl speed1p-select3 5000 stmt $script
sl@0
   178
db eval COMMIT
sl@0
   179
sl@0
   180
# 100000 random SELECTs against rowid.
sl@0
   181
#
sl@0
   182
set list {}
sl@0
   183
for {set i 1} {$i<=100000} {incr i} {
sl@0
   184
  set id [expr {int(rand()*50000)+1}]
sl@0
   185
  lappend list $id
sl@0
   186
}
sl@0
   187
set script {
sl@0
   188
  foreach id $::list {
sl@0
   189
    db eval {SELECT c FROM t1 WHERE rowid=$id}
sl@0
   190
  }
sl@0
   191
}
sl@0
   192
db eval BEGIN
sl@0
   193
speed_trial_tcl speed1p-select4 100000 row $script
sl@0
   194
db eval COMMIT
sl@0
   195
sl@0
   196
# 100000 random SELECTs against a unique indexed column.
sl@0
   197
#
sl@0
   198
set list {}
sl@0
   199
for {set i 1} {$i<=100000} {incr i} {
sl@0
   200
  set id [expr {int(rand()*50000)+1}]
sl@0
   201
  lappend list $id
sl@0
   202
}
sl@0
   203
set script {
sl@0
   204
  foreach id $::list {
sl@0
   205
    db eval {SELECT c FROM t1 WHERE a=$id}
sl@0
   206
  }
sl@0
   207
}
sl@0
   208
db eval BEGIN
sl@0
   209
speed_trial_tcl speed1p-select5 100000 row $script
sl@0
   210
db eval COMMIT
sl@0
   211
sl@0
   212
# 50000 random SELECTs against an indexed column text column
sl@0
   213
#
sl@0
   214
set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
sl@0
   215
set script {
sl@0
   216
  foreach c $::list {
sl@0
   217
    db eval {SELECT c FROM t1 WHERE c=$c}
sl@0
   218
  }
sl@0
   219
}
sl@0
   220
db eval BEGIN
sl@0
   221
speed_trial_tcl speed1p-select6 50000 row $script
sl@0
   222
db eval COMMIT
sl@0
   223
sl@0
   224
sl@0
   225
# Vacuum
sl@0
   226
speed_trial speed1p-vacuum 100000 row VACUUM
sl@0
   227
sl@0
   228
# 5000 updates of ranges where the field being compared is indexed.
sl@0
   229
#
sl@0
   230
set list {}
sl@0
   231
for {set i 0} {$i<5000} {incr i} {
sl@0
   232
  set lwr [expr {$i*2}]
sl@0
   233
  set upr [expr {($i+1)*2}]
sl@0
   234
  lappend list $lwr $upr
sl@0
   235
}
sl@0
   236
set script {
sl@0
   237
  foreach {lwr upr} $::list {
sl@0
   238
    db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
sl@0
   239
  }
sl@0
   240
}
sl@0
   241
db eval BEGIN
sl@0
   242
speed_trial_tcl speed1p-update1 5000 stmt $script
sl@0
   243
db eval COMMIT
sl@0
   244
sl@0
   245
# 50000 single-row updates.  An index is used to find the row quickly.
sl@0
   246
#
sl@0
   247
set list {}
sl@0
   248
for {set i 0} {$i<50000} {incr i} {
sl@0
   249
  set r [expr {int(rand()*500000)}]
sl@0
   250
  lappend list $i $r
sl@0
   251
}
sl@0
   252
set script {
sl@0
   253
  foreach {i r} $::list {
sl@0
   254
    db eval {UPDATE t1 SET b=$r WHERE a=$i}
sl@0
   255
  }
sl@0
   256
}
sl@0
   257
db eval BEGIN
sl@0
   258
speed_trial_tcl speed1p-update2 50000 row $script
sl@0
   259
db eval COMMIT
sl@0
   260
sl@0
   261
# 1 big text update that touches every row in the table.
sl@0
   262
#
sl@0
   263
speed_trial speed1p-update3 50000 row {
sl@0
   264
  UPDATE t1 SET c=a;
sl@0
   265
}
sl@0
   266
sl@0
   267
# Many individual text updates.  Each row in the table is
sl@0
   268
# touched through an index.
sl@0
   269
#
sl@0
   270
set list {}
sl@0
   271
for {set i 1} {$i<=50000} {incr i} {
sl@0
   272
  set r [expr {int(rand()*500000)}]
sl@0
   273
  lappend list $i [number_name $r]
sl@0
   274
}
sl@0
   275
set script {
sl@0
   276
  foreach {i x} $::list {
sl@0
   277
    db eval {UPDATE t1 SET c=$x WHERE a=$i}
sl@0
   278
  }
sl@0
   279
}
sl@0
   280
db eval BEGIN
sl@0
   281
speed_trial_tcl speed1p-update4 50000 row $script
sl@0
   282
db eval COMMIT
sl@0
   283
sl@0
   284
# Delete all content in a table.
sl@0
   285
#
sl@0
   286
speed_trial speed1p-delete1 50000 row {DELETE FROM t1}
sl@0
   287
sl@0
   288
# Copy one table into another
sl@0
   289
#
sl@0
   290
speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   291
sl@0
   292
# Delete all content in a table, one row at a time.
sl@0
   293
#
sl@0
   294
speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
sl@0
   295
sl@0
   296
# Refill the table yet again
sl@0
   297
#
sl@0
   298
speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   299
sl@0
   300
# Drop the table and recreate it without its indices.
sl@0
   301
#
sl@0
   302
db eval BEGIN
sl@0
   303
speed_trial speed1p-drop1 50000 row {
sl@0
   304
   DROP TABLE t1;
sl@0
   305
   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
sl@0
   306
}
sl@0
   307
db eval COMMIT
sl@0
   308
sl@0
   309
# Refill the table yet again.  This copy should be faster because
sl@0
   310
# there are no indices to deal with.
sl@0
   311
#
sl@0
   312
speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
sl@0
   313
sl@0
   314
# Select 20000 rows from the table at random.
sl@0
   315
#
sl@0
   316
speed_trial speed1p-random1 50000 row {
sl@0
   317
  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
sl@0
   318
}
sl@0
   319
sl@0
   320
# Delete 20000 random rows from the table.
sl@0
   321
#
sl@0
   322
speed_trial speed1p-random-del1 20000 row {
sl@0
   323
  DELETE FROM t1 WHERE rowid IN
sl@0
   324
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   325
}
sl@0
   326
do_test speed1p-1.1 {
sl@0
   327
  db one {SELECT count(*) FROM t1}
sl@0
   328
} 30000
sl@0
   329
sl@0
   330
    
sl@0
   331
# Delete 20000 more rows at random from the table.
sl@0
   332
#
sl@0
   333
speed_trial speed1p-random-del2 20000 row {
sl@0
   334
  DELETE FROM t1 WHERE rowid IN
sl@0
   335
    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
sl@0
   336
}
sl@0
   337
do_test speed1p-1.2 {
sl@0
   338
  db one {SELECT count(*) FROM t1}
sl@0
   339
} 10000
sl@0
   340
speed_trial_summary speed1
sl@0
   341
sl@0
   342
finish_test