000001 # 2014 May 6. 000002 # 000003 # The author disclaims copyright to this source code. In place of 000004 # a legal notice, here is a blessing: 000005 # 000006 # May you do good and not evil. 000007 # May you find forgiveness for yourself and forgive others. 000008 # May you share freely, never taking more than you give. 000009 # 000010 #*********************************************************************** 000011 # TESTRUNNER: superslow 000012 # 000013 # This file implements regression tests for SQLite library. 000014 # 000015 # The tests in this file are brute force tests of the multi-threaded 000016 # sorter. 000017 # 000018 000019 set testdir [file dirname $argv0] 000020 source $testdir/tester.tcl 000021 set testprefix sort4 000022 db close 000023 sqlite3_shutdown 000024 sqlite3_config_pmasz 10 000025 sqlite3_initialize 000026 sqlite3 db test.db 000027 000028 000029 if {![string match *MAX_WORKER_THREADS=0* [db eval {PRAGMA compile_options}]]} { 000030 # Configure the sorter to use 3 background threads. 000031 # 000032 # EVIDENCE-OF: R-19249-32353 SQLITE_LIMIT_WORKER_THREADS The maximum 000033 # number of auxiliary worker threads that a single prepared statement 000034 # may start. 000035 # 000036 do_test sort4-init001 { 000037 db eval {PRAGMA threads=5} 000038 sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1 000039 } {5} 000040 do_test sort4-init002 { 000041 sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 3 000042 db eval {PRAGMA threads} 000043 } {3} 000044 } 000045 000046 000047 # Minimum number of seconds to run for. If the value is 0, each test 000048 # is run exactly once. Otherwise, tests are repeated until the timeout 000049 # expires. 000050 set SORT4TIMEOUT 0 000051 if {[permutation] == "multithread"} { set SORT4TIMEOUT 300 } 000052 000053 #-------------------------------------------------------------------- 000054 # Set up a table "t1" containing $nRow rows. Each row contains also 000055 # contains blob fields that collectively contain at least $nPayload 000056 # bytes of content. The table schema is as follows: 000057 # 000058 # CREATE TABLE t1(a INTEGER, <extra-columns>, b INTEGER); 000059 # 000060 # For each row, the values of columns "a" and "b" are set to the same 000061 # pseudo-randomly selected integer. The "extra-columns", of which there 000062 # are at most eight, are named c0, c1, c2 etc. Column c0 contains a 4 000063 # byte string. Column c1 an 8 byte string. Field c2 16 bytes, and so on. 000064 # 000065 # This table is intended to be used for testing queries of the form: 000066 # 000067 # SELECT a, <cols>, b FROM t1 ORDER BY a; 000068 # 000069 # The test code checks that rows are returned in order, and that the 000070 # values of "a" and "b" are the same for each row (the idea being that 000071 # if field "b" at the end of the sorter record has not been corrupted, 000072 # the rest of the record is probably Ok as well). 000073 # 000074 proc populate_table {nRow nPayload} { 000075 set nCol 0 000076 000077 set n 0 000078 for {set nCol 0} {$n < $nPayload} {incr nCol} { 000079 incr n [expr (4 << $nCol)] 000080 } 000081 000082 set cols [lrange [list xxx c0 c1 c2 c3 c4 c5 c6 c7] 1 $nCol] 000083 set data [lrange [list xxx \ 000084 randomblob(4) randomblob(8) randomblob(16) randomblob(32) \ 000085 randomblob(64) randomblob(128) randomblob(256) randomblob(512) \ 000086 ] 1 $nCol] 000087 000088 execsql { DROP TABLE IF EXISTS t1 } 000089 000090 db transaction { 000091 execsql "CREATE TABLE t1(a, [join $cols ,], b);" 000092 set insert "INSERT INTO t1 VALUES(:k, [join $data ,], :k)" 000093 for {set i 0} {$i < $nRow} {incr i} { 000094 set k [expr int(rand()*1000000000)] 000095 execsql $insert 000096 } 000097 } 000098 } 000099 000100 # Helper for [do_sorter_test] 000101 # 000102 proc sorter_test {nRow nRead nPayload} { 000103 set res [list] 000104 000105 set nLoad [expr ($nRow > $nRead) ? $nRead : $nRow] 000106 000107 set nPayload [expr (($nPayload+3)/4) * 4] 000108 set cols [list] 000109 foreach {mask col} { 000110 0x04 c0 0x08 c1 0x10 c2 0x20 c3 000111 0x40 c4 0x80 c5 0x100 c6 0x200 c7 000112 } { 000113 if {$nPayload & $mask} { lappend cols $col } 000114 } 000115 000116 # Create two SELECT statements. Statement $sql1 uses the sorter to sort 000117 # $nRow records of a bit over $nPayload bytes each read from the "t1" 000118 # table created by [populate_table] proc above. Rows are sorted in order 000119 # of the integer field in each "t1" record. 000120 # 000121 # The second SQL statement sorts the same set of rows as the first, but 000122 # uses a LIMIT clause, causing SQLite to use a temp table instead of the 000123 # sorter for sorting. 000124 # 000125 set sql1 "SELECT a, [join $cols ,], b FROM t1 WHERE rowid<=$nRow ORDER BY a" 000126 set sql2 "SELECT a FROM t1 WHERE rowid<=$nRow ORDER BY a LIMIT $nRead" 000127 000128 # Pass the two SQL statements to a helper command written in C. This 000129 # command steps statement $sql1 $nRead times and compares the integer 000130 # values in the rows returned with the results of executing $sql2. If 000131 # the comparison fails (indicating some bug in the sorter), a Tcl 000132 # exception is thrown. 000133 # 000134 sorter_test_sort4_helper db $sql1 $nRead $sql2 000135 set {} {} 000136 } 000137 000138 # Usage: 000139 # 000140 # do_sorter_test <testname> <args>... 000141 # 000142 # where <args> are any of the following switches: 000143 # 000144 # -rows N (number of rows to have sorter sort) 000145 # -read N (number of rows to read out of sorter) 000146 # -payload N (bytes of payload to read with each row) 000147 # -cachesize N (Value for "PRAGMA cache_size = ?") 000148 # -repeats N (number of times to repeat test) 000149 # -fakeheap BOOL (true to use separate allocations for in-memory records) 000150 # 000151 proc do_sorter_test {tn args} { 000152 set a(-rows) 1000 000153 set a(-repeats) 1 000154 set a(-read) 100 000155 set a(-payload) 100 000156 set a(-cachesize) 100 000157 set a(-fakeheap) 0 000158 000159 foreach {s val} $args { 000160 if {[info exists a($s)]==0} { 000161 unset a(-cachesize) 000162 set optlist "[join [array names a] ,] or -cachesize" 000163 error "Unknown option $s, expected $optlist" 000164 } 000165 set a($s) $val 000166 } 000167 if {[permutation] == "memsys3" || [permutation] == "memsys5"} { 000168 set a(-fakeheap) 0 000169 } 000170 if {$a(-fakeheap)} { sorter_test_fakeheap 1 } 000171 000172 000173 db eval "PRAGMA cache_size = $a(-cachesize)" 000174 do_test $tn [subst -nocommands { 000175 for {set i 0} {[set i] < $a(-repeats)} {incr i} { 000176 sorter_test $a(-rows) $a(-read) $a(-payload) 000177 } 000178 }] {} 000179 000180 if {$a(-fakeheap)} { sorter_test_fakeheap 0 } 000181 } 000182 000183 proc clock_seconds {} { 000184 db one {SELECT strftime('%s')} 000185 } 000186 000187 #------------------------------------------------------------------------- 000188 # Begin tests here. 000189 000190 # Create a test database. 000191 do_test 1 { 000192 execsql "PRAGMA page_size = 4096" 000193 populate_table 100000 500 000194 } {} 000195 000196 set iTimeLimit [expr [clock_seconds] + $SORT4TIMEOUT] 000197 000198 for {set t 2} {1} {incr tn} { 000199 do_sorter_test $t.2 -repeats 10 -rows 1000 -read 100 000200 do_sorter_test $t.3 -repeats 10 -rows 100000 -read 1000 000201 do_sorter_test $t.4 -repeats 10 -rows 100000 -read 1000 -payload 500 000202 do_sorter_test $t.5 -repeats 10 -rows 100000 -read 100000 -payload 8 000203 do_sorter_test $t.6 -repeats 10 -rows 100000 -read 10 -payload 8 000204 do_sorter_test $t.7 -repeats 10 -rows 10000 -read 10000 -payload 8 -fakeheap 1 000205 do_sorter_test $t.8 -repeats 10 -rows 100000 -read 10000 -cachesize 250 000206 000207 set iNow [clock_seconds] 000208 if {$iNow>=$iTimeLimit} break 000209 do_test "$testprefix-([expr $iTimeLimit-$iNow] seconds remain)" {} {} 000210 } 000211 000212 finish_test