| OLD | NEW |
| 1 # 2010 March 10 | 1 # 2010 March 10 |
| 2 # | 2 # |
| 3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
| 4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
| 5 # | 5 # |
| 6 # May you do good and not evil. | 6 # May you do good and not evil. |
| 7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
| 8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
| 9 # | 9 # |
| 10 #*********************************************************************** | 10 #*********************************************************************** |
| 11 # | 11 # |
| 12 # Tests for the sqlite3_db_status() function | 12 # Tests for the sqlite3_db_status() function |
| 13 # | 13 # |
| 14 | 14 |
| 15 set testdir [file dirname $argv0] | 15 set testdir [file dirname $argv0] |
| 16 source $testdir/tester.tcl | 16 source $testdir/tester.tcl |
| 17 | 17 |
| 18 ifcapable !compound { |
| 19 finish_test |
| 20 return |
| 21 } |
| 22 |
| 18 # Memory statistics must be enabled for this test. | 23 # Memory statistics must be enabled for this test. |
| 19 db close | 24 db close |
| 20 sqlite3_shutdown | 25 sqlite3_shutdown |
| 21 sqlite3_config_memstatus 1 | 26 sqlite3_config_memstatus 1 |
| 22 sqlite3_initialize | 27 sqlite3_initialize |
| 23 sqlite3 db test.db | 28 sqlite3 db test.db |
| 24 | 29 |
| 25 | 30 |
| 26 # Make sure sqlite3_db_config() and sqlite3_db_status are working. | 31 # Make sure sqlite3_db_config() and sqlite3_db_status are working. |
| 27 # | 32 # |
| (...skipping 21 matching lines...) Expand all Loading... |
| 49 lindex [sqlite3_db_status db SQLITE_DBSTATUS_CACHE_USED 0] 1 | 54 lindex [sqlite3_db_status db SQLITE_DBSTATUS_CACHE_USED 0] 1 |
| 50 } [expr {$BASESZ + 10*$PAGESZ}] | 55 } [expr {$BASESZ + 10*$PAGESZ}] |
| 51 | 56 |
| 52 | 57 |
| 53 proc lookaside {db} { | 58 proc lookaside {db} { |
| 54 expr { $::lookaside_buffer_size * | 59 expr { $::lookaside_buffer_size * |
| 55 [lindex [sqlite3_db_status $db SQLITE_DBSTATUS_LOOKASIDE_USED 0] 1] | 60 [lindex [sqlite3_db_status $db SQLITE_DBSTATUS_LOOKASIDE_USED 0] 1] |
| 56 } | 61 } |
| 57 } | 62 } |
| 58 | 63 |
| 64 ifcapable stat4||stat3 { |
| 65 set STAT3 1 |
| 66 } else { |
| 67 set STAT3 0 |
| 68 } |
| 69 |
| 70 ifcapable malloc_usable_size { |
| 71 finish_test |
| 72 return |
| 73 } |
| 74 |
| 59 #--------------------------------------------------------------------------- | 75 #--------------------------------------------------------------------------- |
| 60 # Run the dbstatus-2 and dbstatus-3 tests with several of different | 76 # Run the dbstatus-2 and dbstatus-3 tests with several of different |
| 61 # lookaside buffer sizes. | 77 # lookaside buffer sizes. |
| 62 # | 78 # |
| 63 foreach ::lookaside_buffer_size {0 64 120} { | 79 foreach ::lookaside_buffer_size {0 64 120} { |
| 64 | 80 |
| 65 # Do not run any of these tests if there is SQL configured to run | 81 # Do not run any of these tests if there is SQL configured to run |
| 66 # as part of the [sqlite3] command. This prevents the script from | 82 # as part of the [sqlite3] command. This prevents the script from |
| 67 # configuring the size of the lookaside buffer after [sqlite3] has | 83 # configuring the size of the lookaside buffer after [sqlite3] has |
| 68 # returned. | 84 # returned. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 CREATE TRIGGER AFTER INSERT ON t1 BEGIN | 127 CREATE TRIGGER AFTER INSERT ON t1 BEGIN |
| 112 INSERT INTO t2 VALUES(new.a, new.b); | 128 INSERT INTO t2 VALUES(new.a, new.b); |
| 113 SELECT * FROM t1, t2 WHERE a=c AND b=d GROUP BY b HAVING a>5 ORDER BY a; | 129 SELECT * FROM t1, t2 WHERE a=c AND b=d GROUP BY b HAVING a>5 ORDER BY a; |
| 114 END; | 130 END; |
| 115 } | 131 } |
| 116 5 { | 132 5 { |
| 117 CREATE TABLE t1(a, b); | 133 CREATE TABLE t1(a, b); |
| 118 CREATE TABLE t2(c, d); | 134 CREATE TABLE t2(c, d); |
| 119 CREATE VIEW v1 AS SELECT * FROM t1 UNION SELECT * FROM t2; | 135 CREATE VIEW v1 AS SELECT * FROM t1 UNION SELECT * FROM t2; |
| 120 } | 136 } |
| 121 6 { | 137 6k { |
| 122 CREATE TABLE t1(a, b); | 138 CREATE TABLE t1(a, b); |
| 123 CREATE INDEX i1 ON t1(a); | 139 CREATE INDEX i1 ON t1(a); |
| 124 CREATE INDEX i2 ON t1(a,b); | 140 CREATE INDEX i2 ON t1(a,b); |
| 125 CREATE INDEX i3 ON t1(b,b); | 141 CREATE INDEX i3 ON t1(b,b); |
| 126 INSERT INTO t1 VALUES(randomblob(20), randomblob(25)); | 142 INSERT INTO t1 VALUES(randomblob(20), randomblob(25)); |
| 127 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; | 143 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; |
| 128 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; | 144 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; |
| 129 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; | 145 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1; |
| 130 ANALYZE; | 146 ANALYZE; |
| 131 } | 147 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 148 } | 164 } |
| 149 8x { | 165 8x { |
| 150 CREATE TABLE t1(a, b, UNIQUE(a, b)); | 166 CREATE TABLE t1(a, b, UNIQUE(a, b)); |
| 151 CREATE VIRTUAL TABLE t2 USING echo(t1); | 167 CREATE VIRTUAL TABLE t2 USING echo(t1); |
| 152 } | 168 } |
| 153 } { | 169 } { |
| 154 set tn "$::lookaside_buffer_size-$tn" | 170 set tn "$::lookaside_buffer_size-$tn" |
| 155 | 171 |
| 156 # Step 1. | 172 # Step 1. |
| 157 db close | 173 db close |
| 158 file delete -force test.db | 174 forcedelete test.db |
| 159 sqlite3 db test.db | 175 sqlite3 db test.db |
| 160 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500 | 176 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500 |
| 161 db cache size 0 | 177 db cache size 0 |
| 162 | 178 |
| 163 catch { register_echo_module db } | 179 catch { register_echo_module db } |
| 164 ifcapable !vtab { if {[string match *x $tn]} continue } | 180 ifcapable !vtab { if {[string match *x $tn]} continue } |
| 165 | 181 |
| 166 # Step 2. | 182 # Step 2. |
| 167 execsql $schema | 183 execsql $schema |
| 168 set nAlloc1 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1] | 184 set nAlloc1 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1] |
| (...skipping 12 matching lines...) Expand all Loading... |
| 181 incr nAlloc3 [lookaside db] | 197 incr nAlloc3 [lookaside db] |
| 182 set nSchema3 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1] | 198 set nSchema3 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1] |
| 183 | 199 |
| 184 # Step 5. | 200 # Step 5. |
| 185 drop_all_tables | 201 drop_all_tables |
| 186 set nAlloc4 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1] | 202 set nAlloc4 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1] |
| 187 incr nAlloc4 [lookaside db] | 203 incr nAlloc4 [lookaside db] |
| 188 set nSchema4 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1] | 204 set nSchema4 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1] |
| 189 set nFree [expr {$nAlloc1-$nAlloc2}] | 205 set nFree [expr {$nAlloc1-$nAlloc2}] |
| 190 | 206 |
| 207 # Tests for which the test name ends in an "k" report slightly less |
| 208 # memory than is actually freed when all schema items are finalized. |
| 209 # This is because memory allocated by KeyInfo objects is no longer |
| 210 # counted as "schema memory". |
| 211 # |
| 191 # Tests for which the test name ends in an "x" report slightly less | 212 # Tests for which the test name ends in an "x" report slightly less |
| 192 # memory than is actually freed when all schema items are finalized. | 213 # memory than is actually freed when all schema items are finalized. |
| 193 # This is because memory allocated by virtual table implementations | 214 # This is because memory allocated by virtual table implementations |
| 194 # for any reason is not counted as "schema memory". | 215 # for any reason is not counted as "schema memory". |
| 195 # | 216 # |
| 196 # Additionally, in auto-vacuum mode, dropping tables and indexes causes | 217 # Additionally, in auto-vacuum mode, dropping tables and indexes causes |
| 197 # the page-cache to shrink. So the amount of memory freed is always | 218 # the page-cache to shrink. So the amount of memory freed is always |
| 198 # much greater than just that reported by DBSTATUS_SCHEMA_USED in this | 219 # much greater than just that reported by DBSTATUS_SCHEMA_USED in this |
| 199 # case. | 220 # case. |
| 200 # | 221 # |
| 201 if {[string match *x $tn] || $AUTOVACUUM} { | 222 # Some of the memory used for sqlite_stat4 is unaccounted for by |
| 223 # dbstatus. |
| 224 # |
| 225 # Finally, on osx the estimate of memory used by the schema may be |
| 226 # slightly low. |
| 227 # |
| 228 if {[string match *k $tn] |
| 229 || [string match *x $tn] || $AUTOVACUUM |
| 230 || ([string match *y $tn] && $STAT3) |
| 231 || ($::tcl_platform(os) == "Darwin") |
| 232 } { |
| 202 do_test dbstatus-2.$tn.ax { expr {($nSchema1-$nSchema2)<=$nFree} } 1 | 233 do_test dbstatus-2.$tn.ax { expr {($nSchema1-$nSchema2)<=$nFree} } 1 |
| 203 } else { | 234 } else { |
| 204 do_test dbstatus-2.$tn.a { expr {$nSchema1-$nSchema2} } $nFree | 235 do_test dbstatus-2.$tn.a { expr {$nSchema1-$nSchema2} } $nFree |
| 205 } | 236 } |
| 206 | 237 |
| 207 do_test dbstatus-2.$tn.b { list $nAlloc1 $nSchema1 } "$nAlloc3 $nSchema3" | 238 do_test dbstatus-2.$tn.b { list $nAlloc1 $nSchema1 } "$nAlloc3 $nSchema3" |
| 208 do_test dbstatus-2.$tn.c { list $nAlloc2 $nSchema2 } "$nAlloc4 $nSchema4" | 239 do_test dbstatus-2.$tn.c { list $nAlloc2 $nSchema2 } "$nAlloc4 $nSchema4" |
| 209 } | 240 } |
| 210 | 241 |
| 211 #------------------------------------------------------------------------- | 242 #------------------------------------------------------------------------- |
| 212 # Tests for SQLITE_DBSTATUS_STMT_USED. | 243 # Tests for SQLITE_DBSTATUS_STMT_USED. |
| 213 # | 244 # |
| 214 # Each test in the following block works as follows. Each test uses a | 245 # Each test in the following block works as follows. Each test uses a |
| 215 # different database schema. | 246 # different database schema. |
| 216 # | 247 # |
| 217 # 1. Open a connection to an empty database. Initialized the database | 248 # 1. Open a connection to an empty database. Initialized the database |
| 218 # schema. | 249 # schema. |
| 219 # | 250 # |
| 220 # 2. Prepare a bunch of SQL statements. Measure the total heap and | 251 # 2. Prepare a bunch of SQL statements. Measure the total heap and |
| 221 # lookaside memory allocated by SQLite, and the memory allocated | 252 # lookaside memory allocated by SQLite, and the memory allocated |
| 222 # for the prepared statements according to sqlite3_db_status(). | 253 # for the prepared statements according to sqlite3_db_status(). |
| 223 # | 254 # |
| 224 # 3. Finalize all prepared statements Measure the total memory | 255 # 3. Finalize all prepared statements. Measure the total memory |
| 225 # and the prepared statement memory again. | 256 # and the prepared statement memory again. |
| 226 # | 257 # |
| 227 # 4. Repeat step 2. | 258 # 4. Repeat step 2. |
| 228 # | 259 # |
| 229 # 5. Repeat step 3. | 260 # 5. Repeat step 3. |
| 230 # | 261 # |
| 231 # Then test that: | 262 # Then test that: |
| 232 # | 263 # |
| 233 # a) The difference in schema memory quantities in steps 2 and 3 is the | 264 # a) The difference in schema memory quantities in steps 2 and 3 is the |
| 234 # same as the difference in total memory in steps 2 and 3. | 265 # same as the difference in total memory in steps 2 and 3. |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 } { | 311 } { |
| 281 SELECT count(*) FROM t2; | 312 SELECT count(*) FROM t2; |
| 282 SELECT * FROM t2 WHERE b>5; | 313 SELECT * FROM t2 WHERE b>5; |
| 283 SELECT * FROM t2 WHERE b='abcdefg'; | 314 SELECT * FROM t2 WHERE b='abcdefg'; |
| 284 } | 315 } |
| 285 } { | 316 } { |
| 286 set tn "$::lookaside_buffer_size-$tn" | 317 set tn "$::lookaside_buffer_size-$tn" |
| 287 | 318 |
| 288 # Step 1. | 319 # Step 1. |
| 289 db close | 320 db close |
| 290 file delete -force test.db | 321 forcedelete test.db |
| 291 sqlite3 db test.db | 322 sqlite3 db test.db |
| 292 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500 | 323 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500 |
| 293 db cache size 1000 | 324 db cache size 1000 |
| 294 | 325 |
| 295 catch { register_echo_module db } | 326 catch { register_echo_module db } |
| 296 ifcapable !vtab { if {[string match *x $tn]} continue } | 327 ifcapable !vtab { if {[string match *x $tn]} continue } |
| 297 | 328 |
| 298 execsql $schema | 329 execsql $schema |
| 299 db cache flush | 330 db cache flush |
| 300 | 331 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 } else { | 371 } else { |
| 341 do_test dbstatus-3.$tn.b { expr $nStmt1==$nFree } {1} | 372 do_test dbstatus-3.$tn.b { expr $nStmt1==$nFree } {1} |
| 342 } | 373 } |
| 343 | 374 |
| 344 do_test dbstatus-3.$tn.c { list $nAlloc1 $nStmt1 } [list $nAlloc3 $nStmt3] | 375 do_test dbstatus-3.$tn.c { list $nAlloc1 $nStmt1 } [list $nAlloc3 $nStmt3] |
| 345 do_test dbstatus-3.$tn.d { list $nAlloc2 $nStmt2 } [list $nAlloc4 $nStmt4] | 376 do_test dbstatus-3.$tn.d { list $nAlloc2 $nStmt2 } [list $nAlloc4 $nStmt4] |
| 346 } | 377 } |
| 347 } | 378 } |
| 348 | 379 |
| 349 finish_test | 380 finish_test |
| OLD | NEW |