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