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