os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/memsubsys1.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 June 18
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
#
sl@0
    12
# This file contains tests of the memory allocation subsystem
sl@0
    13
#
sl@0
    14
# $Id: memsubsys1.test,v 1.12 2008/09/30 00:31:38 drh Exp $
sl@0
    15
sl@0
    16
set testdir [file dirname $argv0]
sl@0
    17
source $testdir/tester.tcl
sl@0
    18
sqlite3_reset_auto_extension
sl@0
    19
sl@0
    20
# This procedure constructs a new database in test.db.  It fills
sl@0
    21
# this database with many small records (enough to force multiple
sl@0
    22
# rebalance operations in the btree-layer and to require a large
sl@0
    23
# page cache), verifies correct results, then returns.
sl@0
    24
#
sl@0
    25
proc build_test_db {testname pragmas} {
sl@0
    26
  catch {db close}
sl@0
    27
  file delete -force test.db test.db-journal
sl@0
    28
  sqlite3 db test.db
sl@0
    29
  sqlite3_db_config_lookaside db 0 0 0
sl@0
    30
  db eval $pragmas
sl@0
    31
  db eval {
sl@0
    32
    CREATE TABLE t1(x, y);
sl@0
    33
    CREATE TABLE t2(a, b);
sl@0
    34
    CREATE INDEX i1 ON t1(x,y);
sl@0
    35
    INSERT INTO t1 VALUES(1, 100);
sl@0
    36
    INSERT INTO t1 VALUES(2, 200);
sl@0
    37
  }
sl@0
    38
  for {set i 2} {$i<5000} {incr i $i} {
sl@0
    39
    db eval {INSERT INTO t2 SELECT * FROM t1}
sl@0
    40
    db eval {INSERT INTO t1 SELECT a+$i, a+b*100 FROM t2}
sl@0
    41
    db eval {DELETE FROM t2}
sl@0
    42
  }
sl@0
    43
  do_test $testname.1 {
sl@0
    44
    db eval {SELECT count(*) FROM t1}
sl@0
    45
  } 8192
sl@0
    46
  integrity_check $testname.2
sl@0
    47
}
sl@0
    48
sl@0
    49
# Reset all of the highwater marks.
sl@0
    50
#
sl@0
    51
proc reset_highwater_marks {} {
sl@0
    52
  sqlite3_status SQLITE_STATUS_MEMORY_USED 1
sl@0
    53
  sqlite3_status SQLITE_STATUS_MALLOC_SIZE 1
sl@0
    54
  sqlite3_status SQLITE_STATUS_PAGECACHE_USED 1
sl@0
    55
  sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 1
sl@0
    56
  sqlite3_status SQLITE_STATUS_PAGECACHE_SIZE 1
sl@0
    57
  sqlite3_status SQLITE_STATUS_SCRATCH_USED 1
sl@0
    58
  sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 1
sl@0
    59
  sqlite3_status SQLITE_STATUS_SCRATCH_SIZE 1
sl@0
    60
  sqlite3_status SQLITE_STATUS_PARSER_STACK 1
sl@0
    61
}
sl@0
    62
sl@0
    63
set xtra_size 256
sl@0
    64
sl@0
    65
# Test 1:  Both PAGECACHE and SCRATCH are shut down.
sl@0
    66
#
sl@0
    67
db close
sl@0
    68
sqlite3_shutdown
sl@0
    69
sqlite3_config_lookaside 0 0
sl@0
    70
sqlite3_initialize
sl@0
    71
reset_highwater_marks
sl@0
    72
build_test_db memsubsys1-1 {PRAGMA page_size=1024}
sl@0
    73
do_test memsubsys1-1.3 {
sl@0
    74
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
    75
} 0
sl@0
    76
do_test memsubsys1-1.4 {
sl@0
    77
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
    78
} 0
sl@0
    79
