aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test201
1 files changed, 201 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test b/libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test
new file mode 100644
index 0000000..41f3d5c
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/test/pageropt.test
@@ -0,0 +1,201 @@
1# 2007 April 12
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# This file implements regression tests for SQLite library.
12# The focus of the tests in this file are to verify that the
13# pager optimizations implemented in version 3.3.14 work.
14#
15# $Id: pageropt.test,v 1.3 2007/08/12 20:07:59 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20ifcapable {!pager_pragmas} {
21 finish_test
22 return
23}
24
25# Run the SQL statement supplied by the argument and return
26# the results. Prepend four integers to the beginning of the
27# result which are
28#
29# (1) The number of page reads from the database
30# (2) The number of page writes to the database
31# (3) The number of page writes to the journal
32# (4) The number of cache pages freed
33#
34proc pagercount_sql {sql {db db}} {
35 global sqlite3_pager_readdb_count
36 global sqlite3_pager_writedb_count
37 global sqlite3_pager_writej_count
38 global sqlite3_pager_pgfree_count
39 set sqlite3_pager_readdb_count 0
40 set sqlite3_pager_writedb_count 0
41 set sqlite3_pager_writej_count 0
42 set sqlite3_pager_pgfree_count 0
43 set r [$db eval $sql]
44 set cnt [list $sqlite3_pager_readdb_count \
45 $sqlite3_pager_writedb_count \
46 $sqlite3_pager_writej_count \
47 $sqlite3_pager_pgfree_count]
48 return [concat $cnt $r]
49}
50
51# Setup the test database
52#
53do_test pageropt-1.1 {
54 sqlite3_soft_heap_limit 0
55 execsql {
56 PRAGMA auto_vacuum = OFF;
57 PRAGMA page_size = 1024;
58 }
59 pagercount_sql {
60 CREATE TABLE t1(x);
61 }
62} {0 2 0 0}
63do_test pageropt-1.2 {
64 pagercount_sql {
65 INSERT INTO t1 VALUES(randomblob(5000));
66 }
67} {0 6 2 0}
68
69# Verify that values remain in cache on for subsequent reads.
70# We should not have to go back to disk.
71#
72do_test pageropt-1.3 {
73 pagercount_sql {
74 SELECT length(x) FROM t1
75 }
76} {0 0 0 0 5000}
77
78# If another thread reads the database, the original cache
79# remains valid.
80#
81sqlite3 db2 test.db
82set blobcontent [db2 one {SELECT hex(x) FROM t1}]
83do_test pageropt-1.4 {
84 pagercount_sql {
85 SELECT hex(x) FROM t1
86 }
87} [list 0 0 0 0 $blobcontent]
88
89# But if the other thread modifies the database, then the cache
90# must refill.
91#
92do_test pageropt-1.5 {
93 db2 eval {CREATE TABLE t2(y)}
94 pagercount_sql {
95 SELECT hex(x) FROM t1
96 }
97} [list 6 0 0 6 $blobcontent]
98do_test pageropt-1.6 {
99 pagercount_sql {
100 SELECT hex(x) FROM t1
101 }
102} [list 0 0 0 0 $blobcontent]
103
104# Verify that the last page of an overflow chain is not read from
105# disk when deleting a row. The one row of t1(x) has four pages
106# of overflow. So deleting that row from t1 should involve reading
107# the sqlite_master table (1 page) the main page of t1 (1 page) and
108# the three overflow pages of t1 for a total of 5 pages.
109#
110# Pages written are page 1 (for the freelist pointer), the root page
111# of the table, and one of the overflow chain pointers because it
112# becomes the trunk of the freelist. Total 3.
113#
114do_test pageropt-2.1 {
115 db close
116 sqlite3 db test.db
117 pagercount_sql {
118 DELETE FROM t1 WHERE rowid=1
119 }
120} {5 3 3 0}
121
122# When pulling pages off of the freelist, there is no reason
123# to actually bring in the old content.
124#
125do_test pageropt-2.2 {
126 db close
127 sqlite3 db test.db
128 pagercount_sql {
129 INSERT INTO t1 VALUES(randomblob(1500));
130 }
131} {3 4 3 0}
132do_test pageropt-2.3 {
133 pagercount_sql {
134 INSERT INTO t1 VALUES(randomblob(1500));
135 }
136} {0 4 3 0}
137
138# Note the new optimization that when pulling the very last page off of the
139# freelist we do not read the content of that page.
140#
141do_test pageropt-2.4 {
142 pagercount_sql {
143 INSERT INTO t1 VALUES(randomblob(1500));
144 }
145} {0 5 3 0}
146
147# Appending a large quantity of data does not involve writing much
148# to the journal file.
149#
150do_test pageropt-3.1 {
151 pagercount_sql {
152 INSERT INTO t2 SELECT * FROM t1;
153 }
154} {1 7 2 0}
155
156# Once again, we do not need to read the last page of an overflow chain
157# while deleting.
158#
159do_test pageropt-3.2 {
160 pagercount_sql {
161 DROP TABLE t2;
162 }
163} {0 2 3 0}
164do_test pageropt-3.3 {
165 pagercount_sql {
166 DELETE FROM t1;
167 }
168} {0 3 3 0}
169
170# There are now 11 pages on the freelist. Move them all into an
171# overflow chain by inserting a single large record. Starting from
172# a cold cache, only page 1, the root page of table t1, and the trunk
173# of the freelist need to be read (3 pages). And only those three
174# pages need to be journalled. But 13 pages need to be written:
175# page1, the root page of table t1, and an 11 page overflow chain.
176#
177do_test pageropt-4.1 {
178 db close
179 sqlite3 db test.db
180 pagercount_sql {
181 INSERT INTO t1 VALUES(randomblob(11300))
182 }
183} {3 13 3 0}
184
185# Now we delete that big entries starting from a cold cache and an
186# empty freelist. The first 10 of the 11 pages overflow chain have
187# to be read, together with page1 and the root of the t1 table. 12
188# reads total. But only page1, the t1 root, and the trunk of the
189# freelist need to be journalled and written back.
190#
191do_test pageroot-4.2 {
192 db close
193 sqlite3 db test.db
194 pagercount_sql {
195 DELETE FROM t1
196 }
197} {12 3 3 0}
198
199sqlite3_soft_heap_limit $soft_limit
200catch {db2 close}
201finish_test