os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/speed3.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
# 2007 May 17
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 testing that the overflow-page related
sl@0
    13
# enhancements added after version 3.3.17 speed things up.
sl@0
    14
#
sl@0
    15
# $Id: speed3.test,v 1.5 2007/10/09 08:29:33 danielk1977 Exp $
sl@0
    16
#
sl@0
    17
sl@0
    18
#---------------------------------------------------------------------
sl@0
    19
# Test plan:
sl@0
    20
#
sl@0
    21
# If auto-vacuum is enabled for the database, the following cases
sl@0
    22
# should show performance improvement with respect to 3.3.17.
sl@0
    23
#
sl@0
    24
#   + When deleting rows that span overflow pages. This is faster
sl@0
    25
#     because the overflow pages no longer need to be read before
sl@0
    26
#     they can be moved to the free list (test cases speed3-1.X). 
sl@0
    27
#
sl@0
    28
#   + When reading a column value stored on an overflow page that
sl@0
    29
#     is not the first overflow page for the row. The improvement
sl@0
    30
#     in this case is because the overflow pages between the tree
sl@0
    31
#     page and the overflow page containing the value do not have
sl@0
    32
#     to be read (test cases speed3-2.X).
sl@0
    33
#
sl@0
    34
sl@0
    35
set testdir [file dirname $argv0]
sl@0
    36
source $testdir/tester.tcl
sl@0
    37
sl@0
    38
ifcapable !tclvar||!attach {
sl@0
    39
  finish_test
sl@0
    40
  return
sl@0
    41
}
sl@0
    42
sl@0
    43
speed_trial_init speed1
sl@0
    44
sl@0
    45
# Set a uniform random seed
sl@0
    46
expr srand(0)
sl@0
    47
sl@0
    48
set ::NROW 1000
sl@0
    49
sl@0
    50
# The number_name procedure below converts its argment (an integer)
sl@0
    51
# into a string which is the English-language name for that number.
sl@0
    52
#
sl@0
    53
# Example:
sl@0
    54
#
sl@0
    55
#     puts [number_name 123]   ->  "one hundred twenty three"
sl@0
    56
#
sl@0
    57
set ones {zero one two three four five six seven eight nine
sl@0
    58
          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
sl@0
    59
          eighteen nineteen}
sl@0
    60
set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
sl@0
    61
proc number_name {n} {
sl@0
    62
  if {$n>=1000} {
sl@0
    63
    set txt "[number_name [expr {$n/1000}]] thousand"
sl@0
    64
    set n [expr {$n%1000}]
sl@0
    65
  } else {
sl@0
    66
    set txt {}
sl@0
    67
  }
sl@0
    68
  if {$n>=100} {
sl@0
    69
    append txt " [lindex $::ones [expr {$n/100}]] hundred"
sl@0
    70
    set n [expr {$n%100}]
sl@0
    71
  }
sl@0
    72
  if {$n>=20} {
sl@0
    73
    append txt " [lindex $::tens [expr {$n/10}]]"
sl@0
    74
    set n [expr {$n%10}]
sl@0
    75
  }
sl@0
    76
  if {$n>0} {
sl@0
    77
    append txt " [lindex $::ones $n]"
sl@0
    78
  }
sl@0
    79
  set txt [string trim $txt]
sl@0
    80
  if {$txt==""} {set txt zero}
sl@0
    81
  return $txt
sl@0
    82
}
sl@0
    83
sl@0
    84
proc populate_t1 {db} {
sl@0
    85
  $db transaction {
sl@0
    86
    for {set ii 0} {$ii < $::NROW} {incr ii} {
sl@0
    87
      set N [number_name $ii]
sl@0
    88
      set repeats [expr {(10000/[string length $N])+1}]
sl@0
    89
      set text [string range [string repeat $N $repeats] 0 10000]
sl@0
    90
      $db eval {INSERT INTO main.t1 VALUES($ii, $text, $ii)}
sl@0
    91
    }
sl@0
    92
    $db eval {INSERT INTO aux.t1 SELECT * FROM main.t1}
sl@0
    93
  }
sl@0
    94
}
sl@0
    95
sl@0
    96
sl@0
    97
