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