benchmark_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. "strings"
  13. "sync"
  14. "sync/atomic"
  15. "testing"
  16. )
  17. type TB testing.B
  18. func (tb *TB) check(err error) {
  19. if err != nil {
  20. tb.Fatal(err)
  21. }
  22. }
  23. func (tb *TB) checkDB(db *sql.DB, err error) *sql.DB {
  24. tb.check(err)
  25. return db
  26. }
  27. func (tb *TB) checkRows(rows *sql.Rows, err error) *sql.Rows {
  28. tb.check(err)
  29. return rows
  30. }
  31. func (tb *TB) checkStmt(stmt *sql.Stmt, err error) *sql.Stmt {
  32. tb.check(err)
  33. return stmt
  34. }
  35. func initDB(b *testing.B, queries ...string) *sql.DB {
  36. tb := (*TB)(b)
  37. db := tb.checkDB(sql.Open("mysql", dsn))
  38. for _, query := range queries {
  39. if _, err := db.Exec(query); err != nil {
  40. b.Fatalf("Error on %q: %v", query, err)
  41. }
  42. }
  43. return db
  44. }
  45. const concurrencyLevel = 10
  46. func BenchmarkQuery(b *testing.B) {
  47. tb := (*TB)(b)
  48. b.StopTimer()
  49. b.ReportAllocs()
  50. db := initDB(b,
  51. "DROP TABLE IF EXISTS foo",
  52. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  53. `INSERT INTO foo VALUES (1, "one")`,
  54. `INSERT INTO foo VALUES (2, "two")`,
  55. )
  56. db.SetMaxIdleConns(concurrencyLevel)
  57. defer db.Close()
  58. stmt := tb.checkStmt(db.Prepare("SELECT val FROM foo WHERE id=?"))
  59. defer stmt.Close()
  60. remain := int64(b.N)
  61. var wg sync.WaitGroup
  62. wg.Add(concurrencyLevel)
  63. defer wg.Wait()
  64. b.StartTimer()
  65. for i := 0; i < concurrencyLevel; i++ {
  66. go func() {
  67. for {
  68. if atomic.AddInt64(&remain, -1) < 0 {
  69. wg.Done()
  70. return
  71. }
  72. var got string
  73. tb.check(stmt.QueryRow(1).Scan(&got))
  74. if got != "one" {
  75. b.Errorf("query = %q; want one", got)
  76. wg.Done()
  77. return
  78. }
  79. }
  80. }()
  81. }
  82. }
  83. func BenchmarkExec(b *testing.B) {
  84. tb := (*TB)(b)
  85. b.StopTimer()
  86. b.ReportAllocs()
  87. db := tb.checkDB(sql.Open("mysql", dsn))
  88. db.SetMaxIdleConns(concurrencyLevel)
  89. defer db.Close()
  90. stmt := tb.checkStmt(db.Prepare("DO 1"))
  91. defer stmt.Close()
  92. remain := int64(b.N)
  93. var wg sync.WaitGroup
  94. wg.Add(concurrencyLevel)
  95. defer wg.Wait()
  96. b.StartTimer()
  97. for i := 0; i < concurrencyLevel; i++ {
  98. go func() {
  99. for {
  100. if atomic.AddInt64(&remain, -1) < 0 {
  101. wg.Done()
  102. return
  103. }
  104. if _, err := stmt.Exec(); err != nil {
  105. b.Fatal(err.Error())
  106. }
  107. }
  108. }()
  109. }
  110. }
  111. // data, but no db writes
  112. var roundtripSample []byte
  113. func initRoundtripBenchmarks() ([]byte, int, int) {
  114. if roundtripSample == nil {
  115. roundtripSample = []byte(strings.Repeat("0123456789abcdef", 1024*1024))
  116. }
  117. return roundtripSample, 16, len(roundtripSample)
  118. }
  119. func BenchmarkRoundtripTxt(b *testing.B) {
  120. b.StopTimer()
  121. sample, min, max := initRoundtripBenchmarks()
  122. sampleString := string(sample)
  123. b.ReportAllocs()
  124. tb := (*TB)(b)
  125. db := tb.checkDB(sql.Open("mysql", dsn))
  126. defer db.Close()
  127. b.StartTimer()
  128. var result string
  129. for i := 0; i < b.N; i++ {
  130. length := min + i
  131. if length > max {
  132. length = max
  133. }
  134. test := sampleString[0:length]
  135. rows := tb.checkRows(db.Query(`SELECT "` + test + `"`))
  136. if !rows.Next() {
  137. rows.Close()
  138. b.Fatalf("crashed")
  139. }
  140. err := rows.Scan(&result)
  141. if err != nil {
  142. rows.Close()
  143. b.Fatalf("crashed")
  144. }
  145. if result != test {
  146. rows.Close()
  147. b.Errorf("mismatch")
  148. }
  149. rows.Close()
  150. }
  151. }
  152. func BenchmarkRoundtripBin(b *testing.B) {
  153. b.StopTimer()
  154. sample, min, max := initRoundtripBenchmarks()
  155. b.ReportAllocs()
  156. tb := (*TB)(b)
  157. db := tb.checkDB(sql.Open("mysql", dsn))
  158. defer db.Close()
  159. stmt := tb.checkStmt(db.Prepare("SELECT ?"))
  160. defer stmt.Close()
  161. b.StartTimer()
  162. var result sql.RawBytes
  163. for i := 0; i < b.N; i++ {
  164. length := min + i
  165. if length > max {
  166. length = max
  167. }
  168. test := sample[0:length]
  169. rows := tb.checkRows(stmt.Query(test))
  170. if !rows.Next() {
  171. rows.Close()
  172. b.Fatalf("crashed")
  173. }
  174. err := rows.Scan(&result)
  175. if err != nil {
  176. rows.Close()
  177. b.Fatalf("crashed")
  178. }
  179. if !bytes.Equal(result, test) {
  180. rows.Close()
  181. b.Errorf("mismatch")
  182. }
  183. rows.Close()
  184. }
  185. }