set max_pagecache [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
sl@0
    80
#show_memstats
sl@0
    81
sl@0
    82
# Test 2:  Activate PAGECACHE with 20 pages
sl@0
    83
#
sl@0
    84
db close
sl@0
    85
sqlite3_shutdown
sl@0
    86
sqlite3_config_pagecache [expr 1024+$xtra_size] 20
sl@0
    87
sqlite3_initialize
sl@0
    88
reset_highwater_marks
sl@0
    89
build_test_db memsubsys1-2 {PRAGMA page_size=1024}
sl@0
    90
#show_memstats
sl@0
    91
do_test memsubsys1-2.3 {
sl@0
    92
  set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
sl@0
    93
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
    94
  expr {
sl@0
    95
    ($pg_used*1024 + $pg_ovfl) < $max_pagecache &&
sl@0
    96
    ($pg_used*(1024+$xtra_size) + $pg_ovfl) >= $max_pagecache
sl@0
    97
  }
sl@0
    98
} 1
sl@0
    99
do_test memsubsys1-2.4 {
sl@0
   100
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   101
} 19
sl@0
   102
do_test memsubsys1-2.5 {
sl@0
   103
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   104
} 0
sl@0
   105
sl@0
   106
# Test 3:  Activate PAGECACHE with 20 pages but use the wrong page size
sl@0
   107
# so that PAGECACHE is not used.
sl@0
   108
#
sl@0
   109
db close
sl@0
   110
sqlite3_shutdown
sl@0
   111
sqlite3_config_pagecache [expr 512+$xtra_size] 20
sl@0
   112
sqlite3_initialize
sl@0
   113
reset_highwater_marks
sl@0
   114
build_test_db memsubsys1-3.1 {PRAGMA page_size=1024}
sl@0
   115
#show_memstats
sl@0
   116
do_test memsubsys1-3.1.3 {
sl@0
   117
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   118
} 0
sl@0
   119
do_test memsubsys1-3.1.4 {
sl@0
   120
  set overflow [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
sl@0
   121
} $max_pagecache
sl@0
   122
do_test memsubsys1-3.1.5 {
sl@0
   123
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   124
} 0
sl@0
   125
db close
sl@0
   126
sqlite3_shutdown
sl@0
   127
sqlite3_config_pagecache [expr 2048+$xtra_size] 20
sl@0
   128
sqlite3_initialize
sl@0
   129
reset_highwater_marks
sl@0
   130
build_test_db memsubsys1-3.2 {PRAGMA page_size=2048}
sl@0
   131
#show_memstats
sl@0
   132
do_test memsubsys1-3.2.3 {
sl@0
   133
  db eval {PRAGMA page_size}
sl@0
   134
} 2048
sl@0
   135
do_test memsubsys1-3.2.4 {
sl@0
   136
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   137
} 19
sl@0
   138
do_test memsubsys1-3.2.5 {
sl@0
   139
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   140
} 0
sl@0
   141
sl@0
   142
# Test 4:  Activate both PAGECACHE and SCRATCH.
sl@0
   143
#
sl@0
   144
db close
sl@0
   145
sqlite3_shutdown
sl@0
   146
sqlite3_config_pagecache [expr 1024+$xtra_size] 50
sl@0
   147
sqlite3_config_scratch 6000 2
sl@0
   148
sqlite3_initialize
sl@0
   149
reset_highwater_marks
sl@0
   150
build_test_db memsubsys1-4 {PRAGMA page_size=1024}
sl@0
   151
#show_memstats
sl@0
   152
do_test memsubsys1-4.3 {
sl@0
   153
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   154
} 49
sl@0
   155
do_test memsubsys1-4.4 {
sl@0
   156
  set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
sl@0
   157
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   158
  expr {
sl@0
   159
    ($pg_used*1024 + $pg_ovfl) < $max_pagecache &&
sl@0
   160
    ($pg_used*(1024+$xtra_size) + $pg_ovfl) >= $max_pagecache
sl@0
   161
  }
sl@0
   162
} 1
sl@0
   163
do_test memsubsys1-4.5 {
sl@0
   164
  set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
sl@0
   165
  expr {$maxreq<7000}
sl@0
   166
} 1
sl@0
   167
do_test memsubsys1-4.6 {
sl@0
   168
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   169
} 1
sl@0
   170
sl@0
   171
# Test 5:  Activate both PAGECACHE and SCRATCH.  But make the page size
sl@0
   172
# such that the SCRATCH allocations are too small.
sl@0
   173
#
sl@0
   174
db close
sl@0
   175
sqlite3_shutdown
sl@0
   176
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
sl@0
   177
sqlite3_config_scratch 6000 2
sl@0
   178
sqlite3_initialize
sl@0
   179
reset_highwater_marks
sl@0
   180
build_test_db memsubsys1-5 {PRAGMA page_size=4096}
sl@0
   181
#show_memstats
sl@0
   182
do_test memsubsys1-5.3 {
sl@0
   183
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   184
} 23
sl@0
   185
do_test memsubsys1-5.4 {
sl@0
   186
  set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
sl@0
   187
  expr {$maxreq>4096}
sl@0
   188
} 1
sl@0
   189
do_test memsubsys1-5.5 {
sl@0
   190
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   191
} 0
sl@0
   192
do_test memsubsys1-5.6 {
sl@0
   193
  set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
sl@0
   194
  expr {$s_ovfl>6000}
sl@0
   195
} 1
sl@0
   196
sl@0
   197
# Test 6:  Activate both PAGECACHE and SCRATCH with a 4k page size.
sl@0
   198
# Make it so that SCRATCH is large enough
sl@0
   199
#
sl@0
   200
db close
sl@0
   201
sqlite3_shutdown
sl@0
   202
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
sl@0
   203
sqlite3_config_scratch 25000 1
sl@0
   204
sqlite3_initialize
sl@0
   205
reset_highwater_marks
sl@0
   206
build_test_db memsubsys1-6 {PRAGMA page_size=4096}
sl@0
   207
#show_memstats
sl@0
   208
do_test memsubsys1-6.3 {
sl@0
   209
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   210
} 23
sl@0
   211
do_test memsubsys1-6.4 {
sl@0
   212
  set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
sl@0
   213
  expr {$maxreq>4096 && $maxreq<=(4096+$xtra_size)}
sl@0
   214
} 1
sl@0
   215
do_test memsubsys1-6.5 {
sl@0
   216
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   217
} 1
sl@0
   218
do_test memsubsys1-6.6 {
sl@0
   219
  set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
sl@0
   220
} 0
sl@0
   221
sl@0
   222
# Test 7:  Activate both PAGECACHE and SCRATCH with a 4k page size.
sl@0
   223
# Set cache_size small so that no PAGECACHE overflow occurs.  Verify
sl@0
   224
# that maximum allocation size is small.
sl@0
   225
#
sl@0
   226
db close
sl@0
   227
sqlite3_shutdown
sl@0
   228
sqlite3_config_pagecache [expr 4096+$xtra_size] 24
sl@0
   229
sqlite3_config_scratch 25000 1
sl@0
   230
sqlite3_initialize
sl@0
   231
reset_highwater_marks
sl@0
   232
build_test_db memsubsys1-7 {
sl@0
   233
  PRAGMA page_size=4096;
sl@0
   234
  PRAGMA cache_size=10;
sl@0
   235
  PRAGMA temp_store=memory;
sl@0
   236
}
sl@0
   237
#show_memstats
sl@0
   238
do_test memsubsys1-7.3 {
sl@0
   239
  set pg_used [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_USED 0] 2]
sl@0
   240
  expr {$pg_used<24}
sl@0
   241
} 1
sl@0
   242
do_test memsubsys1-7.4 {
sl@0
   243
  set pg_ovfl [lindex [sqlite3_status SQLITE_STATUS_PAGECACHE_OVERFLOW 0] 2]
sl@0
   244
} 0
sl@0
   245
do_test memsubsys1-7.5 {
sl@0
   246
  set maxreq [lindex [sqlite3_status SQLITE_STATUS_MALLOC_SIZE 0] 2]
sl@0
   247
  expr {$maxreq<4000}
sl@0
   248
} 1
sl@0
   249
do_test memsubsys1-7.6 {
sl@0
   250
  set s_used [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_USED 0] 2]
sl@0
   251
} 1
sl@0
   252
do_test memsubsys1-7.7 {
sl@0
   253
  set s_ovfl [lindex [sqlite3_status SQLITE_STATUS_SCRATCH_OVERFLOW 0] 2]
sl@0
   254
} 0
sl@0
   255
sl@0
   256
db close
sl@0
   257
sqlite3_shutdown
sl@0
   258
sqlite3_config_pagecache 0 0
sl@0
   259
sqlite3_config_scratch 0 0
sl@0
   260
sqlite3_config_lookaside 100 500
sl@0
   261
sqlite3_initialize
sl@0
   262
autoinstall_test_functions
sl@0
   263
finish_test