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