proc io_log {db} {
sl@0
    98
  db_enter db
sl@0
    99
  array set stats1 [btree_pager_stats [btree_from_db db]]
sl@0
   100
  array set stats2 [btree_pager_stats [btree_from_db db 2]]
sl@0
   101
  db_leave db
sl@0
   102
# puts "1: [array get stats1]"
sl@0
   103
# puts "2: [array get stats2]"
sl@0
   104
  puts "Incrvacuum: Read $stats1(read), wrote $stats1(write)"
sl@0
   105
  puts "Normal    : Read $stats2(read), wrote $stats2(write)"
sl@0
   106
}
sl@0
   107
sl@0
   108
proc overflow_report {db} {
sl@0
   109
  set bt [btree_from_db db]
sl@0
   110
  set csr [btree_cursor $bt 3 0]
sl@0
   111
sl@0
   112
  for {btree_first $csr} {![btree_eof $csr]} {btree_next $csr} {
sl@0
   113
    puts "[btree_ovfl_info $bt $csr]"
sl@0
   114
  }
sl@0
   115
sl@0
   116
  btree_close_cursor $csr
sl@0
   117
  
sl@0
   118
}
sl@0
   119
sl@0
   120
proc reset_db {} {
sl@0
   121
  db close
sl@0
   122
  sqlite3 db test.db
sl@0
   123
  db eval { 
sl@0
   124
    PRAGMA main.cache_size = 200000;
sl@0
   125
    PRAGMA main.auto_vacuum = 'incremental';
sl@0
   126
    ATTACH 'test2.db' AS 'aux'; 
sl@0
   127
    PRAGMA aux.auto_vacuum = 'none';
sl@0
   128
  }
sl@0
   129
}
sl@0
   130
sl@0
   131
file delete -force test2.db test2.db-journal
sl@0
   132
reset_db
sl@0
   133
sl@0
   134
# Set up a database in auto-vacuum mode and create a database schema.
sl@0
   135
#
sl@0
   136
do_test speed3-0.1 {
sl@0
   137
  execsql {
sl@0
   138
    CREATE TABLE main.t1(a INTEGER, b TEXT, c INTEGER);
sl@0
   139
  }
sl@0
   140
  execsql {
sl@0
   141
    SELECT name FROM sqlite_master ORDER BY 1;
sl@0
   142
  }
sl@0
   143
} {t1}
sl@0
   144
do_test speed3-0.2 {
sl@0
   145
  execsql {
sl@0
   146
    CREATE TABLE aux.t1(a INTEGER, b TEXT, c INTEGER);
sl@0
   147
  }
sl@0
   148
  execsql {
sl@0
   149
    SELECT name FROM aux.sqlite_master ORDER BY 1;
sl@0
   150
  }
sl@0
   151
} {t1}
sl@0
   152
do_test speed3-0.3 {
sl@0
   153
  populate_t1 db
sl@0
   154
  execsql {
sl@0
   155
    SELECT count(*) FROM main.t1;
sl@0
   156
    SELECT count(*) FROM aux.t1;
sl@0
   157
  }
sl@0
   158
} "$::NROW $::NROW"
sl@0
   159
do_test speed3-0.4 {
sl@0
   160
  execsql {
sl@0
   161
    PRAGMA main.auto_vacuum;
sl@0
   162
    PRAGMA aux.auto_vacuum;
sl@0
   163
  }
sl@0
   164
} {2 0}
sl@0
   165
sl@0
   166
# Delete all content in a table, one row at a time.
sl@0
   167
#
sl@0
   168
#io_log db
sl@0
   169
#overflow_report db
sl@0
   170
reset_db
sl@0
   171
speed_trial speed3-1.incrvacuum $::NROW row {DELETE FROM main.t1 WHERE 1}
sl@0
   172
speed_trial speed3-1.normal     $::NROW row {DELETE FROM aux.t1 WHERE 1}
sl@0
   173
io_log db
sl@0
   174
sl@0
   175
# Select the "C" column (located at the far end of the overflow 
sl@0
   176
# chain) from each table row.
sl@0
   177
#
sl@0
   178
#db eval {PRAGMA incremental_vacuum(500000)}
sl@0
   179
populate_t1 db
sl@0
   180
#overflow_report db
sl@0
   181
reset_db
sl@0
   182
speed_trial speed3-2.incrvacuum $::NROW row {SELECT c FROM main.t1}
sl@0
   183
speed_trial speed3-2.normal     $::NROW row {SELECT c FROM aux.t1}
sl@0
   184
io_log db
sl@0
   185
sl@0
   186
finish_test