speedtest.tcl 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/tclsh
  2. #
  3. # Run this script using TCLSH to do a speed comparison between
  4. # various versions of SQLite and PostgreSQL and MySQL
  5. #
  6. # Run a test
  7. #
  8. set cnt 1
  9. proc runtest {title} {
  10. global cnt
  11. set sqlfile test$cnt.sql
  12. puts "<h2>Test $cnt: $title</h2>"
  13. incr cnt
  14. set fd [open $sqlfile r]
  15. set sql [string trim [read $fd [file size $sqlfile]]]
  16. close $fd
  17. set sx [split $sql \n]
  18. set n [llength $sx]
  19. if {$n>8} {
  20. set sql {}
  21. for {set i 0} {$i<3} {incr i} {append sql [lindex $sx $i]<br>\n}
  22. append sql "<i>... [expr {$n-6}] lines omitted</i><br>\n"
  23. for {set i [expr {$n-3}]} {$i<$n} {incr i} {
  24. append sql [lindex $sx $i]<br>\n
  25. }
  26. } else {
  27. regsub -all \n [string trim $sql] <br> sql
  28. }
  29. puts "<blockquote>"
  30. puts "$sql"
  31. puts "</blockquote><table border=0 cellpadding=0 cellspacing=0>"
  32. set format {<tr><td>%s</td><td align="right">&nbsp;&nbsp;&nbsp;%.3f</td></tr>}
  33. set delay 1000
  34. # exec sync; after $delay;
  35. # set t [time "exec psql drh <$sqlfile" 1]
  36. # set t [expr {[lindex $t 0]/1000000.0}]
  37. # puts [format $format PostgreSQL: $t]
  38. exec sync; after $delay;
  39. set t [time "exec mysql -f drh <$sqlfile" 1]
  40. set t [expr {[lindex $t 0]/1000000.0}]
  41. puts [format $format MySQL: $t]
  42. # set t [time "exec ./sqlite232 s232.db <$sqlfile" 1]
  43. # set t [expr {[lindex $t 0]/1000000.0}]
  44. # puts [format $format {SQLite 2.3.2:} $t]
  45. # set t [time "exec ./sqlite-100 s100.db <$sqlfile" 1]
  46. # set t [expr {[lindex $t 0]/1000000.0}]
  47. # puts [format $format {SQLite 2.4 (cache=100):} $t]
  48. exec sync; after $delay;
  49. set t [time "exec ./sqlite248 s2k.db <$sqlfile" 1]
  50. set t [expr {[lindex $t 0]/1000000.0}]
  51. puts [format $format {SQLite 2.4.8:} $t]
  52. exec sync; after $delay;
  53. set t [time "exec ./sqlite248 sns.db <$sqlfile" 1]
  54. set t [expr {[lindex $t 0]/1000000.0}]
  55. puts [format $format {SQLite 2.4.8 (nosync):} $t]
  56. exec sync; after $delay;
  57. set t [time "exec ./sqlite2412 s2kb.db <$sqlfile" 1]
  58. set t [expr {[lindex $t 0]/1000000.0}]
  59. puts [format $format {SQLite 2.4.12:} $t]
  60. exec sync; after $delay;
  61. set t [time "exec ./sqlite2412 snsb.db <$sqlfile" 1]
  62. set t [expr {[lindex $t 0]/1000000.0}]
  63. puts [format $format {SQLite 2.4.12 (nosync):} $t]
  64. # set t [time "exec ./sqlite-t1 st1.db <$sqlfile" 1]
  65. # set t [expr {[lindex $t 0]/1000000.0}]
  66. # puts [format $format {SQLite 2.4 (test):} $t]
  67. puts "</table>"
  68. }
  69. # Initialize the environment
  70. #
  71. expr srand(1)
  72. catch {exec /bin/sh -c {rm -f s*.db}}
  73. set fd [open clear.sql w]
  74. puts $fd {
  75. drop table t1;
  76. drop table t2;
  77. }
  78. close $fd
  79. catch {exec psql drh <clear.sql}
  80. catch {exec mysql drh <clear.sql}
  81. set fd [open 2kinit.sql w]
  82. puts $fd {
  83. PRAGMA default_cache_size=2000;
  84. PRAGMA default_synchronous=on;
  85. }
  86. close $fd
  87. exec ./sqlite248 s2k.db <2kinit.sql
  88. exec ./sqlite2412 s2kb.db <2kinit.sql
  89. set fd [open nosync-init.sql w]
  90. puts $fd {
  91. PRAGMA default_cache_size=2000;
  92. PRAGMA default_synchronous=off;
  93. }
  94. close $fd
  95. exec ./sqlite248 sns.db <nosync-init.sql
  96. exec ./sqlite2412 snsb.db <nosync-init.sql
  97. set ones {zero one two three four five six seven eight nine
  98. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  99. eighteen nineteen}
  100. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  101. proc number_name {n} {
  102. if {$n>=1000} {
  103. set txt "[number_name [expr {$n/1000}]] thousand"
  104. set n [expr {$n%1000}]
  105. } else {
  106. set txt {}
  107. }
  108. if {$n>=100} {
  109. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  110. set n [expr {$n%100}]
  111. }
  112. if {$n>=20} {
  113. append txt " [lindex $::tens [expr {$n/10}]]"
  114. set n [expr {$n%10}]
  115. }
  116. if {$n>0} {
  117. append txt " [lindex $::ones $n]"
  118. }
  119. set txt [string trim $txt]
  120. if {$txt==""} {set txt zero}
  121. return $txt
  122. }
  123. set fd [open test$cnt.sql w]
  124. puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));"
  125. for {set i 1} {$i<=1000} {incr i} {
  126. set r [expr {int(rand()*100000)}]
  127. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  128. }
  129. close $fd
  130. runtest {1000 INSERTs}
  131. set fd [open test$cnt.sql w]
  132. puts $fd "BEGIN;"
  133. puts $fd "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));"
  134. for {set i 1} {$i<=25000} {incr i} {
  135. set r [expr {int(rand()*500000)}]
  136. puts $fd "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');"
  137. }
  138. puts $fd "COMMIT;"
  139. close $fd
  140. runtest {25000 INSERTs in a transaction}
  141. set fd [open test$cnt.sql w]
  142. for {set i 0} {$i<100} {incr i} {
  143. set lwr [expr {$i*100}]
  144. set upr [expr {($i+10)*100}]
  145. puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;"
  146. }
  147. close $fd
  148. runtest {100 SELECTs without an index}
  149. set fd [open test$cnt.sql w]
  150. for {set i 1} {$i<=100} {incr i} {
  151. puts $fd "SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%[number_name $i]%';"
  152. }
  153. close $fd
  154. runtest {100 SELECTs on a string comparison}
  155. set fd [open test$cnt.sql w]
  156. puts $fd {CREATE INDEX i2a ON t2(a);}
  157. puts $fd {CREATE INDEX i2b ON t2(b);}
  158. close $fd
  159. runtest {Creating an index}
  160. set fd [open test$cnt.sql w]
  161. for {set i 0} {$i<5000} {incr i} {
  162. set lwr [expr {$i*100}]
  163. set upr [expr {($i+1)*100}]
  164. puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;"
  165. }
  166. close $fd
  167. runtest {5000 SELECTs with an index}
  168. set fd [open test$cnt.sql w]
  169. puts $fd "BEGIN;"
  170. for {set i 0} {$i<1000} {incr i} {
  171. set lwr [expr {$i*10}]
  172. set upr [expr {($i+1)*10}]
  173. puts $fd "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
  174. }
  175. puts $fd "COMMIT;"
  176. close $fd
  177. runtest {1000 UPDATEs without an index}
  178. set fd [open test$cnt.sql w]
  179. puts $fd "BEGIN;"
  180. for {set i 1} {$i<=25000} {incr i} {
  181. set r [expr {int(rand()*500000)}]
  182. puts $fd "UPDATE t2 SET b=$r WHERE a=$i;"
  183. }
  184. puts $fd "COMMIT;"
  185. close $fd
  186. runtest {25000 UPDATEs with an index}
  187. set fd [open test$cnt.sql w]
  188. puts $fd "BEGIN;"
  189. for {set i 1} {$i<=25000} {incr i} {
  190. set r [expr {int(rand()*500000)}]
  191. puts $fd "UPDATE t2 SET c='[number_name $r]' WHERE a=$i;"
  192. }
  193. puts $fd "COMMIT;"
  194. close $fd
  195. runtest {25000 text UPDATEs with an index}
  196. set fd [open test$cnt.sql w]
  197. puts $fd "BEGIN;"
  198. puts $fd "INSERT INTO t1 SELECT * FROM t2;"
  199. puts $fd "INSERT INTO t2 SELECT * FROM t1;"
  200. puts $fd "COMMIT;"
  201. close $fd
  202. runtest {INSERTs from a SELECT}
  203. set fd [open test$cnt.sql w]
  204. puts $fd {DELETE FROM t2 WHERE c LIKE '%fifty%';}
  205. close $fd
  206. runtest {DELETE without an index}
  207. set fd [open test$cnt.sql w]
  208. puts $fd {DELETE FROM t2 WHERE a>10 AND a<20000;}
  209. close $fd
  210. runtest {DELETE with an index}
  211. set fd [open test$cnt.sql w]
  212. puts $fd {INSERT INTO t2 SELECT * FROM t1;}
  213. close $fd
  214. runtest {A big INSERT after a big DELETE}
  215. set fd [open test$cnt.sql w]
  216. puts $fd {BEGIN;}
  217. puts $fd {DELETE FROM t1;}
  218. for {set i 1} {$i<=3000} {incr i} {
  219. set r [expr {int(rand()*100000)}]
  220. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  221. }
  222. puts $fd {COMMIT;}
  223. close $fd
  224. runtest {A big DELETE followed by many small INSERTs}
  225. set fd [open test$cnt.sql w]
  226. puts $fd {DROP TABLE t1;}
  227. puts $fd {DROP TABLE t2;}
  228. close $fd
  229. runtest {DROP TABLE}