benchmark_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // by Brad Fitzpatrick
  46. const concurrencyLevel = 10
  47. func BenchmarkQuery(b *testing.B) {
  48. tb := (*TB)(b)
  49. b.StopTimer()
  50. b.ReportAllocs()
  51. db := initDB(b,
  52. "DROP TABLE IF EXISTS foo",
  53. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  54. `INSERT INTO foo VALUES (1, "one")`,
  55. `INSERT INTO foo VALUES (2, "two")`,
  56. )
  57. db.SetMaxIdleConns(concurrencyLevel)
  58. defer db.Close()
  59. stmt := tb.checkStmt(db.Prepare("SELECT val FROM foo WHERE id=?"))
  60. defer stmt.Close()
  61. remain := int64(b.N)
  62. var wg sync.WaitGroup
  63. wg.Add(concurrencyLevel)
  64. defer wg.Wait()
  65. b.StartTimer()
  66. for i := 0; i < concurrencyLevel; i++ {
  67. go func() {
  68. for {
  69. if atomic.AddInt64(&remain, -1) < 0 {
  70. wg.Done()
  71. return
  72. }
  73. var got string
  74. tb.check(stmt.QueryRow(1).Scan(&got))
  75. if got != "one" {
  76. b.Errorf("query = %q; want one", got)
  77. wg.Done()
  78. return
  79. }
  80. }
  81. }()
  82. }
  83. }
  84. // data, but no db writes
  85. var roundtripSample []byte
  86. func initRoundtripBenchmarks() ([]byte, int, int) {
  87. if roundtripSample == nil {
  88. roundtripSample = []byte(strings.Repeat("0123456789abcdef", 1024*1024))
  89. }
  90. return roundtripSample, 16, len(roundtripSample)
  91. }
  92. func BenchmarkRoundtripTxt(b *testing.B) {
  93. b.StopTimer()
  94. sample, min, max := initRoundtripBenchmarks()
  95. sampleString := string(sample)
  96. b.ReportAllocs()
  97. tb := (*TB)(b)
  98. db := tb.checkDB(sql.Open("mysql", dsn))
  99. defer db.Close()
  100. b.StartTimer()
  101. var result string
  102. for i := 0; i < b.N; i++ {
  103. length := min + i
  104. if length > max {
  105. length = max
  106. }
  107. test := sampleString[0:length]
  108. rows := tb.checkRows(db.Query(`SELECT "` + test + `"`))
  109. if !rows.Next() {
  110. rows.Close()
  111. b.Fatalf("crashed")
  112. }
  113. err := rows.Scan(&result)
  114. if err != nil {
  115. rows.Close()
  116. b.Fatalf("crashed")
  117. }
  118. if result != test {
  119. rows.Close()
  120. b.Errorf("mismatch")
  121. }
  122. rows.Close()
  123. }
  124. }
  125. func BenchmarkRoundtripBin(b *testing.B) {
  126. b.StopTimer()
  127. sample, min, max := initRoundtripBenchmarks()
  128. b.ReportAllocs()
  129. tb := (*TB)(b)
  130. db := tb.checkDB(sql.Open("mysql", dsn))
  131. defer db.Close()
  132. stmt := tb.checkStmt(db.Prepare("SELECT ?"))
  133. defer stmt.Close()
  134. b.StartTimer()
  135. var result sql.RawBytes
  136. for i := 0; i < b.N; i++ {
  137. length := min + i
  138. if length > max {
  139. length = max
  140. }
  141. test := sample[0:length]
  142. rows := tb.checkRows(stmt.Query(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 !bytes.Equal(result, test) {
  153. rows.Close()
  154. b.Errorf("mismatch")
  155. }
  156. rows.Close()
  157. }
  158. }