benchmark_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. if w, ok := err.(MySQLWarnings); ok {
  44. b.Logf("warning on %q: %v", query, w)
  45. } else {
  46. b.Fatalf("error on %q: %v", query, err)
  47. }
  48. }
  49. }
  50. return db
  51. }
  52. const concurrencyLevel = 10
  53. func BenchmarkQuery(b *testing.B) {
  54. tb := (*TB)(b)
  55. b.StopTimer()
  56. b.ReportAllocs()
  57. db := initDB(b,
  58. "DROP TABLE IF EXISTS foo",
  59. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  60. `INSERT INTO foo VALUES (1, "one")`,
  61. `INSERT INTO foo VALUES (2, "two")`,
  62. )
  63. db.SetMaxIdleConns(concurrencyLevel)
  64. defer db.Close()
  65. stmt := tb.checkStmt(db.Prepare("SELECT val FROM foo WHERE id=?"))
  66. defer stmt.Close()
  67. remain := int64(b.N)
  68. var wg sync.WaitGroup
  69. wg.Add(concurrencyLevel)
  70. defer wg.Wait()
  71. b.StartTimer()
  72. for i := 0; i < concurrencyLevel; i++ {
  73. go func() {
  74. for {
  75. if atomic.AddInt64(&remain, -1) < 0 {
  76. wg.Done()
  77. return
  78. }
  79. var got string
  80. tb.check(stmt.QueryRow(1).Scan(&got))
  81. if got != "one" {
  82. b.Errorf("query = %q; want one", got)
  83. wg.Done()
  84. return
  85. }
  86. }
  87. }()
  88. }
  89. }
  90. func BenchmarkExec(b *testing.B) {
  91. tb := (*TB)(b)
  92. b.StopTimer()
  93. b.ReportAllocs()
  94. db := tb.checkDB(sql.Open("mysql", dsn))
  95. db.SetMaxIdleConns(concurrencyLevel)
  96. defer db.Close()
  97. stmt := tb.checkStmt(db.Prepare("DO 1"))
  98. defer stmt.Close()
  99. remain := int64(b.N)
  100. var wg sync.WaitGroup
  101. wg.Add(concurrencyLevel)
  102. defer wg.Wait()
  103. b.StartTimer()
  104. for i := 0; i < concurrencyLevel; i++ {
  105. go func() {
  106. for {
  107. if atomic.AddInt64(&remain, -1) < 0 {
  108. wg.Done()
  109. return
  110. }
  111. if _, err := stmt.Exec(); err != nil {
  112. b.Fatal(err.Error())
  113. }
  114. }
  115. }()
  116. }
  117. }
  118. // data, but no db writes
  119. var roundtripSample []byte
  120. func initRoundtripBenchmarks() ([]byte, int, int) {
  121. if roundtripSample == nil {
  122. roundtripSample = []byte(strings.Repeat("0123456789abcdef", 1024*1024))
  123. }
  124. return roundtripSample, 16, len(roundtripSample)
  125. }
  126. func BenchmarkRoundtripTxt(b *testing.B) {
  127. b.StopTimer()
  128. sample, min, max := initRoundtripBenchmarks()
  129. sampleString := string(sample)
  130. b.ReportAllocs()
  131. tb := (*TB)(b)
  132. db := tb.checkDB(sql.Open("mysql", dsn))
  133. defer db.Close()
  134. b.StartTimer()
  135. var result string
  136. for i := 0; i < b.N; i++ {
  137. length := min + i
  138. if length > max {
  139. length = max
  140. }
  141. test := sampleString[0:length]
  142. rows := tb.checkRows(db.Query(`SELECT "` + test + `"`))
  143. if !rows.Next() {
  144. rows.Close()
  145. b.Fatalf("crashed")
  146. }
  147. err := rows.Scan(&result)
  148. if err != nil {
  149. rows.Close()
  150. b.Fatalf("crashed")
  151. }
  152. if result != test {
  153. rows.Close()
  154. b.Errorf("mismatch")
  155. }
  156. rows.Close()
  157. }
  158. }
  159. func BenchmarkRoundtripBin(b *testing.B) {
  160. b.StopTimer()
  161. sample, min, max := initRoundtripBenchmarks()
  162. b.ReportAllocs()
  163. tb := (*TB)(b)
  164. db := tb.checkDB(sql.Open("mysql", dsn))
  165. defer db.Close()
  166. stmt := tb.checkStmt(db.Prepare("SELECT ?"))
  167. defer stmt.Close()
  168. b.StartTimer()
  169. var result sql.RawBytes
  170. for i := 0; i < b.N; i++ {
  171. length := min + i
  172. if length > max {
  173. length = max
  174. }
  175. test := sample[0:length]
  176. rows := tb.checkRows(stmt.Query(test))
  177. if !rows.Next() {
  178. rows.Close()
  179. b.Fatalf("crashed")
  180. }
  181. err := rows.Scan(&result)
  182. if err != nil {
  183. rows.Close()
  184. b.Fatalf("crashed")
  185. }
  186. if !bytes.Equal(result, test) {
  187. rows.Close()
  188. b.Errorf("mismatch")
  189. }
  190. rows.Close()
  191. }
  192. }
  193. func BenchmarkInterpolation(b *testing.B) {
  194. mc := &mysqlConn{
  195. cfg: &Config{
  196. InterpolateParams: true,
  197. Loc: time.UTC,
  198. },
  199. maxAllowedPacket: maxPacketSize,
  200. maxWriteSize: maxPacketSize - 1,
  201. buf: newBuffer(nil),
  202. }
  203. args := []driver.Value{
  204. int64(42424242),
  205. float64(math.Pi),
  206. false,
  207. time.Unix(1423411542, 807015000),
  208. []byte("bytes containing special chars ' \" \a \x00"),
  209. "string containing special chars ' \" \a \x00",
  210. }
  211. q := "SELECT ?, ?, ?, ?, ?, ?"
  212. b.ReportAllocs()
  213. b.ResetTimer()
  214. for i := 0; i < b.N; i++ {
  215. _, err := mc.interpolateParams(q, args)
  216. if err != nil {
  217. b.Fatal(err)
  218. }
  219. }
  220. }