xref: /sqlite-3.40.0/test/pcache.test (revision f2fcd075)
1# 2008 August 29
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 is focused on testing the pcache module.
13#
14# $Id: pcache.test,v 1.5 2009/05/08 06:52:48 danielk1977 Exp $
15
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18
19# Do not use a codec for tests in this file, as the database file is
20# manipulated directly using tcl scripts (using the [hexio_write] command).
21#
22do_not_use_codec
23
24# The pcache module limits the number of pages available to purgeable
25# caches to the sum of the 'cache_size' values for the set of open
26# caches. This block of tests, pcache-1.*, test that the library behaves
27# corrctly when it is forced to exceed this limit.
28#
29do_test pcache-1.1 {
30  db close
31  pcache_stats
32} {current 0 max 0 min 0 recyclable 0}
33
34do_test pcache-1.2 {
35  sqlite3 db test.db
36  execsql {
37    PRAGMA cache_size=12;
38    PRAGMA auto_vacuum=0;
39  }
40  pcache_stats
41} {current 1 max 12 min 10 recyclable 1}
42
43do_test pcache-1.3 {
44  execsql {
45    BEGIN;
46    CREATE TABLE t1(a, b, c);
47    CREATE TABLE t2(a, b, c);
48    CREATE TABLE t3(a, b, c);
49    CREATE TABLE t4(a, b, c);
50    CREATE TABLE t5(a, b, c);
51  }
52  pcache_stats
53} {current 6 max 12 min 10 recyclable 0}
54
55do_test pcache-1.4 {
56  execsql {
57    CREATE TABLE t6(a, b, c);
58    CREATE TABLE t7(a, b, c);
59    CREATE TABLE t8(a, b, c);
60    CREATE TABLE t9(a, b, c);
61  }
62  pcache_stats
63} {current 10 max 12 min 10 recyclable 0}
64
65do_test pcache-1.5 {
66  sqlite3 db2 test.db
67  execsql "PRAGMA cache_size=10" db2
68  pcache_stats
69} {current 11 max 22 min 20 recyclable 1}
70
71do_test pcache-1.6.1 {
72  execsql {
73    BEGIN;
74    SELECT * FROM sqlite_master;
75  } db2
76  pcache_stats
77} {current 11 max 22 min 20 recyclable 0}
78
79# At this point connection db2 has a read lock on the database file and a
80# single pinned page in its cache. Connection [db] is holding 10 dirty
81# pages. It cannot recycle them because of the read lock held by db2.
82#
83do_test pcache-1.6.2 {
84  execsql {
85    CREATE INDEX i1 ON t1(a, b);
86    CREATE INDEX i2 ON t2(a, b);
87    CREATE INDEX i3 ON t3(a, b);
88    CREATE INDEX i4 ON t4(a, b);
89    CREATE INDEX i5 ON t5(a, b);
90    CREATE INDEX i6 ON t6(a, b);
91    CREATE INDEX i7 ON t7(a, b);
92    CREATE INDEX i8 ON t8(a, b);
93    CREATE INDEX i9 ON t9(a, b);
94    CREATE INDEX i10 ON t9(a, b);
95    CREATE INDEX i11 ON t9(a, b);
96  }
97  pcache_stats
98} {current 23 max 22 min 20 recyclable 0}
99
100do_test pcache-1.7 {
101  execsql {
102    CREATE TABLE t10(a, b, c);
103  }
104  pcache_stats
105} {current 24 max 22 min 20 recyclable 0}
106
107# Rolling back the transaction held by db2 at this point releases a pinned
108# page. Because the number of allocated pages is greater than the
109# configured maximum, this page should be freed immediately instead of
110# recycled.
111#
112do_test pcache-1.8 {
113  execsql {ROLLBACK} db2
114  pcache_stats
115} {current 23 max 22 min 20 recyclable 0}
116
117do_test pcache-1.9 {
118  execsql COMMIT
119  pcache_stats
120} {current 22 max 22 min 20 recyclable 22}
121
122do_test pcache-1.10 {
123  db2 close
124  pcache_stats
125} {current 12 max 12 min 10 recyclable 12}
126
127do_test pcache-1.11 {
128  execsql { PRAGMA cache_size = 20 }
129  pcache_stats
130} {current 12 max 20 min 10 recyclable 12}
131
132do_test pcache-1.12 {
133  execsql {
134    SELECT * FROM t1 ORDER BY a; SELECT * FROM t1;
135    SELECT * FROM t2 ORDER BY a; SELECT * FROM t2;
136    SELECT * FROM t3 ORDER BY a; SELECT * FROM t3;
137    SELECT * FROM t4 ORDER BY a; SELECT * FROM t4;
138    SELECT * FROM t5 ORDER BY a; SELECT * FROM t5;
139    SELECT * FROM t6 ORDER BY a; SELECT * FROM t6;
140    SELECT * FROM t7 ORDER BY a; SELECT * FROM t7;
141    SELECT * FROM t8 ORDER BY a; SELECT * FROM t8;
142    SELECT * FROM t9 ORDER BY a; SELECT * FROM t9;
143  }
144  pcache_stats
145} {current 19 max 20 min 10 recyclable 19}
146
147do_test pcache-1.13 {
148  execsql { PRAGMA cache_size = 15 }
149  pcache_stats
150} {current 15 max 15 min 10 recyclable 15}
151
152do_test pcache-1.14 {
153  hexio_write test.db 24 [hexio_render_int32 1000]
154  execsql { SELECT * FROM sqlite_master }
155  pcache_stats
156} {current 2 max 15 min 10 recyclable 2}
157
158do_test pcache-1.15 {
159  execsql {
160    SELECT * FROM t1 ORDER BY a; SELECT * FROM t1;
161    SELECT * FROM t2 ORDER BY a; SELECT * FROM t2;
162    SELECT * FROM t3 ORDER BY a; SELECT * FROM t3;
163    SELECT * FROM t4 ORDER BY a; SELECT * FROM t4;
164    SELECT * FROM t5 ORDER BY a; SELECT * FROM t5;
165    SELECT * FROM t6 ORDER BY a; SELECT * FROM t6;
166    SELECT * FROM t7 ORDER BY a; SELECT * FROM t7;
167    SELECT * FROM t8 ORDER BY a; SELECT * FROM t8;
168    SELECT * FROM t9 ORDER BY a; SELECT * FROM t9;
169  }
170  pcache_stats
171} {current 14 max 15 min 10 recyclable 14}
172
173finish_test
174