speedtest2.tcl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 ./sqlite240 s2k.db <$sqlfile" 1]
  50. set t [expr {[lindex $t 0]/1000000.0}]
  51. puts [format $format {SQLite 2.4:} $t]
  52. exec sync; after $delay;
  53. set t [time "exec ./sqlite240 sns.db <$sqlfile" 1]
  54. set t [expr {[lindex $t 0]/1000000.0}]
  55. puts [format $format {SQLite 2.4 (nosync):} $t]
  56. # set t [time "exec ./sqlite-t1 st1.db <$sqlfile" 1]
  57. # set t [expr {[lindex $t 0]/1000000.0}]
  58. # puts [format $format {SQLite 2.4 (test):} $t]
  59. puts "</table>"
  60. }
  61. # Initialize the environment
  62. #
  63. expr srand(1)
  64. catch {exec /bin/sh -c {rm -f s*.db}}
  65. set fd [open clear.sql w]
  66. puts $fd {
  67. drop table t1;
  68. drop table t2;
  69. }
  70. close $fd
  71. catch {exec psql drh <clear.sql}
  72. catch {exec mysql drh <clear.sql}
  73. set fd [open 2kinit.sql w]
  74. puts $fd {
  75. PRAGMA default_cache_size=2000;
  76. PRAGMA default_synchronous=on;
  77. }
  78. close $fd
  79. exec ./sqlite240 s2k.db <2kinit.sql
  80. exec ./sqlite-t1 st1.db <2kinit.sql
  81. set fd [open nosync-init.sql w]
  82. puts $fd {
  83. PRAGMA default_cache_size=2000;
  84. PRAGMA default_synchronous=off;
  85. }
  86. close $fd
  87. exec ./sqlite240 sns.db <nosync-init.sql
  88. set ones {zero one two three four five six seven eight nine
  89. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  90. eighteen nineteen}
  91. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  92. proc number_name {n} {
  93. if {$n>=1000} {
  94. set txt "[number_name [expr {$n/1000}]] thousand"
  95. set n [expr {$n%1000}]
  96. } else {
  97. set txt {}
  98. }
  99. if {$n>=100} {
  100. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  101. set n [expr {$n%100}]
  102. }
  103. if {$n>=20} {
  104. append txt " [lindex $::tens [expr {$n/10}]]"
  105. set n [expr {$n%10}]
  106. }
  107. if {$n>0} {
  108. append txt " [lindex $::ones $n]"
  109. }
  110. set txt [string trim $txt]
  111. if {$txt==""} {set txt zero}
  112. return $txt
  113. }
  114. set fd [open test$cnt.sql w]
  115. puts $fd "BEGIN;"
  116. puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));"
  117. for {set i 1} {$i<=25000} {incr i} {
  118. set r [expr {int(rand()*500000)}]
  119. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  120. }
  121. puts $fd "COMMIT;"
  122. close $fd
  123. runtest {25000 INSERTs in a transaction}
  124. set fd [open test$cnt.sql w]
  125. puts $fd "DELETE FROM t1;"
  126. close $fd
  127. runtest {DELETE everything}
  128. set fd [open test$cnt.sql w]
  129. puts $fd "BEGIN;"
  130. for {set i 1} {$i<=25000} {incr i} {
  131. set r [expr {int(rand()*500000)}]
  132. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  133. }
  134. puts $fd "COMMIT;"
  135. close $fd
  136. runtest {25000 INSERTs in a transaction}
  137. set fd [open test$cnt.sql w]
  138. puts $fd "DELETE FROM t1;"
  139. close $fd
  140. runtest {DELETE everything}
  141. set fd [open test$cnt.sql w]
  142. puts $fd "BEGIN;"
  143. for {set i 1} {$i<=25000} {incr i} {
  144. set r [expr {int(rand()*500000)}]
  145. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  146. }
  147. puts $fd "COMMIT;"
  148. close $fd
  149. runtest {25000 INSERTs in a transaction}
  150. set fd [open test$cnt.sql w]
  151. puts $fd "DELETE FROM t1;"
  152. close $fd
  153. runtest {DELETE everything}
  154. set fd [open test$cnt.sql w]
  155. puts $fd "BEGIN;"
  156. for {set i 1} {$i<=25000} {incr i} {
  157. set r [expr {int(rand()*500000)}]
  158. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  159. }
  160. puts $fd "COMMIT;"
  161. close $fd
  162. runtest {25000 INSERTs in a transaction}
  163. set fd [open test$cnt.sql w]
  164. puts $fd "DELETE FROM t1;"
  165. close $fd
  166. runtest {DELETE everything}
  167. set fd [open test$cnt.sql w]
  168. puts $fd "BEGIN;"
  169. for {set i 1} {$i<=25000} {incr i} {
  170. set r [expr {int(rand()*500000)}]
  171. puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');"
  172. }
  173. puts $fd "COMMIT;"
  174. close $fd
  175. runtest {25000 INSERTs in a transaction}
  176. set fd [open test$cnt.sql w]
  177. puts $fd "DELETE FROM t1;"
  178. close $fd
  179. runtest {DELETE everything}
  180. set fd [open test$cnt.sql w]
  181. puts $fd {DROP TABLE t1;}
  182. close $fd
  183. runtest {DROP TABLE}