sl@0: # 2007 May 17 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #************************************************************************* sl@0: # This file implements regression tests for SQLite library. The sl@0: # focus of this script is testing that the overflow-page related sl@0: # enhancements added after version 3.3.17 speed things up. sl@0: # sl@0: # $Id: speed3.test,v 1.5 2007/10/09 08:29:33 danielk1977 Exp $ sl@0: # sl@0: sl@0: #--------------------------------------------------------------------- sl@0: # Test plan: sl@0: # sl@0: # If auto-vacuum is enabled for the database, the following cases sl@0: # should show performance improvement with respect to 3.3.17. sl@0: # sl@0: # + When deleting rows that span overflow pages. This is faster sl@0: # because the overflow pages no longer need to be read before sl@0: # they can be moved to the free list (test cases speed3-1.X). sl@0: # sl@0: # + When reading a column value stored on an overflow page that sl@0: # is not the first overflow page for the row. The improvement sl@0: # in this case is because the overflow pages between the tree sl@0: # page and the overflow page containing the value do not have sl@0: # to be read (test cases speed3-2.X). sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !tclvar||!attach { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: speed_trial_init speed1 sl@0: sl@0: # Set a uniform random seed sl@0: expr srand(0) sl@0: sl@0: set ::NROW 1000 sl@0: sl@0: # The number_name procedure below converts its argment (an integer) sl@0: # into a string which is the English-language name for that number. sl@0: # sl@0: # Example: sl@0: # sl@0: # puts [number_name 123] -> "one hundred twenty three" sl@0: # sl@0: set ones {zero one two three four five six seven eight nine sl@0: ten eleven twelve thirteen fourteen fifteen sixteen seventeen sl@0: eighteen nineteen} sl@0: set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} sl@0: proc number_name {n} { sl@0: if {$n>=1000} { sl@0: set txt "[number_name [expr {$n/1000}]] thousand" sl@0: set n [expr {$n%1000}] sl@0: } else { sl@0: set txt {} sl@0: } sl@0: if {$n>=100} { sl@0: append txt " [lindex $::ones [expr {$n/100}]] hundred" sl@0: set n [expr {$n%100}] sl@0: } sl@0: if {$n>=20} { sl@0: append txt " [lindex $::tens [expr {$n/10}]]" sl@0: set n [expr {$n%10}] sl@0: } sl@0: if {$n>0} { sl@0: append txt " [lindex $::ones $n]" sl@0: } sl@0: set txt [string trim $txt] sl@0: if {$txt==""} {set txt zero} sl@0: return $txt sl@0: } sl@0: sl@0: proc populate_t1 {db} { sl@0: $db transaction { sl@0: for {set ii 0} {$ii < $::NROW} {incr ii} { sl@0: set N [number_name $ii] sl@0: set repeats [expr {(10000/[string length $N])+1}] sl@0: set text [string range [string repeat $N $repeats] 0 10000] sl@0: $db eval {INSERT INTO main.t1 VALUES($ii, $text, $ii)} sl@0: } sl@0: $db eval {INSERT INTO aux.t1 SELECT * FROM main.t1} sl@0: } sl@0: } sl@0: sl@0: sl@0: proc io_log {db} { sl@0: db_enter db sl@0: array set stats1 [btree_pager_stats [btree_from_db db]] sl@0: array set stats2 [btree_pager_stats [btree_from_db db 2]] sl@0: db_leave db sl@0: # puts "1: [array get stats1]" sl@0: # puts "2: [array get stats2]" sl@0: puts "Incrvacuum: Read $stats1(read), wrote $stats1(write)" sl@0: puts "Normal : Read $stats2(read), wrote $stats2(write)" sl@0: } sl@0: sl@0: proc overflow_report {db} { sl@0: set bt [btree_from_db db] sl@0: set csr [btree_cursor $bt 3 0] sl@0: sl@0: for {btree_first $csr} {![btree_eof $csr]} {btree_next $csr} { sl@0: puts "[btree_ovfl_info $bt $csr]" sl@0: } sl@0: sl@0: btree_close_cursor $csr sl@0: sl@0: } sl@0: sl@0: proc reset_db {} { sl@0: db close sl@0: sqlite3 db test.db sl@0: db eval { sl@0: PRAGMA main.cache_size = 200000; sl@0: PRAGMA main.auto_vacuum = 'incremental'; sl@0: ATTACH 'test2.db' AS 'aux'; sl@0: PRAGMA aux.auto_vacuum = 'none'; sl@0: } sl@0: } sl@0: sl@0: file delete -force test2.db test2.db-journal sl@0: reset_db sl@0: sl@0: # Set up a database in auto-vacuum mode and create a database schema. sl@0: # sl@0: do_test speed3-0.1 { sl@0: execsql { sl@0: CREATE TABLE main.t1(a INTEGER, b TEXT, c INTEGER); sl@0: } sl@0: execsql { sl@0: SELECT name FROM sqlite_master ORDER BY 1; sl@0: } sl@0: } {t1} sl@0: do_test speed3-0.2 { sl@0: execsql { sl@0: CREATE TABLE aux.t1(a INTEGER, b TEXT, c INTEGER); sl@0: } sl@0: execsql { sl@0: SELECT name FROM aux.sqlite_master ORDER BY 1; sl@0: } sl@0: } {t1} sl@0: do_test speed3-0.3 { sl@0: populate_t1 db sl@0: execsql { sl@0: SELECT count(*) FROM main.t1; sl@0: SELECT count(*) FROM aux.t1; sl@0: } sl@0: } "$::NROW $::NROW" sl@0: do_test speed3-0.4 { sl@0: execsql { sl@0: PRAGMA main.auto_vacuum; sl@0: PRAGMA aux.auto_vacuum; sl@0: } sl@0: } {2 0} sl@0: sl@0: # Delete all content in a table, one row at a time. sl@0: # sl@0: #io_log db sl@0: #overflow_report db sl@0: reset_db sl@0: speed_trial speed3-1.incrvacuum $::NROW row {DELETE FROM main.t1 WHERE 1} sl@0: speed_trial speed3-1.normal $::NROW row {DELETE FROM aux.t1 WHERE 1} sl@0: io_log db sl@0: sl@0: # Select the "C" column (located at the far end of the overflow sl@0: # chain) from each table row. sl@0: # sl@0: #db eval {PRAGMA incremental_vacuum(500000)} sl@0: populate_t1 db sl@0: #overflow_report db sl@0: reset_db sl@0: speed_trial speed3-2.incrvacuum $::NROW row {SELECT c FROM main.t1} sl@0: speed_trial speed3-2.normal $::NROW row {SELECT c FROM aux.t1} sl@0: io_log db sl@0: sl@0: finish_test