sl@0: # 2007 April 12 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # This file implements regression tests for SQLite library. sl@0: # The focus of the tests in this file are to verify that the sl@0: # pager optimizations implemented in version 3.3.14 work. sl@0: # sl@0: # $Id: pageropt.test,v 1.5 2008/08/20 14:49:25 danielk1977 Exp $ sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable {!pager_pragmas||secure_delete} { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # Run the SQL statement supplied by the argument and return sl@0: # the results. Prepend four integers to the beginning of the sl@0: # result which are sl@0: # sl@0: # (1) The number of page reads from the database sl@0: # (2) The number of page writes to the database sl@0: # (3) The number of page writes to the journal sl@0: # (4) The number of cache pages freed sl@0: # sl@0: proc pagercount_sql {sql {db db}} { sl@0: global sqlite3_pager_readdb_count sl@0: global sqlite3_pager_writedb_count sl@0: global sqlite3_pager_writej_count sl@0: global sqlite3_pager_pgfree_count sl@0: set sqlite3_pager_readdb_count 0 sl@0: set sqlite3_pager_writedb_count 0 sl@0: set sqlite3_pager_writej_count 0 sl@0: set r [$db eval $sql] sl@0: set cnt [list $sqlite3_pager_readdb_count \ sl@0: $sqlite3_pager_writedb_count \ sl@0: $sqlite3_pager_writej_count ] sl@0: return [concat $cnt $r] sl@0: } sl@0: sl@0: # Setup the test database sl@0: # sl@0: do_test pageropt-1.1 { sl@0: sqlite3_soft_heap_limit 0 sl@0: execsql { sl@0: PRAGMA auto_vacuum = OFF; sl@0: PRAGMA page_size = 1024; sl@0: } sl@0: pagercount_sql { sl@0: CREATE TABLE t1(x); sl@0: } sl@0: } {0 2 0} sl@0: do_test pageropt-1.2 { sl@0: pagercount_sql { sl@0: INSERT INTO t1 VALUES(randomblob(5000)); sl@0: } sl@0: } {0 6 2} sl@0: sl@0: # Verify that values remain in cache on for subsequent reads. sl@0: # We should not have to go back to disk. sl@0: # sl@0: do_test pageropt-1.3 { sl@0: pagercount_sql { sl@0: SELECT length(x) FROM t1 sl@0: } sl@0: } {0 0 0 5000} sl@0: sl@0: # If another thread reads the database, the original cache sl@0: # remains valid. sl@0: # sl@0: sqlite3 db2 test.db sl@0: set blobcontent [db2 one {SELECT hex(x) FROM t1}] sl@0: do_test pageropt-1.4 { sl@0: pagercount_sql { sl@0: SELECT hex(x) FROM t1 sl@0: } sl@0: } [list 0 0 0 $blobcontent] sl@0: sl@0: # But if the other thread modifies the database, then the cache sl@0: # must refill. sl@0: # sl@0: do_test pageropt-1.5 { sl@0: db2 eval {CREATE TABLE t2(y)} sl@0: pagercount_sql { sl@0: SELECT hex(x) FROM t1 sl@0: } sl@0: } [list 6 0 0 $blobcontent] sl@0: do_test pageropt-1.6 { sl@0: pagercount_sql { sl@0: SELECT hex(x) FROM t1 sl@0: } sl@0: } [list 0 0 0 $blobcontent] sl@0: sl@0: # Verify that the last page of an overflow chain is not read from sl@0: # disk when deleting a row. The one row of t1(x) has four pages sl@0: # of overflow. So deleting that row from t1 should involve reading sl@0: # the sqlite_master table (1 page) the main page of t1 (1 page) and sl@0: # the three overflow pages of t1 for a total of 5 pages. sl@0: # sl@0: # Pages written are page 1 (for the freelist pointer), the root page sl@0: # of the table, and one of the overflow chain pointers because it sl@0: # becomes the trunk of the freelist. Total 3. sl@0: # sl@0: do_test pageropt-2.1 { sl@0: db close sl@0: sqlite3 db test.db sl@0: pagercount_sql { sl@0: DELETE FROM t1 WHERE rowid=1 sl@0: } sl@0: } {5 3 3} sl@0: sl@0: # When pulling pages off of the freelist, there is no reason sl@0: # to actually bring in the old content. sl@0: # sl@0: do_test pageropt-2.2 { sl@0: db close sl@0: sqlite3 db test.db sl@0: pagercount_sql { sl@0: INSERT INTO t1 VALUES(randomblob(1500)); sl@0: } sl@0: } {3 4 3} sl@0: do_test pageropt-2.3 { sl@0: pagercount_sql { sl@0: INSERT INTO t1 VALUES(randomblob(1500)); sl@0: } sl@0: } {0 4 3} sl@0: sl@0: # Note the new optimization that when pulling the very last page off of the sl@0: # freelist we do not read the content of that page. sl@0: # sl@0: do_test pageropt-2.4 { sl@0: pagercount_sql { sl@0: INSERT INTO t1 VALUES(randomblob(1500)); sl@0: } sl@0: } {0 5 3} sl@0: sl@0: # Appending a large quantity of data does not involve writing much sl@0: # to the journal file. sl@0: # sl@0: do_test pageropt-3.1 { sl@0: pagercount_sql { sl@0: INSERT INTO t2 SELECT * FROM t1; sl@0: } sl@0: } {1 7 2} sl@0: sl@0: # Once again, we do not need to read the last page of an overflow chain sl@0: # while deleting. sl@0: # sl@0: do_test pageropt-3.2 { sl@0: pagercount_sql { sl@0: DROP TABLE t2; sl@0: } sl@0: } {0 2 3} sl@0: do_test pageropt-3.3 { sl@0: pagercount_sql { sl@0: DELETE FROM t1; sl@0: } sl@0: } {0 3 3} sl@0: sl@0: # There are now 11 pages on the freelist. Move them all into an sl@0: # overflow chain by inserting a single large record. Starting from sl@0: # a cold cache, only page 1, the root page of table t1, and the trunk sl@0: # of the freelist need to be read (3 pages). And only those three sl@0: # pages need to be journalled. But 13 pages need to be written: sl@0: # page1, the root page of table t1, and an 11 page overflow chain. sl@0: # sl@0: do_test pageropt-4.1 { sl@0: db close sl@0: sqlite3 db test.db sl@0: pagercount_sql { sl@0: INSERT INTO t1 VALUES(randomblob(11300)) sl@0: } sl@0: } {3 13 3} sl@0: sl@0: # Now we delete that big entries starting from a cold cache and an sl@0: # empty freelist. The first 10 of the 11 pages overflow chain have sl@0: # to be read, together with page1 and the root of the t1 table. 12 sl@0: # reads total. But only page1, the t1 root, and the trunk of the sl@0: # freelist need to be journalled and written back. sl@0: # sl@0: do_test pageropt-4.2 { sl@0: db close sl@0: sqlite3 db test.db sl@0: pagercount_sql { sl@0: DELETE FROM t1 sl@0: } sl@0: } {12 3 3} sl@0: sl@0: sqlite3_soft_heap_limit $soft_limit sl@0: catch {db2 close} sl@0: finish_test