3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # Tests for the sqlite3_db_status() function
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
18 # Memory statistics must be enabled for this test.
21 sqlite3_config_memstatus 1
26 # Make sure sqlite3_db_config() and sqlite3_db_status are working.
28 unset -nocomplain PAGESZ
29 unset -nocomplain BASESZ
30 do_test dbstatus-1.1 {
36 set sz1 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_CACHE_USED 0] 1]
40 set sz2 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_CACHE_USED 0] 1]
41 set ::PAGESZ [expr {$sz2-$sz1}]
42 set ::BASESZ [expr {$sz1-$::PAGESZ}]
43 expr {$::PAGESZ>1024 && $::PAGESZ<1300}
45 do_test dbstatus-1.2 {
47 INSERT INTO t1 VALUES(zeroblob(9000));
49 lindex [sqlite3_db_status db SQLITE_DBSTATUS_CACHE_USED 0] 1
50 } [expr {$BASESZ + 10*$PAGESZ}]
54 expr { $::lookaside_buffer_size *
55 [lindex [sqlite3_db_status $db SQLITE_DBSTATUS_LOOKASIDE_USED 0] 1]
59 #---------------------------------------------------------------------------
60 # Run the dbstatus-2 and dbstatus-3 tests with several of different
61 # lookaside buffer sizes.
63 foreach ::lookaside_buffer_size {0 64 120} {
65 # 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
67 # configuring the size of the lookaside buffer after [sqlite3] has
69 if {[presql] != ""} break
71 #-------------------------------------------------------------------------
72 # Tests for SQLITE_DBSTATUS_SCHEMA_USED.
74 # Each test in the following block works as follows. Each test uses a
75 # different database schema.
77 # 1. Open a connection to an empty database. Disable statement caching.
79 # 2. Execute the SQL to create the database schema. Measure the total
80 # heap and lookaside memory allocated by SQLite, and the memory
81 # allocated for the database schema according to sqlite3_db_status().
83 # 3. Drop all tables in the database schema. Measure the total memory
84 # and the schema memory again.
92 # a) The difference in schema memory quantities in steps 2 and 3 is the
93 # same as the difference in total memory in steps 2 and 3.
95 # b) Step 4 reports the same amount of schema and total memory used as
98 # c) Step 5 reports the same amount of schema and total memory used as
101 foreach {tn schema} {
102 1 { CREATE TABLE t1(a, b) }
103 2 { CREATE TABLE t1(a PRIMARY KEY, b REFERENCES t1, c UNIQUE) }
105 CREATE TABLE t1(a, b);
106 CREATE INDEX i1 ON t1(a, b);
109 CREATE TABLE t1(a, b);
110 CREATE TABLE t2(c, d);
111 CREATE TRIGGER AFTER INSERT ON t1 BEGIN
112 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;
117 CREATE TABLE t1(a, b);
118 CREATE TABLE t2(c, d);
119 CREATE VIEW v1 AS SELECT * FROM t1 UNION SELECT * FROM t2;
122 CREATE TABLE t1(a, b);
123 CREATE INDEX i1 ON t1(a);
124 CREATE INDEX i2 ON t1(a,b);
125 CREATE INDEX i3 ON t1(b,b);
126 INSERT INTO t1 VALUES(randomblob(20), randomblob(25));
127 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1;
128 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1;
129 INSERT INTO t1 SELECT randomblob(20), randomblob(25) FROM t1;
133 CREATE TABLE t1(a, b);
134 CREATE TABLE t2(c, d);
140 SELECT c||b, d||a FROM t2 LEFT OUTER JOIN t1 GROUP BY c, d
143 CREATE TRIGGER tr1 INSTEAD OF INSERT ON v1 BEGIN
145 UPDATE t1 SET a=5, b=(SELECT c FROM t2);
150 CREATE TABLE t1(a, b, UNIQUE(a, b));
151 CREATE VIRTUAL TABLE t2 USING echo(t1);
154 set tn "$::lookaside_buffer_size-$tn"
158 file delete -force test.db
160 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500
163 catch { register_echo_module db }
164 ifcapable !vtab { if {[string match *x $tn]} continue }
168 set nAlloc1 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
169 incr nAlloc1 [lookaside db]
170 set nSchema1 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1]
174 set nAlloc2 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
175 incr nAlloc2 [lookaside db]
176 set nSchema2 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1]
180 set nAlloc3 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
181 incr nAlloc3 [lookaside db]
182 set nSchema3 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1]
186 set nAlloc4 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
187 incr nAlloc4 [lookaside db]
188 set nSchema4 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_SCHEMA_USED 0] 1]
189 set nFree [expr {$nAlloc1-$nAlloc2}]
191 # 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.
193 # This is because memory allocated by virtual table implementations
194 # for any reason is not counted as "schema memory".
196 # Additionally, in auto-vacuum mode, dropping tables and indexes causes
197 # 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
201 if {[string match *x $tn] || $AUTOVACUUM} {
202 do_test dbstatus-2.$tn.ax { expr {($nSchema1-$nSchema2)<=$nFree} } 1
204 do_test dbstatus-2.$tn.a { expr {$nSchema1-$nSchema2} } $nFree
207 do_test dbstatus-2.$tn.b { list $nAlloc1 $nSchema1 } "$nAlloc3 $nSchema3"
208 do_test dbstatus-2.$tn.c { list $nAlloc2 $nSchema2 } "$nAlloc4 $nSchema4"
211 #-------------------------------------------------------------------------
212 # Tests for SQLITE_DBSTATUS_STMT_USED.
214 # Each test in the following block works as follows. Each test uses a
215 # different database schema.
217 # 1. Open a connection to an empty database. Initialized the database
220 # 2. Prepare a bunch of SQL statements. Measure the total heap and
221 # lookaside memory allocated by SQLite, and the memory allocated
222 # for the prepared statements according to sqlite3_db_status().
224 # 3. Finalize all prepared statements Measure the total memory
225 # and the prepared statement memory again.
233 # 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.
236 # b) Step 4 reports the same amount of schema and total memory used as
239 # c) Step 5 reports the same amount of schema and total memory used as
242 foreach {tn schema statements} {
243 1 { CREATE TABLE t1(a, b) } {
245 INSERT INTO t1 VALUES(1, 2);
246 INSERT INTO t1 SELECT * FROM t1;
251 PRAGMA recursive_triggers = 1;
252 CREATE TABLE t1(a, b);
253 CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN (new.a>0) BEGIN
254 INSERT INTO t1 VALUES(new.a-1, new.b);
257 INSERT INTO t1 VALUES(5, 'x');
260 PRAGMA recursive_triggers = 1;
261 CREATE TABLE t1(a, b);
262 CREATE TABLE t2(a, b);
263 CREATE TRIGGER tr1 AFTER INSERT ON t1 WHEN (new.a>0) BEGIN
264 INSERT INTO t2 VALUES(new.a-1, new.b);
266 CREATE TRIGGER tr2 AFTER INSERT ON t1 WHEN (new.a>0) BEGIN
267 INSERT INTO t1 VALUES(new.a-1, new.b);
270 INSERT INTO t1 VALUES(10, 'x');
273 CREATE TABLE t1(a, b);
275 SELECT count(*) FROM t1 WHERE upper(a)='ABC';
278 CREATE TABLE t1(a, b UNIQUE);
279 CREATE VIRTUAL TABLE t2 USING echo(t1);
281 SELECT count(*) FROM t2;
282 SELECT * FROM t2 WHERE b>5;
283 SELECT * FROM t2 WHERE b='abcdefg';
286 set tn "$::lookaside_buffer_size-$tn"
290 file delete -force test.db
292 sqlite3_db_config_lookaside db 0 $::lookaside_buffer_size 500
295 catch { register_echo_module db }
296 ifcapable !vtab { if {[string match *x $tn]} continue }
303 set nAlloc1 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
304 incr nAlloc1 [lookaside db]
305 set nStmt1 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_STMT_USED 0] 1]
310 set nAlloc2 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
311 incr nAlloc2 [lookaside db]
312 set nStmt2 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_STMT_USED 0] 1]
316 set nAlloc3 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
317 incr nAlloc3 [lookaside db]
318 set nStmt3 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_STMT_USED 0] 1]
323 set nAlloc4 [lindex [sqlite3_status SQLITE_STATUS_MEMORY_USED 0] 1]
324 incr nAlloc4 [lookaside db]
325 set nStmt4 [lindex [sqlite3_db_status db SQLITE_DBSTATUS_STMT_USED 0] 1]
327 set nFree [expr {$nAlloc1-$nAlloc2}]
329 do_test dbstatus-3.$tn.a { expr $nStmt2 } {0}
331 # Tests for which the test name ends in an "x" report slightly less
332 # memory than is actually freed when all statements are finalized.
333 # This is because a small amount of memory allocated by a virtual table
334 # implementation using sqlite3_mprintf() is technically considered
335 # external and so is not counted as "statement memory".
337 #puts "$nStmt1 $nFree"
338 if {[string match *x $tn]} {
339 do_test dbstatus-3.$tn.bx { expr $nStmt1<=$nFree } {1}
341 do_test dbstatus-3.$tn.b { expr $nStmt1==$nFree } {1}
344 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]