pcache.test 4.9 KB

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