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