Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: third_party/sqlite/test/io.test

Issue 3108030: Move bundled copy of sqlite one level deeper to better separate it... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/sqlite/test/intpkey.test ('k') | third_party/sqlite/test/ioerr.test » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # 2007 August 21
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 # The focus of this file is testing some specific characteristics of the
13 # IO traffic generated by SQLite (making sure SQLite is not writing out
14 # more database pages than it has to, stuff like that).
15 #
16
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19
20 db close
21 sqlite3_simulate_device
22 sqlite3 db test.db -vfs devsym
23
24 # Test summary:
25 #
26 # io-1.* - Test that quick-balance does not journal pages unnecessarily.
27 #
28 # io-2.* - Test the "atomic-write optimization".
29 #
30 # io-3.* - Test the IO traffic enhancements triggered when the
31 # IOCAP_SEQUENTIAL device capability flag is set (no
32 # fsync() calls on the journal file).
33 #
34 # io-4.* - Test the IO traffic enhancements triggered when the
35 # IOCAP_SAFE_APPEND device capability flag is set (fewer
36 # fsync() calls on the journal file, no need to set nRec
37 # field in the single journal header).
38 #
39 # io-5.* - Test that the default page size is selected and used
40 # correctly.
41 #
42
43 set ::nWrite 0
44 proc nWrite {db} {
45 set bt [btree_from_db $db]
46 db_enter $db
47 array set stats [btree_pager_stats $bt]
48 db_leave $db
49 set res [expr $stats(write) - $::nWrite]
50 set ::nWrite $stats(write)
51 set res
52 }
53
54 set ::nSync 0
55 proc nSync {} {
56 set res [expr {$::sqlite_sync_count - $::nSync}]
57 set ::nSync $::sqlite_sync_count
58 set res
59 }
60
61 do_test io-1.1 {
62 execsql {
63 PRAGMA auto_vacuum = OFF;
64 PRAGMA page_size = 1024;
65 CREATE TABLE abc(a,b);
66 }
67 nWrite db
68 } {2}
69
70 # Insert into the table 4 records of aproximately 240 bytes each.
71 # This should completely fill the root-page of the table. Each
72 # INSERT causes 2 db pages to be written - the root-page of "abc"
73 # and page 1 (db change-counter page).
74 do_test io-1.2 {
75 set ret [list]
76 execsql { INSERT INTO abc VALUES(1,randstr(230,230)); }
77 lappend ret [nWrite db]
78 execsql { INSERT INTO abc VALUES(2,randstr(230,230)); }
79 lappend ret [nWrite db]
80 execsql { INSERT INTO abc VALUES(3,randstr(230,230)); }
81 lappend ret [nWrite db]
82 execsql { INSERT INTO abc VALUES(4,randstr(230,230)); }
83 lappend ret [nWrite db]
84 } {2 2 2 2}
85
86 # Insert another 240 byte record. This causes two leaf pages
87 # to be added to the root page of abc. 4 pages in total
88 # are written to the db file - the two leaf pages, the root
89 # of abc and the change-counter page.
90 do_test io-1.3 {
91 execsql { INSERT INTO abc VALUES(5,randstr(230,230)); }
92 nWrite db
93 } {4}
94
95 # Insert another 3 240 byte records. After this, the tree consists of
96 # the root-node, which is close to empty, and two leaf pages, both of
97 # which are full.
98 do_test io-1.4 {
99 set ret [list]
100 execsql { INSERT INTO abc VALUES(6,randstr(230,230)); }
101 lappend ret [nWrite db]
102 execsql { INSERT INTO abc VALUES(7,randstr(230,230)); }
103 lappend ret [nWrite db]
104 execsql { INSERT INTO abc VALUES(8,randstr(230,230)); }
105 lappend ret [nWrite db]
106 } {2 2 2}
107
108 # This insert should use the quick-balance trick to add a third leaf
109 # to the b-tree used to store table abc. It should only be necessary to
110 # write to 3 pages to do this: the change-counter, the root-page and
111 # the new leaf page.
112 do_test io-1.5 {
113 execsql { INSERT INTO abc VALUES(9,randstr(230,230)); }
114 nWrite db
115 } {3}
116
117 ifcapable atomicwrite {
118
119 #----------------------------------------------------------------------
120 # Test cases io-2.* test the atomic-write optimization.
121 #
122 do_test io-2.1 {
123 execsql { DELETE FROM abc; VACUUM; }
124 } {}
125
126 # Clear the write and sync counts.
127 nWrite db ; nSync
128
129 # The following INSERT updates 2 pages and requires 4 calls to fsync():
130 #
131 # 1) The directory in which the journal file is created,
132 # 2) The journal file (to sync the page data),
133 # 3) The journal file (to sync the journal file header),
134 # 4) The database file.
135 #
136 do_test io-2.2 {
137 execsql { INSERT INTO abc VALUES(1, 2) }
138 list [nWrite db] [nSync]
139 } {2 4}
140
141 # Set the device-characteristic mask to include the SQLITE_IOCAP_ATOMIC,
142 # then do another INSERT similar to the one in io-2.2. This should
143 # only write 1 page and require a single fsync().
144 #
145 # The single fsync() is the database file. Only one page is reported as
146 # written because page 1 - the change-counter page - is written using
147 # an out-of-band method that bypasses the write counter.
148 #
149 sqlite3_simulate_device -char atomic
150 do_test io-2.3 {
151 execsql { INSERT INTO abc VALUES(3, 4) }
152 list [nWrite db] [nSync]
153 } {1 1}
154
155 # Test that the journal file is not created and the change-counter is
156 # updated when the atomic-write optimization is used.
157 #
158 do_test io-2.4.1 {
159 execsql {
160 BEGIN;
161 INSERT INTO abc VALUES(5, 6);
162 }
163 sqlite3 db2 test.db -vfs devsym
164 execsql { SELECT * FROM abc } db2
165 } {1 2 3 4}
166 do_test io-2.4.2 {
167 file exists test.db-journal
168 } {0}
169 do_test io-2.4.3 {
170 execsql { COMMIT }
171 execsql { SELECT * FROM abc } db2
172 } {1 2 3 4 5 6}
173 db2 close
174
175 # Test that the journal file is created and sync()d if the transaction
176 # modifies more than one database page, even if the IOCAP_ATOMIC flag
177 # is set.
178 #
179 do_test io-2.5.1 {
180 execsql { CREATE TABLE def(d, e) }
181 nWrite db ; nSync
182 execsql {
183 BEGIN;
184 INSERT INTO abc VALUES(7, 8);
185 }
186 file exists test.db-journal
187 } {0}
188 do_test io-2.5.2 {
189 execsql { INSERT INTO def VALUES('a', 'b'); }
190 file exists test.db-journal
191 } {1}
192 do_test io-2.5.3 {
193 execsql { COMMIT }
194 list [nWrite db] [nSync]
195 } {3 4}
196
197 # Test that the journal file is created and sync()d if the transaction
198 # modifies a single database page and also appends a page to the file.
199 # Internally, this case is handled differently to the one above. The
200 # journal file is not actually created until the 'COMMIT' statement
201 # is executed.
202 #
203 do_test io-2.6.1 {
204 execsql {
205 BEGIN;
206 INSERT INTO abc VALUES(9, randstr(1000,1000));
207 }
208 file exists test.db-journal
209 } {0}
210 do_test io-2.6.2 {
211 # Create a file at "test.db-journal". This will prevent SQLite from
212 # opening the journal for exclusive access. As a result, the COMMIT
213 # should fail with SQLITE_CANTOPEN and the transaction rolled back.
214 #
215 file mkdir test.db-journal
216 catchsql { COMMIT }
217 } {1 {unable to open database file}}
218 do_test io-2.6.3 {
219 file delete -force test.db-journal
220 catchsql { COMMIT }
221 } {1 {cannot commit - no transaction is active}}
222 do_test io-2.6.4 {
223 execsql { SELECT * FROM abc }
224 } {1 2 3 4 5 6 7 8}
225
226 # Test that if the database modification is part of multi-file commit,
227 # the journal file is always created. In this case, the journal file
228 # is created during execution of the COMMIT statement, so we have to
229 # use the same technique to check that it is created as in the above
230 # block.
231 file delete -force test2.db test2.db-journal
232 ifcapable attach {
233 do_test io-2.7.1 {
234 execsql {
235 ATTACH 'test2.db' AS aux;
236 PRAGMA aux.page_size = 1024;
237 CREATE TABLE aux.abc2(a, b);
238 BEGIN;
239 INSERT INTO abc VALUES(9, 10);
240 }
241 file exists test.db-journal
242 } {0}
243 do_test io-2.7.2 {
244 execsql { INSERT INTO abc2 SELECT * FROM abc }
245 file exists test2.db-journal
246 } {0}
247 do_test io-2.7.3 {
248 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
249 } {1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10}
250 do_test io-2.7.4 {
251 file mkdir test2.db-journal
252 catchsql { COMMIT }
253 } {1 {unable to open database file}}
254 do_test io-2.7.5 {
255 file delete -force test2.db-journal
256 catchsql { COMMIT }
257 } {1 {cannot commit - no transaction is active}}
258 do_test io-2.7.6 {
259 execsql { SELECT * FROM abc UNION ALL SELECT * FROM abc2 }
260 } {1 2 3 4 5 6 7 8}
261 }
262
263 # Try an explicit ROLLBACK before the journal file is created.
264 #
265 do_test io-2.8.1 {
266 execsql {
267 BEGIN;
268 DELETE FROM abc;
269 }
270 file exists test.db-journal
271 } {0}
272 do_test io-2.8.2 {
273 execsql { SELECT * FROM abc }
274 } {}
275 do_test io-2.8.3 {
276 execsql {
277 ROLLBACK;
278 SELECT * FROM abc;
279 }
280 } {1 2 3 4 5 6 7 8}
281
282 # Test that the atomic write optimisation is not enabled if the sector
283 # size is larger than the page-size.
284 #
285 do_test io-2.9.1 {
286 db close
287 sqlite3 db test.db
288 sqlite3_simulate_device -char atomic -sectorsize 2048
289 execsql {
290 BEGIN;
291 INSERT INTO abc VALUES(9, 10);
292 }
293 file exists test.db-journal
294 } {1}
295 do_test io-2.9.2 {
296 execsql { ROLLBACK; }
297 db close
298 file delete -force test.db test.db-journal
299 sqlite3 db test.db -vfs devsym
300 execsql {
301 PRAGMA auto_vacuum = OFF;
302 PRAGMA page_size = 2048;
303 CREATE TABLE abc(a, b);
304 }
305 execsql {
306 BEGIN;
307 INSERT INTO abc VALUES(9, 10);
308 }
309 file exists test.db-journal
310 } {0}
311 do_test io-2.9.3 {
312 execsql { COMMIT }
313 } {}
314
315 # Test a couple of the more specific IOCAP_ATOMIC flags
316 # (i.e IOCAP_ATOMIC2K etc.).
317 #
318 do_test io-2.10.1 {
319 sqlite3_simulate_device -char atomic1k
320 execsql {
321 BEGIN;
322 INSERT INTO abc VALUES(11, 12);
323 }
324 file exists test.db-journal
325 } {1}
326 do_test io-2.10.2 {
327 execsql { ROLLBACK }
328 sqlite3_simulate_device -char atomic2k
329 execsql {
330 BEGIN;
331 INSERT INTO abc VALUES(11, 12);
332 }
333 file exists test.db-journal
334 } {0}
335 do_test io-2.10.3 {
336 execsql { ROLLBACK }
337 } {}
338
339 do_test io-2.11.0 {
340 execsql {
341 PRAGMA locking_mode = exclusive;
342 PRAGMA locking_mode;
343 }
344 } {exclusive exclusive}
345 do_test io-2.11.1 {
346 execsql {
347 INSERT INTO abc VALUES(11, 12);
348 }
349 file exists test.db-journal
350 } {0}
351
352 do_test io-2.11.2 {
353 execsql {
354 PRAGMA locking_mode = normal;
355 INSERT INTO abc VALUES(13, 14);
356 }
357 file exists test.db-journal
358 } {0}
359
360 } ;# /* ifcapable atomicwrite */
361
362 #----------------------------------------------------------------------
363 # Test cases io-3.* test the IOCAP_SEQUENTIAL optimization.
364 #
365 sqlite3_simulate_device -char sequential -sectorsize 0
366 ifcapable pager_pragmas {
367 do_test io-3.1 {
368 db close
369 file delete -force test.db test.db-journal
370 sqlite3 db test.db -vfs devsym
371 db eval {
372 PRAGMA auto_vacuum=OFF;
373 }
374 # File size might be 1 due to the hack to work around ticket #3260.
375 # Search for #3260 in os_unix.c for additional information.
376 expr {[file size test.db]>1}
377 } {0}
378 do_test io-3.2 {
379 execsql { CREATE TABLE abc(a, b) }
380 nSync
381 execsql {
382 PRAGMA temp_store = memory;
383 PRAGMA cache_size = 10;
384 BEGIN;
385 INSERT INTO abc VALUES('hello', 'world');
386 INSERT INTO abc SELECT * FROM abc;
387 INSERT INTO abc SELECT * FROM abc;
388 INSERT INTO abc SELECT * FROM abc;
389 INSERT INTO abc SELECT * FROM abc;
390 INSERT INTO abc SELECT * FROM abc;
391 INSERT INTO abc SELECT * FROM abc;
392 INSERT INTO abc SELECT * FROM abc;
393 INSERT INTO abc SELECT * FROM abc;
394 INSERT INTO abc SELECT * FROM abc;
395 INSERT INTO abc SELECT * FROM abc;
396 INSERT INTO abc SELECT * FROM abc;
397 }
398 # File has grown - showing there was a cache-spill - but there
399 # have been no calls to fsync(). The file is probably about 30KB.
400 # But some VFS implementations (symbian) buffer writes so the actual
401 # size may be a little less than that. So this test case just tests
402 # that the file is now greater than 20000 bytes in size.
403 list [expr [file size test.db]>20000] [nSync]
404 } {1 0}
405 do_test io-3.3 {
406 # The COMMIT requires a single fsync() - to the database file.
407 execsql { COMMIT }
408 list [file size test.db] [nSync]
409 } {39936 1}
410 }
411
412 #----------------------------------------------------------------------
413 # Test cases io-4.* test the IOCAP_SAFE_APPEND optimization.
414 #
415 sqlite3_simulate_device -char safe_append
416
417 # With the SAFE_APPEND flag set, simple transactions require 3, rather
418 # than 4, calls to fsync(). The fsync() calls are on:
419 #
420 # 1) The directory in which the journal file is created, (unix only)
421 # 2) The journal file (to sync the page data),
422 # 3) The database file.
423 #
424 # Normally, when the SAFE_APPEND flag is not set, there is another fsync()
425 # on the journal file between steps (2) and (3) above.
426 #
427 set expected_sync_count 2
428 if {$::tcl_platform(platform)=="unix"} {
429 ifcapable dirsync {
430 incr expected_sync_count
431 }
432 }
433
434 do_test io-4.1 {
435 execsql { DELETE FROM abc }
436 nSync
437 execsql { INSERT INTO abc VALUES('a', 'b') }
438 nSync
439 } $expected_sync_count
440
441 # With SAFE_APPEND set, the nRec field of the journal file header should
442 # be set to 0xFFFFFFFF before the first journal sync. The nRec field
443 # occupies bytes 8-11 of the journal file.
444 #
445 do_test io-4.2.1 {
446 execsql { BEGIN }
447 execsql { INSERT INTO abc VALUES('c', 'd') }
448 file exists test.db-journal
449 } {1}
450 if {$::tcl_platform(platform)=="unix"} {
451 do_test io-4.2.2 {
452 hexio_read test.db-journal 8 4
453 } {FFFFFFFF}
454 }
455 do_test io-4.2.3 {
456 execsql { COMMIT }
457 nSync
458 } $expected_sync_count
459 sqlite3_simulate_device -char safe_append
460
461 # With SAFE_APPEND set, there should only ever be one journal-header
462 # written to the database, even though the sync-mode is "full".
463 #
464 do_test io-4.3.1 {
465 execsql {
466 INSERT INTO abc SELECT * FROM abc;
467 INSERT INTO abc SELECT * FROM abc;
468 INSERT INTO abc SELECT * FROM abc;
469 INSERT INTO abc SELECT * FROM abc;
470 INSERT INTO abc SELECT * FROM abc;
471 INSERT INTO abc SELECT * FROM abc;
472 INSERT INTO abc SELECT * FROM abc;
473 INSERT INTO abc SELECT * FROM abc;
474 INSERT INTO abc SELECT * FROM abc;
475 INSERT INTO abc SELECT * FROM abc;
476 INSERT INTO abc SELECT * FROM abc;
477 }
478 expr {[file size test.db]/1024}
479 } {43}
480 ifcapable pager_pragmas {
481 do_test io-4.3.2 {
482 execsql {
483 PRAGMA synchronous = full;
484 PRAGMA cache_size = 10;
485 PRAGMA synchronous;
486 }
487 } {2}
488 }
489 do_test io-4.3.3 {
490 execsql {
491 BEGIN;
492 UPDATE abc SET a = 'x';
493 }
494 file exists test.db-journal
495 } {1}
496 if {$tcl_platform(platform) != "symbian"} {
497 # This test is not run on symbian because the file-buffer makes it
498 # difficult to predict the exact size of the file as reported by
499 # [file size].
500 do_test io-4.3.4 {
501 # The UPDATE statement in the statement above modifies 41 pages
502 # (all pages in the database except page 1 and the root page of
503 # abc). Because the cache_size is set to 10, this must have required
504 # at least 4 cache-spills. If there were no journal headers written
505 # to the journal file after the cache-spill, then the size of the
506 # journal file is give by:
507 #
508 # <jrnl file size> = <jrnl header size> + nPage * (<page-size> + 8)
509 #
510 # If the journal file contains additional headers, this formula
511 # will not predict the size of the journal file.
512 #
513 file size test.db-journal
514 } [expr 512 + (1024+8)*41]
515 }
516
517 #----------------------------------------------------------------------
518 # Test cases io-5.* test that the default page size is selected and
519 # used correctly.
520 #
521 set tn 0
522 foreach {char sectorsize pgsize} {
523 {} 512 1024
524 {} 1024 1024
525 {} 2048 2048
526 {} 8192 8192
527 {} 16384 8192
528 {atomic} 512 8192
529 {atomic512} 512 1024
530 {atomic2K} 512 2048
531 {atomic2K} 4096 4096
532 {atomic2K atomic} 512 8192
533 {atomic64K} 512 1024
534 } {
535 incr tn
536 if {$pgsize>$::SQLITE_MAX_PAGE_SIZE} continue
537 db close
538 file delete -force test.db test.db-journal
539 sqlite3_simulate_device -char $char -sectorsize $sectorsize
540 sqlite3 db test.db -vfs devsym
541 db eval {
542 PRAGMA auto_vacuum=OFF;
543 }
544 ifcapable !atomicwrite {
545 if {[regexp {^atomic} $char]} continue
546 }
547 do_test io-5.$tn {
548 execsql {
549 CREATE TABLE abc(a, b, c);
550 }
551 expr {[file size test.db]/2}
552 } $pgsize
553 }
554
555 sqlite3_simulate_device -char {} -sectorsize 0
556 finish_test
OLDNEW
« no previous file with comments | « third_party/sqlite/test/intpkey.test ('k') | third_party/sqlite/test/ioerr.test » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698