benchmark_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
  2. //
  3. // Copyright 2013 The Go-MySQL-Driver Authors. All rights reserved.
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public
  6. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  7. // You can obtain one at http://mozilla.org/MPL/2.0/.
  8. package mysql
  9. import (
  10. "bytes"
  11. "database/sql"
  12. "database/sql/driver"
  13. "math"
  14. "strings"
  15. "sync"
  16. "sync/atomic"
  17. "testing"
  18. "time"
  19. )
  20. type TB testing.B
  21. func (tb *TB) check(err error) {
  22. if err != nil {
  23. tb.Fatal(err)
  24. }
  25. }
  26. func (tb *TB) checkDB(db *sql.DB, err error) *sql.DB {
  27. tb.check(err)
  28. return db
  29. }
  30. func (tb *TB) checkRows(rows *sql.Rows, err error) *sql.Rows {
  31. tb.check(err)
  32. return rows
  33. }
  34. func (tb *TB) checkStmt(stmt *sql.Stmt, err error) *sql.Stmt {
  35. tb.check(err)
  36. return stmt
  37. }
  38. func initDB(b *testing.B, queries ...string) *sql.DB {
  39. tb := (*TB)(b)
  40. db := tb.checkDB(sql.Open("mysql", dsn))
  41. for _, query := range queries {
  42. if _, err := db.Exec(query); err != nil {
  43. b.Fatalf("error on %q: %v", query, err)
  44. }
  45. }
  46. return db
  47. }
  48. const concurrencyLevel = 10
  49. func BenchmarkQuery(b *testing.B) {
  50. tb := (*TB)(b)
  51. b.StopTimer()
  52. b.ReportAllocs()
  53. db := initDB(b,
  54. "DROP TABLE IF EXISTS foo",
  55. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  56. `INSERT INTO foo VALUES (1, "one")`,
  57. `INSERT INTO foo VALUES (2, "two")`,
  58. )
  59. db.SetMaxIdleConns(concurrencyLevel)
  60. defer db.Close()
  61. stmt := tb.checkStmt(db.Prepare("SELECT val FROM foo WHERE id=?"))
  62. defer stmt.Close()
  63. remain := int64(b.N)
  64. var wg sync.WaitGroup
  65. wg.Add(concurrencyLevel)
  66. defer wg.Wait()
  67. b.StartTimer()
  68. for i := 0; i < concurrencyLevel; i++ {
  69. go func() {
  70. for {
  71. if atomic.AddInt64(&remain, -1) < 0 {
  72. wg.Done()
  73. return
  74. }
  75. var got string
  76. tb.check(stmt.QueryRow(1).Scan(&got))
  77. if got != "one" {
  78. b.Errorf("query = %q; want one", got)
  79. wg.Done()
  80. return
  81. }
  82. }
  83. }()
  84. }
  85. }
  86. func BenchmarkExec(b *testing.B) {
  87. tb := (*TB)(b)
  88. b.StopTimer()
  89. b.ReportAllocs()
  90. db := tb.checkDB(sql.Open("mysql", dsn))
  91. db.SetMaxIdleConns(concurrencyLevel)
  92. defer db.Close()
  93. stmt := tb.checkStmt(db.Prepare("DO 1"))
  94. defer stmt.Close()
  95. remain := int64(b.N)
  96. var wg sync.WaitGroup
  97. wg.Add(concurrencyLevel)
  98. defer wg.Wait()
  99. b.StartTimer()
  100. for i := 0; i < concurrencyLevel; i++ {
  101. go func() {
  102. for {
  103. if atomic.AddInt64(&remain, -1) < 0 {
  104. wg.Done()
  105. return
  106. }
  107. if _, err := stmt.Exec(); err != nil {
  108. b.Fatal(err.Error())
  109. }
  110. }
  111. }()
  112. }
  113. }
  114. // data, but no db writes
  115. var roundtripSample []byte
  116. func initRoundtripBenchmarks() ([]byte, int, int) {
  117. if roundtripSample == nil {
  118. roundtripSample = []byte(strings.Repeat("0123456789abcdef", 1024*1024))
  119. }
  120. return roundtripSample, 16, len(roundtripSample)
  121. }
  122. func BenchmarkRoundtripTxt(b *testing.B) {
  123. b.StopTimer()
  124. sample, min, max := initRoundtripBenchmarks()
  125. sampleString := string(sample)
  126. b.ReportAllocs()
  127. tb := (*TB)(b)
  128. db := tb.checkDB(sql.Open("mysql", dsn))
  129. defer db.Close()
  130. b.StartTimer()
  131. var result string
  132. for i := 0; i < b.N; i++ {
  133. length := min + i
  134. if length > max {
  135. length = max
  136. }
  137. test := sampleString[0:length]
  138. rows := tb.checkRows(db.Query(`SELECT "` + test + `"`))
  139. if !rows.Next() {
  140. rows.Close()
  141. b.Fatalf("crashed")
  142. }
  143. err := rows.Scan(&result)
  144. if err != nil {
  145. rows.Close()
  146. b.Fatalf("crashed")
  147. }
  148. if result != test {
  149. rows.Close()
  150. b.Errorf("mismatch")
  151. }
  152. rows.Close()
  153. }
  154. }
  155. func BenchmarkRoundtripBin(b *testing.B) {
  156. b.StopTimer()
  157. sample, min, max := initRoundtripBenchmarks()
  158. b.ReportAllocs()
  159. tb := (*TB)(b)
  160. db := tb.checkDB(sql.Open("mysql", dsn))
  161. defer db.Close()
  162. stmt := tb.checkStmt(db.Prepare("SELECT ?"))
  163. defer stmt.Close()
  164. b.StartTimer()
  165. var result sql.RawBytes
  166. for i := 0; i < b.N; i++ {
  167. length := min + i
  168. if length > max {
  169. length = max
  170. }
  171. test := sample[0:length]
  172. rows := tb.checkRows(stmt.Query(test))
  173. if !rows.Next() {
  174. rows.Close()
  175. b.Fatalf("crashed")
  176. }
  177. err := rows.Scan(&result)
  178. if err != nil {
  179. rows.Close()
  180. b.Fatalf("crashed")
  181. }
  182. if !bytes.Equal(result, test) {
  183. rows.Close()
  184. b.Errorf("mismatch")
  185. }
  186. rows.Close()
  187. }
  188. }
  189. func BenchmarkInterpolation(b *testing.B) {
  190. mc := &mysqlConn{
  191. cfg: &Config{
  192. InterpolateParams: true,
  193. Loc: time.UTC,
  194. },
  195. maxAllowedPacket: maxPacketSize,
  196. maxWriteSize: maxPacketSize - 1,
  197. buf: newBuffer(nil),
  198. }
  199. args := []driver.Value{
  200. int64(42424242),
  201. float64(math.Pi),
  202. false,
  203. time.Unix(1423411542, 807015000),
  204. []byte("bytes containing special chars ' \" \a \x00"),
  205. "string containing special chars ' \" \a \x00",
  206. }
  207. q := "SELECT ?, ?, ?, ?, ?, ?"
  208. b.ReportAllocs()
  209. b.ResetTimer()
  210. for i := 0; i < b.N; i++ {
  211. _, err := mc.interpolateParams(q, args)
  212. if err != nil {
  213. b.Fatal(err)
  214. }
  215. }
  216. }