xref: /sqlite-3.40.0/test/sort4.test (revision 8dd7a6a9)
1449cb9a5Sdan# 2014 May 6.
2449cb9a5Sdan#
3449cb9a5Sdan# The author disclaims copyright to this source code.  In place of
4449cb9a5Sdan# a legal notice, here is a blessing:
5449cb9a5Sdan#
6449cb9a5Sdan#    May you do good and not evil.
7449cb9a5Sdan#    May you find forgiveness for yourself and forgive others.
8449cb9a5Sdan#    May you share freely, never taking more than you give.
9449cb9a5Sdan#
10449cb9a5Sdan#***********************************************************************
11449cb9a5Sdan# This file implements regression tests for SQLite library.
12449cb9a5Sdan#
13449cb9a5Sdan# The tests in this file are brute force tests of the multi-threaded
14449cb9a5Sdan# sorter.
15449cb9a5Sdan#
16449cb9a5Sdan
17449cb9a5Sdanset testdir [file dirname $argv0]
18449cb9a5Sdansource $testdir/tester.tcl
19449cb9a5Sdanset testprefix sort4
203bd1791dSdrhdb close
213bd1791dSdrhsqlite3_shutdown
223bd1791dSdrhsqlite3_config_pmasz 10
233bd1791dSdrhsqlite3_initialize
243bd1791dSdrhsqlite3 db test.db
253bd1791dSdrh
26449cb9a5Sdan
27dfea4533Sdan# Configure the sorter to use 3 background threads.
28*8dd7a6a9Sdrh#
29*8dd7a6a9Sdrh# EVIDENCE-OF: R-19249-32353 SQLITE_LIMIT_WORKER_THREADS The maximum
30*8dd7a6a9Sdrh# number of auxiliary worker threads that a single prepared statement
31*8dd7a6a9Sdrh# may start.
32*8dd7a6a9Sdrh#
33*8dd7a6a9Sdrhdo_test sort4-init001 {
34*8dd7a6a9Sdrh  db eval {PRAGMA threads=5}
35*8dd7a6a9Sdrh  sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS -1
36*8dd7a6a9Sdrh} {5}
37*8dd7a6a9Sdrhdo_test sort4-init002 {
38*8dd7a6a9Sdrh  sqlite3_limit db SQLITE_LIMIT_WORKER_THREADS 3
39*8dd7a6a9Sdrh  db eval {PRAGMA threads}
40*8dd7a6a9Sdrh} {3}
41*8dd7a6a9Sdrh
42449cb9a5Sdan
43dfea4533Sdan# Minimum number of seconds to run for. If the value is 0, each test
44dfea4533Sdan# is run exactly once. Otherwise, tests are repeated until the timeout
45dfea4533Sdan# expires.
46dfea4533Sdanset SORT4TIMEOUT 0
47dfea4533Sdanif {[permutation] == "multithread"} { set SORT4TIMEOUT 300 }
48dfea4533Sdan
49449cb9a5Sdan#--------------------------------------------------------------------
50449cb9a5Sdan# Set up a table "t1" containing $nRow rows. Each row contains also
51dfea4533Sdan# contains blob fields that collectively contain at least $nPayload
52dfea4533Sdan# bytes of content. The table schema is as follows:
53dfea4533Sdan#
54dfea4533Sdan#   CREATE TABLE t1(a INTEGER, <extra-columns>, b INTEGER);
55dfea4533Sdan#
56dfea4533Sdan# For each row, the values of columns "a" and "b" are set to the same
57dfea4533Sdan# pseudo-randomly selected integer. The "extra-columns", of which there
58dfea4533Sdan# are at most eight, are named c0, c1, c2 etc. Column c0 contains a 4
59dfea4533Sdan# byte string. Column c1 an 8 byte string. Field c2 16 bytes, and so on.
60dfea4533Sdan#
61dfea4533Sdan# This table is intended to be used for testing queries of the form:
62dfea4533Sdan#
63dfea4533Sdan#   SELECT a, <cols>, b FROM t1 ORDER BY a;
64dfea4533Sdan#
65dfea4533Sdan# The test code checks that rows are returned in order, and that the
66dfea4533Sdan# values of "a" and "b" are the same for each row (the idea being that
67dfea4533Sdan# if field "b" at the end of the sorter record has not been corrupted,
68dfea4533Sdan# the rest of the record is probably Ok as well).
69449cb9a5Sdan#
70449cb9a5Sdanproc populate_table {nRow nPayload} {
71449cb9a5Sdan  set nCol 0
72449cb9a5Sdan
73449cb9a5Sdan  set n 0
74449cb9a5Sdan  for {set nCol 0} {$n < $nPayload} {incr nCol} {
75449cb9a5Sdan    incr n [expr (4 << $nCol)]
76449cb9a5Sdan  }
77449cb9a5Sdan
78449cb9a5Sdan  set cols [lrange [list xxx c0 c1 c2 c3 c4 c5 c6 c7] 1 $nCol]
79449cb9a5Sdan  set data [lrange [list xxx \
80449cb9a5Sdan      randomblob(4) randomblob(8) randomblob(16) randomblob(32) \
81449cb9a5Sdan      randomblob(64) randomblob(128) randomblob(256) randomblob(512) \
82449cb9a5Sdan  ] 1 $nCol]
83449cb9a5Sdan
84449cb9a5Sdan  execsql { DROP TABLE IF EXISTS t1 }
85449cb9a5Sdan
86449cb9a5Sdan  db transaction {
87449cb9a5Sdan    execsql "CREATE TABLE t1(a, [join $cols ,], b);"
88449cb9a5Sdan    set insert "INSERT INTO t1 VALUES(:k, [join $data ,], :k)"
89449cb9a5Sdan    for {set i 0} {$i < $nRow} {incr i} {
90449cb9a5Sdan      set k [expr int(rand()*1000000000)]
91449cb9a5Sdan      execsql $insert
92449cb9a5Sdan    }
93449cb9a5Sdan  }
94449cb9a5Sdan}
95449cb9a5Sdan
96449cb9a5Sdan# Helper for [do_sorter_test]
97449cb9a5Sdan#
98dfea4533Sdanproc sorter_test {nRow nRead nPayload} {
99449cb9a5Sdan  set res [list]
100449cb9a5Sdan
101449cb9a5Sdan  set nLoad [expr ($nRow > $nRead) ? $nRead : $nRow]
102449cb9a5Sdan
103449cb9a5Sdan  set nPayload [expr (($nPayload+3)/4) * 4]
104449cb9a5Sdan  set cols [list]
105449cb9a5Sdan  foreach {mask col} {
106449cb9a5Sdan    0x04  c0 0x08  c1 0x10  c2 0x20  c3
107449cb9a5Sdan    0x40  c4 0x80  c5 0x100 c6 0x200 c7
108449cb9a5Sdan  } {
109449cb9a5Sdan    if {$nPayload & $mask} { lappend cols $col }
110449cb9a5Sdan  }
111449cb9a5Sdan
112dfea4533Sdan  # Create two SELECT statements. Statement $sql1 uses the sorter to sort
113dfea4533Sdan  # $nRow records of a bit over $nPayload bytes each read from the "t1"
114dfea4533Sdan  # table created by [populate_table] proc above. Rows are sorted in order
115dfea4533Sdan  # of the integer field in each "t1" record.
116dfea4533Sdan  #
117dfea4533Sdan  # The second SQL statement sorts the same set of rows as the first, but
118dfea4533Sdan  # uses a LIMIT clause, causing SQLite to use a temp table instead of the
119dfea4533Sdan  # sorter for sorting.
120dfea4533Sdan  #
121dfea4533Sdan  set sql1 "SELECT a, [join $cols ,], b FROM t1 WHERE rowid<=$nRow ORDER BY a"
122dfea4533Sdan  set sql2 "SELECT a FROM t1 WHERE rowid<=$nRow ORDER BY a LIMIT $nRead"
123449cb9a5Sdan
124dfea4533Sdan  # Pass the two SQL statements to a helper command written in C. This
125dfea4533Sdan  # command steps statement $sql1 $nRead times and compares the integer
126dfea4533Sdan  # values in the rows returned with the results of executing $sql2. If
127dfea4533Sdan  # the comparison fails (indicating some bug in the sorter), a Tcl
128dfea4533Sdan  # exception is thrown.
129dfea4533Sdan  #
130dfea4533Sdan  sorter_test_sort4_helper db $sql1 $nRead $sql2
131449cb9a5Sdan  set {} {}
132449cb9a5Sdan}
133449cb9a5Sdan
134449cb9a5Sdan# Usage:
135449cb9a5Sdan#
136449cb9a5Sdan#   do_sorter_test <testname> <args>...
137449cb9a5Sdan#
138449cb9a5Sdan# where <args> are any of the following switches:
139449cb9a5Sdan#
140449cb9a5Sdan#   -rows N          (number of rows to have sorter sort)
141449cb9a5Sdan#   -read N          (number of rows to read out of sorter)
142449cb9a5Sdan#   -payload N       (bytes of payload to read with each row)
143449cb9a5Sdan#   -cachesize N     (Value for "PRAGMA cache_size = ?")
144449cb9a5Sdan#   -repeats N       (number of times to repeat test)
1455a7dbc70Sdan#   -fakeheap BOOL   (true to use separate allocations for in-memory records)
146449cb9a5Sdan#
147449cb9a5Sdanproc do_sorter_test {tn args} {
148449cb9a5Sdan  set a(-rows)      1000
149449cb9a5Sdan  set a(-repeats)   1
150449cb9a5Sdan  set a(-read)      100
151449cb9a5Sdan  set a(-payload)   100
152449cb9a5Sdan  set a(-cachesize) 100
1535a7dbc70Sdan  set a(-fakeheap)  0
154449cb9a5Sdan
155449cb9a5Sdan  foreach {s val} $args {
156449cb9a5Sdan    if {[info exists a($s)]==0} {
157449cb9a5Sdan      unset a(-cachesize)
158449cb9a5Sdan      set optlist "[join [array names a] ,] or -cachesize"
159449cb9a5Sdan      error "Unknown option $s, expected $optlist"
160449cb9a5Sdan    }
161449cb9a5Sdan    set a($s) $val
162449cb9a5Sdan  }
1635a7dbc70Sdan  if {[permutation] == "memsys3" || [permutation] == "memsys5"} {
1645a7dbc70Sdan    set a(-fakeheap) 0
1655a7dbc70Sdan  }
1665a7dbc70Sdan  if {$a(-fakeheap)} { sorter_test_fakeheap 1 }
1675a7dbc70Sdan
168449cb9a5Sdan
169dfea4533Sdan  db eval "PRAGMA cache_size = $a(-cachesize)"
170dfea4533Sdan  do_test $tn [subst -nocommands {
171dfea4533Sdan    for {set i 0} {[set i] < $a(-repeats)} {incr i} {
172dfea4533Sdan      sorter_test $a(-rows) $a(-read) $a(-payload)
173449cb9a5Sdan    }
174dfea4533Sdan  }] {}
1755a7dbc70Sdan
1765a7dbc70Sdan  if {$a(-fakeheap)} { sorter_test_fakeheap 0 }
177449cb9a5Sdan}
178449cb9a5Sdan
179dfea4533Sdanproc clock_seconds {} {
180dfea4533Sdan  db one {SELECT strftime('%s')}
181dfea4533Sdan}
182dfea4533Sdan
183dfea4533Sdan#-------------------------------------------------------------------------
184dfea4533Sdan# Begin tests here.
185dfea4533Sdan
186dfea4533Sdan# Create a test database.
187449cb9a5Sdando_test 1 {
188449cb9a5Sdan  execsql "PRAGMA page_size = 4096"
189449cb9a5Sdan  populate_table 100000 500
190449cb9a5Sdan} {}
191449cb9a5Sdan
192dfea4533Sdanset iTimeLimit [expr [clock_seconds] + $SORT4TIMEOUT]
193dfea4533Sdan
1945a7dbc70Sdanfor {set t 2} {1} {incr tn} {
1955a7dbc70Sdan  do_sorter_test $t.2 -repeats 10 -rows 1000   -read 100
1965a7dbc70Sdan  do_sorter_test $t.3 -repeats 10 -rows 100000 -read 1000
1975a7dbc70Sdan  do_sorter_test $t.4 -repeats 10 -rows 100000 -read 1000 -payload 500
1985a7dbc70Sdan  do_sorter_test $t.5 -repeats 10 -rows 100000 -read 100000 -payload 8
1995a7dbc70Sdan  do_sorter_test $t.6 -repeats 10 -rows 100000 -read 10 -payload 8
2005a7dbc70Sdan  do_sorter_test $t.7 -repeats 10 -rows 10000 -read 10000 -payload 8 -fakeheap 1
2015a7dbc70Sdan  do_sorter_test $t.8 -repeats 10 -rows 100000 -read 10000 -cachesize 250
202dfea4533Sdan
203dfea4533Sdan  set iNow [clock_seconds]
204dfea4533Sdan  if {$iNow>=$iTimeLimit} break
205dfea4533Sdan  do_test "$testprefix-([expr $iTimeLimit-$iNow] seconds remain)" {} {}
206dfea4533Sdan}
207449cb9a5Sdan
208449cb9a5Sdanfinish_test
209