notify2.test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # 2009 March 04
  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. #
  12. # $Id: notify2.test,v 1.7 2009/03/30 11:59:31 drh Exp $
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. if {[run_thread_tests]==0} { finish_test ; return }
  16. ifcapable !unlock_notify||!shared_cache { finish_test ; return }
  17. # The tests in this file test the sqlite3_blocking_step() function in
  18. # test_thread.c. sqlite3_blocking_step() is not an SQLite API function,
  19. # it is just a demonstration of how the sqlite3_unlock_notify() function
  20. # can be used to synchronize multi-threaded access to SQLite databases
  21. # in shared-cache mode.
  22. #
  23. # Since the implementation of sqlite3_blocking_step() is included on the
  24. # website as example code, it is important to test that it works.
  25. #
  26. # notify2-1.*:
  27. #
  28. # This test uses $nThread threads. Each thread opens the main database
  29. # and attaches two other databases. Each database contains a single table.
  30. #
  31. # Each thread repeats transactions over and over for 20 seconds. Each
  32. # transaction consists of 3 operations. Each operation is either a read
  33. # or a write of one of the tables. The read operations verify an invariant
  34. # to make sure that things are working as expected. If an SQLITE_LOCKED
  35. # error is returned the current transaction is rolled back immediately.
  36. #
  37. # This exercise is repeated twice, once using sqlite3_step(), and the
  38. # other using sqlite3_blocking_step(). The results are compared to ensure
  39. # that sqlite3_blocking_step() resulted in higher transaction throughput.
  40. #
  41. db close
  42. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  43. # Number of threads to run simultaneously.
  44. #
  45. set nThread 6
  46. set nSecond 5
  47. # The Tcl script executed by each of the $nThread threads used by this test.
  48. #
  49. set ThreadProgram {
  50. # Proc used by threads to execute SQL.
  51. #
  52. proc execsql_blocking {db zSql} {
  53. set lRes [list]
  54. set rc SQLITE_OK
  55. set sql $zSql
  56. while {$rc=="SQLITE_OK" && $zSql ne ""} {
  57. set STMT [$::xPrepare $db $zSql -1 zSql]
  58. while {[set rc [$::xStep $STMT]] eq "SQLITE_ROW"} {
  59. for {set i 0} {$i < [sqlite3_column_count $STMT]} {incr i} {
  60. lappend lRes [sqlite3_column_text $STMT 0]
  61. }
  62. }
  63. set rc [sqlite3_finalize $STMT]
  64. }
  65. if {$rc != "SQLITE_OK"} { error "$rc $sql [sqlite3_errmsg $db]" }
  66. return $lRes
  67. }
  68. proc execsql_retry {db sql} {
  69. set msg "SQLITE_LOCKED blah..."
  70. while { [string match SQLITE_LOCKED* $msg] } {
  71. catch { execsql_blocking $db $sql } msg
  72. }
  73. }
  74. proc select_one {args} {
  75. set n [llength $args]
  76. lindex $args [expr int($n*rand())]
  77. }
  78. proc opendb {} {
  79. # Open a database connection. Attach the two auxillary databases.
  80. set ::DB [sqlite3_open test.db]
  81. execsql_retry $::DB { ATTACH 'test2.db' AS aux2; }
  82. execsql_retry $::DB { ATTACH 'test3.db' AS aux3; }
  83. }
  84. opendb
  85. #after 2000
  86. # This loop runs for ~20 seconds.
  87. #
  88. set iStart [clock_seconds]
  89. while { ([clock_seconds]-$iStart) < $nSecond } {
  90. # Each transaction does 3 operations. Each operation is either a read
  91. # or write of a randomly selected table (t1, t2 or t3). Set the variables
  92. # $SQL(1), $SQL(2) and $SQL(3) to the SQL commands used to implement
  93. # each operation.
  94. #
  95. for {set ii 1} {$ii <= 3} {incr ii} {
  96. foreach {tbl database} [select_one {t1 main} {t2 aux2} {t3 aux3}] {}
  97. set SQL($ii) [string map [list xxx $tbl yyy $database] [select_one {
  98. SELECT
  99. (SELECT b FROM xxx WHERE a=(SELECT max(a) FROM xxx))==total(a)
  100. FROM xxx WHERE a!=(SELECT max(a) FROM xxx);
  101. } {
  102. DELETE FROM xxx WHERE a<(SELECT max(a)-100 FROM xxx);
  103. INSERT INTO xxx SELECT NULL, total(a) FROM xxx;
  104. } {
  105. CREATE INDEX IF NOT EXISTS yyy.xxx_i ON xxx(b);
  106. } {
  107. DROP INDEX IF EXISTS yyy.xxx_i;
  108. }
  109. ]]
  110. }
  111. # Execute the SQL transaction.
  112. #
  113. set rc [catch { execsql_blocking $::DB "
  114. BEGIN;
  115. $SQL(1);
  116. $SQL(2);
  117. $SQL(3);
  118. COMMIT;
  119. "
  120. } msg]
  121. if {$rc && [string match "SQLITE_LOCKED*" $msg]
  122. || [string match "SQLITE_SCHEMA*" $msg]
  123. } {
  124. # Hit an SQLITE_LOCKED error. Rollback the current transaction.
  125. set rc [catch { execsql_blocking $::DB ROLLBACK } msg]
  126. if {$rc && [string match "SQLITE_LOCKED*" $msg]} {
  127. sqlite3_close $::DB
  128. opendb
  129. }
  130. } elseif {$rc} {
  131. # Hit some other kind of error. This is a malfunction.
  132. error $msg
  133. } else {
  134. # No error occurred. Check that any SELECT statements in the transaction
  135. # returned "1". Otherwise, the invariant was false, indicating that
  136. # some malfunction has occurred.
  137. foreach r $msg { if {$r != 1} { puts "Invariant check failed: $msg" } }
  138. }
  139. }
  140. # Close the database connection and return 0.
  141. #
  142. sqlite3_close $::DB
  143. expr 0
  144. }
  145. foreach {iTest xStep xPrepare} {
  146. 1 sqlite3_blocking_step sqlite3_blocking_prepare_v2
  147. 2 sqlite3_step sqlite3_nonblocking_prepare_v2
  148. } {
  149. forcedelete test.db test2.db test3.db
  150. set ThreadSetup "set xStep $xStep;set xPrepare $xPrepare;set nSecond $nSecond"
  151. # Set up the database schema used by this test. Each thread opens file
  152. # test.db as the main database, then attaches files test2.db and test3.db
  153. # as auxillary databases. Each file contains a single table (t1, t2 and t3, in
  154. # files test.db, test2.db and test3.db, respectively).
  155. #
  156. do_test notify2-$iTest.1.1 {
  157. sqlite3 db test.db
  158. execsql {
  159. ATTACH 'test2.db' AS aux2;
  160. ATTACH 'test3.db' AS aux3;
  161. CREATE TABLE main.t1(a INTEGER PRIMARY KEY, b);
  162. CREATE TABLE aux2.t2(a INTEGER PRIMARY KEY, b);
  163. CREATE TABLE aux3.t3(a INTEGER PRIMARY KEY, b);
  164. INSERT INTO t1 SELECT NULL, 0;
  165. INSERT INTO t2 SELECT NULL, 0;
  166. INSERT INTO t3 SELECT NULL, 0;
  167. }
  168. } {}
  169. do_test notify2-$iTest.1.2 {
  170. db close
  171. } {}
  172. # Launch $nThread threads. Then wait for them to finish.
  173. #
  174. puts "Running $xStep test for $nSecond seconds"
  175. unset -nocomplain finished
  176. for {set ii 0} {$ii < $nThread} {incr ii} {
  177. thread_spawn finished($ii) $ThreadSetup $ThreadProgram
  178. }
  179. for {set ii 0} {$ii < $nThread} {incr ii} {
  180. do_test notify2-$iTest.2.$ii {
  181. if {![info exists finished($ii)]} { vwait finished($ii) }
  182. set finished($ii)
  183. } {0}
  184. }
  185. # Count the total number of succesful writes.
  186. do_test notify2-$iTest.3.1 {
  187. sqlite3 db test.db
  188. execsql {
  189. ATTACH 'test2.db' AS aux2;
  190. ATTACH 'test3.db' AS aux3;
  191. }
  192. set anWrite($xStep) [execsql {
  193. SELECT (SELECT max(a) FROM t1)
  194. + (SELECT max(a) FROM t2)
  195. + (SELECT max(a) FROM t3)
  196. }]
  197. db close
  198. } {}
  199. }
  200. # The following tests checks to make sure sqlite3_blocking_step() is
  201. # faster than sqlite3_step(). blocking_step() is always faster on
  202. # multi-core and is usually faster on single-core. But sometimes, by
  203. # chance, step() will be faster on a single core, in which case the
  204. # following test will fail.
  205. #
  206. puts "The following test seeks to demonstrate that the sqlite3_unlock_notify()"
  207. puts "interface helps multi-core systems to run faster. This test sometimes"
  208. puts "fails on single-core machines."
  209. puts [array get anWrite]
  210. do_test notify2-3 {
  211. expr {$anWrite(sqlite3_blocking_step) > $anWrite(sqlite3_step)}
  212. } {1}
  213. sqlite3_enable_shared_cache $::enable_shared_cache
  214. finish_test