Fix a problem causing the recovery extension to use excessive memory and CPU time...
[sqlite.git] / test / sort4.test
blob84125885abf205408ed52eb615215985a55d3934
1 # 2014 May 6.
3 # The author disclaims copyright to this source code.  In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
11 # TESTRUNNER: superslow
13 # This file implements regression tests for SQLite library. 
15 # The tests in this file are brute force tests of the multi-threaded
16 # sorter.
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
21 set testprefix sort4
22 db close
23 sqlite3_shutdown
24 sqlite3_config_pmasz 10
25 sqlite3_initialize
26 sqlite3 db test.db
29 # Configure the sorter to use 3 background threads.
31 # EVIDENCE-OF: R-19249-32353 SQLITE_LIMIT_WORKER_THREADS The maximum
32 # number of auxiliary worker threads that a single prepared statement
33 # may start.
35 do_test sort4-init001 {
36   db eval {PRAGMA threads=5}
37   sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1
38 } {5}
39 do_test sort4-init002 {
40   sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 3
41   db eval {PRAGMA threads}
42 } {3}
45 # Minimum number of seconds to run for. If the value is 0, each test
46 # is run exactly once. Otherwise, tests are repeated until the timeout
47 # expires.
48 set SORT4TIMEOUT 0
49 if {[permutation] == "multithread"} { set SORT4TIMEOUT 300 }
51 #--------------------------------------------------------------------
52 # Set up a table "t1" containing $nRow rows. Each row contains also
53 # contains blob fields that collectively contain at least $nPayload 
54 # bytes of content. The table schema is as follows:
56 #   CREATE TABLE t1(a INTEGER, <extra-columns>, b INTEGER);
58 # For each row, the values of columns "a" and "b" are set to the same
59 # pseudo-randomly selected integer. The "extra-columns", of which there
60 # are at most eight, are named c0, c1, c2 etc. Column c0 contains a 4
61 # byte string. Column c1 an 8 byte string. Field c2 16 bytes, and so on.
63 # This table is intended to be used for testing queries of the form: 
65 #   SELECT a, <cols>, b FROM t1 ORDER BY a;
67 # The test code checks that rows are returned in order, and that the 
68 # values of "a" and "b" are the same for each row (the idea being that
69 # if field "b" at the end of the sorter record has not been corrupted, 
70 # the rest of the record is probably Ok as well).
72 proc populate_table {nRow nPayload} {
73   set nCol 0
75   set n 0
76   for {set nCol 0} {$n < $nPayload} {incr nCol} {
77     incr n [expr (4 << $nCol)]
78   }
80   set cols [lrange [list xxx c0 c1 c2 c3 c4 c5 c6 c7] 1 $nCol]
81   set data [lrange [list xxx \
82       randomblob(4) randomblob(8) randomblob(16) randomblob(32) \
83       randomblob(64) randomblob(128) randomblob(256) randomblob(512) \
84   ] 1 $nCol]
86   execsql { DROP TABLE IF EXISTS t1 }
88   db transaction {
89     execsql "CREATE TABLE t1(a, [join $cols ,], b);"
90     set insert "INSERT INTO t1 VALUES(:k, [join $data ,], :k)"
91     for {set i 0} {$i < $nRow} {incr i} {
92       set k [expr int(rand()*1000000000)]
93       execsql $insert
94     }
95   }
98 # Helper for [do_sorter_test]
100 proc sorter_test {nRow nRead nPayload} {
101   set res [list]
103   set nLoad [expr ($nRow > $nRead) ? $nRead : $nRow]
105   set nPayload [expr (($nPayload+3)/4) * 4]
106   set cols [list]
107   foreach {mask col} { 
108     0x04  c0 0x08  c1 0x10  c2 0x20  c3 
109     0x40  c4 0x80  c5 0x100 c6 0x200 c7 
110   } {
111     if {$nPayload & $mask} { lappend cols $col }
112   }
114   # Create two SELECT statements. Statement $sql1 uses the sorter to sort
115   # $nRow records of a bit over $nPayload bytes each read from the "t1"
116   # table created by [populate_table] proc above. Rows are sorted in order
117   # of the integer field in each "t1" record.
118   #
119   # The second SQL statement sorts the same set of rows as the first, but
120   # uses a LIMIT clause, causing SQLite to use a temp table instead of the
121   # sorter for sorting.
122   #
123   set sql1 "SELECT a, [join $cols ,], b FROM t1 WHERE rowid<=$nRow ORDER BY a"
124   set sql2 "SELECT a FROM t1 WHERE rowid<=$nRow ORDER BY a LIMIT $nRead"
126   # Pass the two SQL statements to a helper command written in C. This
127   # command steps statement $sql1 $nRead times and compares the integer
128   # values in the rows returned with the results of executing $sql2. If
129   # the comparison fails (indicating some bug in the sorter), a Tcl
130   # exception is thrown.
131   #
132   sorter_test_sort4_helper db $sql1 $nRead $sql2
133   set {} {} 
136 # Usage:
138 #   do_sorter_test <testname> <args>...
140 # where <args> are any of the following switches:
142 #   -rows N          (number of rows to have sorter sort)
143 #   -read N          (number of rows to read out of sorter)
144 #   -payload N       (bytes of payload to read with each row)
145 #   -cachesize N     (Value for "PRAGMA cache_size = ?")
146 #   -repeats N       (number of times to repeat test)
147 #   -fakeheap BOOL   (true to use separate allocations for in-memory records)
149 proc do_sorter_test {tn args} {
150   set a(-rows)      1000
151   set a(-repeats)   1
152   set a(-read)      100
153   set a(-payload)   100
154   set a(-cachesize) 100
155   set a(-fakeheap)  0
157   foreach {s val} $args {
158     if {[info exists a($s)]==0} { 
159       unset a(-cachesize)
160       set optlist "[join [array names a] ,] or -cachesize"
161       error "Unknown option $s, expected $optlist"
162     }
163     set a($s) $val
164   }
165   if {[permutation] == "memsys3" || [permutation] == "memsys5"} {
166     set a(-fakeheap) 0
167   }
168   if {$a(-fakeheap)} { sorter_test_fakeheap 1 }
171   db eval "PRAGMA cache_size = $a(-cachesize)"
172   do_test $tn [subst -nocommands {
173     for {set i 0} {[set i] < $a(-repeats)} {incr i} {
174       sorter_test $a(-rows) $a(-read) $a(-payload)
175     }
176   }] {}
178   if {$a(-fakeheap)} { sorter_test_fakeheap 0 }
181 proc clock_seconds {} {
182   db one {SELECT strftime('%s')}
185 #-------------------------------------------------------------------------
186 # Begin tests here.
188 # Create a test database.
189 do_test 1 {
190   execsql "PRAGMA page_size = 4096"
191   populate_table 100000 500
192 } {}
194 set iTimeLimit [expr [clock_seconds] + $SORT4TIMEOUT]
196 for {set t 2} {1} {incr tn} {
197   do_sorter_test $t.2 -repeats 10 -rows 1000   -read 100
198   do_sorter_test $t.3 -repeats 10 -rows 100000 -read 1000
199   do_sorter_test $t.4 -repeats 10 -rows 100000 -read 1000 -payload 500
200   do_sorter_test $t.5 -repeats 10 -rows 100000 -read 100000 -payload 8
201   do_sorter_test $t.6 -repeats 10 -rows 100000 -read 10 -payload 8
202   do_sorter_test $t.7 -repeats 10 -rows 10000 -read 10000 -payload 8 -fakeheap 1
203   do_sorter_test $t.8 -repeats 10 -rows 100000 -read 10000 -cachesize 250
205   set iNow [clock_seconds]
206   if {$iNow>=$iTimeLimit} break
207   do_test "$testprefix-([expr $iTimeLimit-$iNow] seconds remain)" {} {}
210 finish_test