2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # Run this script using TCLSH to do a speed comparison between
7 # various versions of SQLite and PostgreSQL and MySQL
13 proc runtest
{title
} {
15 set sqlfile test
$cnt.sql
16 puts "<h2>Test $cnt: $title</h2>"
18 set fd
[open $sqlfile r
]
19 set sql
[string trim
[read $fd [file size
$sqlfile]]]
21 set sx
[split $sql \n]
25 for {set i
0} {$i<3} {incr i
} {append sql
[lindex $sx $i]<br
>\n}
26 append sql
"<i>... [expr {$n-6}] lines omitted</i><br>\n"
27 for {set i
[expr {$n-3}]} {$i<$n} {incr i
} {
28 append sql
[lindex $sx $i]<br
>\n
31 regsub -all \n [string trim
$sql] <br
> sql
35 puts "</blockquote><table border=0 cellpadding=0 cellspacing=0>"
36 set format {<tr
><td
>%s
</td
><td align
="right"> 
; 
; 
;%.3f
</td
></tr
>}
38 # exec sync; after $delay;
39 # set t [time "exec psql drh <$sqlfile" 1]
40 # set t [expr {[lindex $t 0]/1000000.0}]
41 # puts [format $format PostgreSQL: $t]
42 exec sync
; after $delay;
43 set t
[time "exec mysql -f drh <$sqlfile" 1]
44 set t
[expr {[lindex $t 0]/1000000.0}]
45 puts [format $format MySQL
: $t]
46 # set t [time "exec ./sqlite232 s232.db <$sqlfile" 1]
47 # set t [expr {[lindex $t 0]/1000000.0}]
48 # puts [format $format {SQLite 2.3.2:} $t]
49 # set t [time "exec ./sqlite-100 s100.db <$sqlfile" 1]
50 # set t [expr {[lindex $t 0]/1000000.0}]
51 # puts [format $format {SQLite 2.4 (cache=100):} $t]
52 exec sync
; after $delay;
53 set t
[time "exec ./sqlite248 s2k.db <$sqlfile" 1]
54 set t
[expr {[lindex $t 0]/1000000.0}]
55 puts [format $format {SQLite
2.4.8:} $t]
56 exec sync
; after $delay;
57 set t
[time "exec ./sqlite248 sns.db <$sqlfile" 1]
58 set t
[expr {[lindex $t 0]/1000000.0}]
59 puts [format $format {SQLite
2.4.8 (nosync
):} $t]
60 exec sync
; after $delay;
61 set t
[time "exec ./sqlite2412 s2kb.db <$sqlfile" 1]
62 set t
[expr {[lindex $t 0]/1000000.0}]
63 puts [format $format {SQLite
2.4.12:} $t]
64 exec sync
; after $delay;
65 set t
[time "exec ./sqlite2412 snsb.db <$sqlfile" 1]
66 set t
[expr {[lindex $t 0]/1000000.0}]
67 puts [format $format {SQLite
2.4.12 (nosync
):} $t]
68 # set t [time "exec ./sqlite-t1 st1.db <$sqlfile" 1]
69 # set t [expr {[lindex $t 0]/1000000.0}]
70 # puts [format $format {SQLite 2.4 (test):} $t]
74 # Initialize the environment
77 catch {exec /bin
/sh
-c {rm
-f s
*.db
}}
78 set fd
[open clear.sql w
]
84 catch {exec psql drh
<clear.sql
}
85 catch {exec mysql drh
<clear.sql
}
86 set fd
[open 2kinit.sql w
]
88 PRAGMA default_cache_size
=2000;
89 PRAGMA default_synchronous
=on
;
92 exec .
/sqlite248 s2k.db
<2kinit.sql
93 exec .
/sqlite2412 s2kb.db
<2kinit.sql
94 set fd
[open nosync-init.sql w
]
96 PRAGMA default_cache_size
=2000;
97 PRAGMA default_synchronous
=off
;
100 exec .
/sqlite248 sns.db
<nosync-init.sql
101 exec .
/sqlite2412 snsb.db
<nosync-init.sql
102 set ones
{zero one two three four five six seven eight nine
103 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
105 set tens
{{} ten twenty thirty forty fifty sixty seventy eighty ninety
}
106 proc number_name
{n
} {
108 set txt
"[number_name [expr {$n/1000}]] thousand"
109 set n
[expr {$n%1000}]
114 append txt
" [lindex $::ones [expr {$n/100}]] hundred"
115 set n
[expr {$n%100}]
118 append txt
" [lindex $::tens [expr {$n/10}]]"
122 append txt
" [lindex $::ones $n]"
124 set txt
[string trim
$txt]
125 if {$txt==""} {set txt zero
}
131 set fd
[open test
$cnt.sql w
]
132 puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));"
133 for {set i
1} {$i<=1000} {incr i
} {
134 set r
[expr {int
(rand
()*100000)}]
135 puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
138 runtest
{1000 INSERTs
}
142 set fd
[open test
$cnt.sql w
]
144 puts $fd "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));"
145 for {set i
1} {$i<=25000} {incr i
} {
146 set r
[expr {int
(rand
()*500000)}]
147 puts $fd "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');"
151 runtest
{25000 INSERTs in a transaction
}
155 set fd
[open test
$cnt.sql w
]
156 for {set i
0} {$i<100} {incr i
} {
157 set lwr
[expr {$i*100}]
158 set upr
[expr {($i+10)*100}]
159 puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;"
162 runtest
{100 SELECTs without an index
}
166 set fd
[open test
$cnt.sql w
]
167 for {set i
1} {$i<=100} {incr i
} {
168 puts $fd "SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%[number_name $i]%';"
171 runtest
{100 SELECTs on a
string comparison
}
175 set fd
[open test
$cnt.sql w
]
176 puts $fd {CREATE INDEX i2a ON t2
(a
);}
177 puts $fd {CREATE INDEX i2b ON t2
(b
);}
179 runtest
{Creating an index
}
183 set fd
[open test
$cnt.sql w
]
184 for {set i
0} {$i<5000} {incr i
} {
185 set lwr
[expr {$i*100}]
186 set upr
[expr {($i+1)*100}]
187 puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;"
190 runtest
{5000 SELECTs with an index
}
194 set fd
[open test
$cnt.sql w
]
196 for {set i
0} {$i<1000} {incr i
} {
197 set lwr
[expr {$i*10}]
198 set upr
[expr {($i+1)*10}]
199 puts $fd "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
203 runtest
{1000 UPDATEs without an index
}
207 set fd
[open test
$cnt.sql w
]
209 for {set i
1} {$i<=25000} {incr i
} {
210 set r
[expr {int
(rand
()*500000)}]
211 puts $fd "UPDATE t2 SET b=$r WHERE a=$i;"
215 runtest
{25000 UPDATEs with an index
}
218 set fd
[open test
$cnt.sql w
]
220 for {set i
1} {$i<=25000} {incr i
} {
221 set r
[expr {int
(rand
()*500000)}]
222 puts $fd "UPDATE t2 SET c='[number_name $r]' WHERE a=$i;"
226 runtest
{25000 text UPDATEs with an index
}
230 set fd
[open test
$cnt.sql w
]
232 puts $fd "INSERT INTO t1 SELECT * FROM t2;"
233 puts $fd "INSERT INTO t2 SELECT * FROM t1;"
236 runtest
{INSERTs from a SELECT
}
240 set fd
[open test
$cnt.sql w
]
241 puts $fd {DELETE FROM t2 WHERE c LIKE '
%fifty
%'
;}
243 runtest
{DELETE without an index
}
247 set fd
[open test
$cnt.sql w
]
248 puts $fd {DELETE FROM t2 WHERE a
>10 AND a
<20000;}
250 runtest
{DELETE with an index
}
254 set fd
[open test
$cnt.sql w
]
255 puts $fd {INSERT INTO t2 SELECT
* FROM t1
;}
257 runtest
{A big INSERT
after a big DELETE
}
261 set fd
[open test
$cnt.sql w
]
263 puts $fd {DELETE FROM t1
;}
264 for {set i
1} {$i<=3000} {incr i
} {
265 set r
[expr {int
(rand
()*100000)}]
266 puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
270 runtest
{A big DELETE followed by many small INSERTs
}
274 set fd
[open test
$cnt.sql w
]
275 puts $fd {DROP TABLE t1
;}
276 puts $fd {DROP TABLE t2
;}