OLD | NEW |
1 # 2009 March 04 | 1 # 2009 March 04 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 execsql_retry $::DB { ATTACH 'test3.db' AS aux3; } | 94 execsql_retry $::DB { ATTACH 'test3.db' AS aux3; } |
95 } | 95 } |
96 | 96 |
97 opendb | 97 opendb |
98 | 98 |
99 #after 2000 | 99 #after 2000 |
100 | 100 |
101 # This loop runs for ~20 seconds. | 101 # This loop runs for ~20 seconds. |
102 # | 102 # |
103 set iStart [clock_seconds] | 103 set iStart [clock_seconds] |
| 104 set nOp 0 |
| 105 set nAttempt 0 |
104 while { ([clock_seconds]-$iStart) < $nSecond } { | 106 while { ([clock_seconds]-$iStart) < $nSecond } { |
105 | 107 |
106 # Each transaction does 3 operations. Each operation is either a read | 108 # Each transaction does 3 operations. Each operation is either a read |
107 # or write of a randomly selected table (t1, t2 or t3). Set the variables | 109 # or write of a randomly selected table (t1, t2 or t3). Set the variables |
108 # $SQL(1), $SQL(2) and $SQL(3) to the SQL commands used to implement | 110 # $SQL(1), $SQL(2) and $SQL(3) to the SQL commands used to implement |
109 # each operation. | 111 # each operation. |
110 # | 112 # |
111 for {set ii 1} {$ii <= 3} {incr ii} { | 113 for {set ii 1} {$ii <= 3} {incr ii} { |
112 foreach {tbl database} [select_one {t1 main} {t2 aux2} {t3 aux3}] {} | 114 foreach {tbl database} [select_one {t1 main} {t2 aux2} {t3 aux3}] {} |
113 | 115 |
114 set SQL($ii) [string map [list xxx $tbl yyy $database] [select_one { | 116 set SQL($ii) [string map [list xxx $tbl yyy $database] [select_one { |
115 SELECT | 117 SELECT |
116 (SELECT b FROM xxx WHERE a=(SELECT max(a) FROM xxx))==total(a) | 118 (SELECT b FROM xxx WHERE a=(SELECT max(a) FROM xxx))==total(a) |
117 FROM xxx WHERE a!=(SELECT max(a) FROM xxx); | 119 FROM xxx WHERE a!=(SELECT max(a) FROM xxx); |
118 } { | 120 } { |
119 DELETE FROM xxx WHERE a<(SELECT max(a)-100 FROM xxx); | 121 DELETE FROM xxx WHERE a<(SELECT max(a)-100 FROM xxx); |
120 INSERT INTO xxx SELECT NULL, total(a) FROM xxx; | 122 INSERT INTO xxx SELECT NULL, total(a) FROM xxx; |
121 } { | 123 } { |
122 CREATE INDEX IF NOT EXISTS yyy.xxx_i ON xxx(b); | 124 CREATE INDEX IF NOT EXISTS yyy.xxx_i ON xxx(b); |
123 } { | 125 } { |
124 DROP INDEX IF EXISTS yyy.xxx_i; | 126 DROP INDEX IF EXISTS yyy.xxx_i; |
125 } | 127 } |
126 ]] | 128 ]] |
127 } | 129 } |
128 | 130 |
129 # Execute the SQL transaction. | 131 # Execute the SQL transaction. |
130 # | 132 # |
| 133 incr nAttempt |
131 set rc [catch { execsql_blocking $::DB " | 134 set rc [catch { execsql_blocking $::DB " |
132 BEGIN; | 135 BEGIN; |
133 $SQL(1); | 136 $SQL(1); |
134 $SQL(2); | 137 $SQL(2); |
135 $SQL(3); | 138 $SQL(3); |
136 COMMIT; | 139 COMMIT; |
137 " | 140 " |
138 } msg] | 141 } msg] |
139 | 142 |
140 if {$rc && [string match "SQLITE_LOCKED*" $msg] | 143 if {$rc && [string match "SQLITE_LOCKED*" $msg] |
141 || [string match "SQLITE_SCHEMA*" $msg] | 144 || [string match "SQLITE_SCHEMA*" $msg] |
142 } { | 145 } { |
143 # Hit an SQLITE_LOCKED error. Rollback the current transaction. | 146 # Hit an SQLITE_LOCKED error. Rollback the current transaction. |
144 set rc [catch { execsql_blocking $::DB ROLLBACK } msg] | 147 set rc [catch { execsql_blocking $::DB ROLLBACK } msg] |
145 if {$rc && [string match "SQLITE_LOCKED*" $msg]} { | 148 if {$rc && [string match "SQLITE_LOCKED*" $msg]} { |
146 sqlite3_close $::DB | 149 sqlite3_close $::DB |
147 opendb | 150 opendb |
148 } | 151 } |
149 } elseif {$rc} { | 152 } elseif {$rc} { |
150 # Hit some other kind of error. This is a malfunction. | 153 # Hit some other kind of error. This is a malfunction. |
151 error $msg | 154 error $msg |
152 } else { | 155 } else { |
153 # No error occurred. Check that any SELECT statements in the transaction | 156 # No error occurred. Check that any SELECT statements in the transaction |
154 # returned "1". Otherwise, the invariant was false, indicating that | 157 # returned "1". Otherwise, the invariant was false, indicating that |
155 # some malfunction has occurred. | 158 # some malfunction has occurred. |
156 foreach r $msg { if {$r != 1} { puts "Invariant check failed: $msg" } } | 159 foreach r $msg { if {$r != 1} { puts "Invariant check failed: $msg" } } |
| 160 incr nOp |
157 } | 161 } |
158 } | 162 } |
159 | 163 |
160 # Close the database connection and return 0. | 164 # Close the database connection and return 0. |
161 # | 165 # |
162 sqlite3_close $::DB | 166 sqlite3_close $::DB |
163 expr 0 | 167 list $nOp $nAttempt |
164 } | 168 } |
165 | 169 |
166 foreach {iTest xStep xPrepare} { | 170 foreach {iTest xStep xPrepare} { |
167 1 sqlite3_blocking_step sqlite3_blocking_prepare_v2 | 171 1 sqlite3_blocking_step sqlite3_blocking_prepare_v2 |
168 2 sqlite3_step sqlite3_nonblocking_prepare_v2 | 172 2 sqlite3_step sqlite3_nonblocking_prepare_v2 |
169 } { | 173 } { |
170 forcedelete test.db test2.db test3.db | 174 forcedelete test.db test2.db test3.db |
171 | 175 |
172 set ThreadSetup "set xStep $xStep;set xPrepare $xPrepare;set nSecond $nSecond" | 176 set ThreadSetup "set xStep $xStep;set xPrepare $xPrepare;set nSecond $nSecond" |
173 | 177 |
(...skipping 23 matching lines...) Expand all Loading... |
197 # Launch $nThread threads. Then wait for them to finish. | 201 # Launch $nThread threads. Then wait for them to finish. |
198 # | 202 # |
199 puts "Running $xStep test for $nSecond seconds" | 203 puts "Running $xStep test for $nSecond seconds" |
200 unset -nocomplain finished | 204 unset -nocomplain finished |
201 for {set ii 0} {$ii < $nThread} {incr ii} { | 205 for {set ii 0} {$ii < $nThread} {incr ii} { |
202 thread_spawn finished($ii) $ThreadSetup $ThreadProgram | 206 thread_spawn finished($ii) $ThreadSetup $ThreadProgram |
203 } | 207 } |
204 for {set ii 0} {$ii < $nThread} {incr ii} { | 208 for {set ii 0} {$ii < $nThread} {incr ii} { |
205 do_test notify2-$iTest.2.$ii { | 209 do_test notify2-$iTest.2.$ii { |
206 if {![info exists finished($ii)]} { vwait finished($ii) } | 210 if {![info exists finished($ii)]} { vwait finished($ii) } |
207 set finished($ii) | 211 incr anSuccess($xStep) [lindex $finished($ii) 0] |
| 212 incr anAttempt($xStep) [lindex $finished($ii) 1] |
| 213 expr 0 |
208 } {0} | 214 } {0} |
209 } | 215 } |
210 | 216 |
211 # Count the total number of succesful writes. | 217 # Count the total number of succesful writes. |
212 do_test notify2-$iTest.3.1 { | 218 do_test notify2-$iTest.3.1 { |
213 sqlite3 db test.db | 219 sqlite3 db test.db |
214 execsql { | 220 execsql { |
215 ATTACH 'test2.db' AS aux2; | 221 ATTACH 'test2.db' AS aux2; |
216 ATTACH 'test3.db' AS aux3; | 222 ATTACH 'test3.db' AS aux3; |
217 } | 223 } |
218 set anWrite($xStep) [execsql { | 224 set anWrite($xStep) [execsql { |
219 SELECT (SELECT max(a) FROM t1) | 225 SELECT (SELECT max(a) FROM t1) |
220 + (SELECT max(a) FROM t2) | 226 + (SELECT max(a) FROM t2) |
221 + (SELECT max(a) FROM t3) | 227 + (SELECT max(a) FROM t3) |
222 }] | 228 }] |
223 db close | 229 db close |
224 } {} | 230 } {} |
225 } | 231 } |
226 | 232 |
227 # The following tests checks to make sure sqlite3_blocking_step() is | 233 # The following tests checks to make sure sqlite3_blocking_step() is |
228 # faster than sqlite3_step(). blocking_step() is always faster on | 234 # faster than sqlite3_step(). "Faster" in this case means uses fewer |
229 # multi-core and is usually faster on single-core. But sometimes, by | 235 # CPU cycles. This is not always the same as faster in wall-clock time |
230 # chance, step() will be faster on a single core, in which case the | 236 # for this type of test. The number of CPU cycles per transaction is |
| 237 # roughly proportional to the number of attempts made (i.e. one plus the |
| 238 # number of SQLITE_BUSY or SQLITE_LOCKED errors that require the transaction |
| 239 # to be retried). So this test just measures that a greater percentage of |
| 240 # transactions attempted using blocking_step() succeed. |
| 241 # |
| 242 # The blocking_step() function is almost always faster on multi-core and is |
| 243 # usually faster on single-core. But sometimes, by chance, step() will be |
| 244 # faster on a single core, in which case the |
231 # following test will fail. | 245 # following test will fail. |
232 # | 246 # |
233 puts "The following test seeks to demonstrate that the sqlite3_unlock_notify()" | 247 puts "The following test seeks to demonstrate that the sqlite3_unlock_notify()" |
234 puts "interface helps multi-core systems to run faster. This test sometimes" | 248 puts "interface helps multi-core systems to run more efficiently. This test" |
235 puts "fails on single-core machines." | 249 puts "sometimes fails on single-core machines." |
236 puts [array get anWrite] | 250 puts [array get anWrite] |
237 do_test notify2-3 { | 251 do_test notify2-3 { |
238 expr {$anWrite(sqlite3_blocking_step) > $anWrite(sqlite3_step)} | 252 set blocking [expr { |
| 253 double($anSuccess(sqlite3_blocking_step)) / |
| 254 double($anAttempt(sqlite3_blocking_step)) |
| 255 }] |
| 256 set non [expr { |
| 257 double($anSuccess(sqlite3_step)) / |
| 258 double($anAttempt(sqlite3_step)) |
| 259 }] |
| 260 puts -nonewline [format " blocking: %.1f%% non-blocking %.1f%% ..." \ |
| 261 [expr $blocking*100.0] [expr $non*100.0]] |
| 262 |
| 263 expr {$blocking > $non} |
239 } {1} | 264 } {1} |
240 | 265 |
241 sqlite3_enable_shared_cache $::enable_shared_cache | 266 sqlite3_enable_shared_cache $::enable_shared_cache |
242 finish_test | 267 finish_test |
OLD | NEW |