benchmark_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. "context"
  12. "database/sql"
  13. "database/sql/driver"
  14. "fmt"
  15. "math"
  16. "runtime"
  17. "strings"
  18. "sync"
  19. "sync/atomic"
  20. "testing"
  21. "time"
  22. )
  23. type TB testing.B
  24. func (tb *TB) check(err error) {
  25. if err != nil {
  26. tb.Fatal(err)
  27. }
  28. }
  29. func (tb *TB) checkDB(db *sql.DB, err error) *sql.DB {
  30. tb.check(err)
  31. return db
  32. }
  33. func (tb *TB) checkRows(rows *sql.Rows, err error) *sql.Rows {
  34. tb.check(err)
  35. return rows
  36. }
  37. func (tb *TB) checkStmt(stmt *sql.Stmt, err error) *sql.Stmt {
  38. tb.check(err)
  39. return stmt
  40. }
  41. func initDB(b *testing.B, queries ...string) *sql.DB {
  42. tb := (*TB)(b)
  43. db := tb.checkDB(sql.Open("mysql", dsn))
  44. for _, query := range queries {
  45. if _, err := db.Exec(query); err != nil {
  46. b.Fatalf("error on %q: %v", query, err)
  47. }
  48. }
  49. return db
  50. }
  51. const concurrencyLevel = 10
  52. func BenchmarkQuery(b *testing.B) {
  53. tb := (*TB)(b)
  54. b.StopTimer()
  55. b.ReportAllocs()
  56. db := initDB(b,
  57. "DROP TABLE IF EXISTS foo",
  58. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  59. `INSERT INTO foo VALUES (1, "one")`,
  60. `INSERT INTO foo VALUES (2, "two")`,
  61. )
  62. db.SetMaxIdleConns(concurrencyLevel)
  63. defer db.Close()
  64. stmt := tb.checkStmt(db.Prepare("SELECT val FROM foo WHERE id=?"))
  65. defer stmt.Close()
  66. remain := int64(b.N)
  67. var wg sync.WaitGroup
  68. wg.Add(concurrencyLevel)
  69. defer wg.Wait()
  70. b.StartTimer()
  71. for i := 0; i < concurrencyLevel; i++ {
  72. go func() {
  73. for {
  74. if atomic.AddInt64(&remain, -1) < 0 {
  75. wg.Done()
  76. return
  77. }
  78. var got string
  79. tb.check(stmt.QueryRow(1).Scan(&got))
  80. if got != "one" {
  81. b.Errorf("query = %q; want one", got)
  82. wg.Done()
  83. return
  84. }
  85. }
  86. }()
  87. }
  88. }
  89. func BenchmarkExec(b *testing.B) {
  90. tb := (*TB)(b)
  91. b.StopTimer()
  92. b.ReportAllocs()
  93. db := tb.checkDB(sql.Open("mysql", dsn))
  94. db.SetMaxIdleConns(concurrencyLevel)
  95. defer db.Close()
  96. stmt := tb.checkStmt(db.Prepare("DO 1"))
  97. defer stmt.Close()
  98. remain := int64(b.N)
  99. var wg sync.WaitGroup
  100. wg.Add(concurrencyLevel)
  101. defer wg.Wait()
  102. b.StartTimer()
  103. for i := 0; i < concurrencyLevel; i++ {
  104. go func() {
  105. for {
  106. if atomic.AddInt64(&remain, -1) < 0 {
  107. wg.Done()
  108. return
  109. }
  110. if _, err := stmt.Exec(); err != nil {
  111. b.Logf("stmt.Exec failed: %v", err)
  112. b.Fail()
  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. }
  221. func benchmarkQueryContext(b *testing.B, db *sql.DB, p int) {
  222. ctx, cancel := context.WithCancel(context.Background())
  223. defer cancel()
  224. db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
  225. tb := (*TB)(b)
  226. stmt := tb.checkStmt(db.PrepareContext(ctx, "SELECT val FROM foo WHERE id=?"))
  227. defer stmt.Close()
  228. b.SetParallelism(p)
  229. b.ReportAllocs()
  230. b.ResetTimer()
  231. b.RunParallel(func(pb *testing.PB) {
  232. var got string
  233. for pb.Next() {
  234. tb.check(stmt.QueryRow(1).Scan(&got))
  235. if got != "one" {
  236. b.Fatalf("query = %q; want one", got)
  237. }
  238. }
  239. })
  240. }
  241. func BenchmarkQueryContext(b *testing.B) {
  242. db := initDB(b,
  243. "DROP TABLE IF EXISTS foo",
  244. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  245. `INSERT INTO foo VALUES (1, "one")`,
  246. `INSERT INTO foo VALUES (2, "two")`,
  247. )
  248. defer db.Close()
  249. for _, p := range []int{1, 2, 3, 4} {
  250. b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
  251. benchmarkQueryContext(b, db, p)
  252. })
  253. }
  254. }
  255. func benchmarkExecContext(b *testing.B, db *sql.DB, p int) {
  256. ctx, cancel := context.WithCancel(context.Background())
  257. defer cancel()
  258. db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
  259. tb := (*TB)(b)
  260. stmt := tb.checkStmt(db.PrepareContext(ctx, "DO 1"))
  261. defer stmt.Close()
  262. b.SetParallelism(p)
  263. b.ReportAllocs()
  264. b.ResetTimer()
  265. b.RunParallel(func(pb *testing.PB) {
  266. for pb.Next() {
  267. if _, err := stmt.ExecContext(ctx); err != nil {
  268. b.Fatal(err)
  269. }
  270. }
  271. })
  272. }
  273. func BenchmarkExecContext(b *testing.B) {
  274. db := initDB(b,
  275. "DROP TABLE IF EXISTS foo",
  276. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  277. `INSERT INTO foo VALUES (1, "one")`,
  278. `INSERT INTO foo VALUES (2, "two")`,
  279. )
  280. defer db.Close()
  281. for _, p := range []int{1, 2, 3, 4} {
  282. b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
  283. benchmarkQueryContext(b, db, p)
  284. })
  285. }
  286. }
  287. // BenchmarkQueryRawBytes benchmarks fetching 100 blobs using sql.RawBytes.
  288. // "size=" means size of each blobs.
  289. func BenchmarkQueryRawBytes(b *testing.B) {
  290. var sizes []int = []int{100, 1000, 2000, 4000, 8000, 12000, 16000, 32000, 64000, 256000}
  291. db := initDB(b,
  292. "DROP TABLE IF EXISTS bench_rawbytes",
  293. "CREATE TABLE bench_rawbytes (id INT PRIMARY KEY, val LONGBLOB)",
  294. )
  295. defer db.Close()
  296. blob := make([]byte, sizes[len(sizes)-1])
  297. for i := range blob {
  298. blob[i] = 42
  299. }
  300. for i := 0; i < 100; i++ {
  301. _, err := db.Exec("INSERT INTO bench_rawbytes VALUES (?, ?)", i, blob)
  302. if err != nil {
  303. b.Fatal(err)
  304. }
  305. }
  306. for _, s := range sizes {
  307. b.Run(fmt.Sprintf("size=%v", s), func(b *testing.B) {
  308. db.SetMaxIdleConns(0)
  309. db.SetMaxIdleConns(1)
  310. b.ReportAllocs()
  311. b.ResetTimer()
  312. for j := 0; j < b.N; j++ {
  313. rows, err := db.Query("SELECT LEFT(val, ?) as v FROM bench_rawbytes", s)
  314. if err != nil {
  315. b.Fatal(err)
  316. }
  317. nrows := 0
  318. for rows.Next() {
  319. var buf sql.RawBytes
  320. err := rows.Scan(&buf)
  321. if err != nil {
  322. b.Fatal(err)
  323. }
  324. if len(buf) != s {
  325. b.Fatalf("size mismatch: expected %v, got %v", s, len(buf))
  326. }
  327. nrows++
  328. }
  329. rows.Close()
  330. if nrows != 100 {
  331. b.Fatalf("numbers of rows mismatch: expected %v, got %v", 100, nrows)
  332. }
  333. }
  334. })
  335. }
  336. }