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