Debian Codegraph @

test/sort4.test

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