speed3.test 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # 2007 May 17
  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. The
  12. # focus of this script is testing that the overflow-page related
  13. # enhancements added after version 3.3.17 speed things up.
  14. #
  15. # $Id: speed3.test,v 1.6 2009/07/09 02:48:24 shane Exp $
  16. #
  17. #---------------------------------------------------------------------
  18. # Test plan:
  19. #
  20. # If auto-vacuum is enabled for the database, the following cases
  21. # should show performance improvement with respect to 3.3.17.
  22. #
  23. # + When deleting rows that span overflow pages. This is faster
  24. # because the overflow pages no longer need to be read before
  25. # they can be moved to the free list (test cases speed3-1.X).
  26. #
  27. # + When reading a column value stored on an overflow page that
  28. # is not the first overflow page for the row. The improvement
  29. # in this case is because the overflow pages between the tree
  30. # page and the overflow page containing the value do not have
  31. # to be read (test cases speed3-2.X).
  32. #
  33. set testdir [file dirname $argv0]
  34. source $testdir/tester.tcl
  35. ifcapable !tclvar||!attach {
  36. finish_test
  37. return
  38. }
  39. speed_trial_init speed1
  40. # Set a uniform random seed
  41. expr srand(0)
  42. set ::NROW 1000
  43. # The number_name procedure below converts its argment (an integer)
  44. # into a string which is the English-language name for that number.
  45. #
  46. # Example:
  47. #
  48. # puts [number_name 123] -> "one hundred twenty three"
  49. #
  50. set ones {zero one two three four five six seven eight nine
  51. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  52. eighteen nineteen}
  53. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  54. proc number_name {n} {
  55. if {$n>=1000} {
  56. set txt "[number_name [expr {$n/1000}]] thousand"
  57. set n [expr {$n%1000}]
  58. } else {
  59. set txt {}
  60. }
  61. if {$n>=100} {
  62. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  63. set n [expr {$n%100}]
  64. }
  65. if {$n>=20} {
  66. append txt " [lindex $::tens [expr {$n/10}]]"
  67. set n [expr {$n%10}]
  68. }
  69. if {$n>0} {
  70. append txt " [lindex $::ones $n]"
  71. }
  72. set txt [string trim $txt]
  73. if {$txt==""} {set txt zero}
  74. return $txt
  75. }
  76. proc populate_t1 {db} {
  77. $db transaction {
  78. for {set ii 0} {$ii < $::NROW} {incr ii} {
  79. set N [number_name $ii]
  80. set repeats [expr {(10000/[string length $N])+1}]
  81. set text [string range [string repeat $N $repeats] 0 10000]
  82. $db eval {INSERT INTO main.t1 VALUES($ii, $text, $ii)}
  83. }
  84. $db eval {INSERT INTO aux.t1 SELECT * FROM main.t1}
  85. }
  86. }
  87. proc io_log {db} {
  88. db_enter db
  89. array set stats1 [btree_pager_stats [btree_from_db db]]
  90. array set stats2 [btree_pager_stats [btree_from_db db 2]]
  91. db_leave db
  92. # puts "1: [array get stats1]"
  93. # puts "2: [array get stats2]"
  94. puts "Incrvacuum: Read $stats1(read), wrote $stats1(write)"
  95. puts "Normal : Read $stats2(read), wrote $stats2(write)"
  96. }
  97. proc reset_db {} {
  98. db close
  99. sqlite3 db test.db
  100. db eval {
  101. PRAGMA main.cache_size = 200000;
  102. PRAGMA main.auto_vacuum = 'incremental';
  103. ATTACH 'test2.db' AS 'aux';
  104. PRAGMA aux.auto_vacuum = 'none';
  105. }
  106. }
  107. forcedelete test2.db test2.db-journal
  108. reset_db
  109. # Set up a database in auto-vacuum mode and create a database schema.
  110. #
  111. do_test speed3-0.1 {
  112. execsql {
  113. CREATE TABLE main.t1(a INTEGER, b TEXT, c INTEGER);
  114. }
  115. execsql {
  116. SELECT name FROM sqlite_master ORDER BY 1;
  117. }
  118. } {t1}
  119. do_test speed3-0.2 {
  120. execsql {
  121. CREATE TABLE aux.t1(a INTEGER, b TEXT, c INTEGER);
  122. }
  123. execsql {
  124. SELECT name FROM aux.sqlite_master ORDER BY 1;
  125. }
  126. } {t1}
  127. do_test speed3-0.3 {
  128. populate_t1 db
  129. execsql {
  130. SELECT count(*) FROM main.t1;
  131. SELECT count(*) FROM aux.t1;
  132. }
  133. } "$::NROW $::NROW"
  134. do_test speed3-0.4 {
  135. execsql {
  136. PRAGMA main.auto_vacuum;
  137. PRAGMA aux.auto_vacuum;
  138. }
  139. } {2 0}
  140. # Delete all content in a table, one row at a time.
  141. #
  142. #io_log db
  143. reset_db
  144. speed_trial speed3-1.incrvacuum $::NROW row {DELETE FROM main.t1 WHERE 1}
  145. speed_trial speed3-1.normal $::NROW row {DELETE FROM aux.t1 WHERE 1}
  146. io_log db
  147. # Select the "C" column (located at the far end of the overflow
  148. # chain) from each table row.
  149. #
  150. #db eval {PRAGMA incremental_vacuum(500000)}
  151. populate_t1 db
  152. reset_db
  153. speed_trial speed3-2.incrvacuum $::NROW row {SELECT c FROM main.t1}
  154. speed_trial speed3-2.normal $::NROW row {SELECT c FROM aux.t1}
  155. io_log db
  156. finish_test