benchmark_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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.Fatal(err.Error())
  112. }
  113. }
  114. }()
  115. }
  116. }
  117. // data, but no db writes
  118. var roundtripSample []byte
  119. func initRoundtripBenchmarks() ([]byte, int, int) {
  120. if roundtripSample == nil {
  121. roundtripSample = []byte(strings.Repeat("0123456789abcdef", 1024*1024))
  122. }
  123. return roundtripSample, 16, len(roundtripSample)
  124. }
  125. func BenchmarkRoundtripTxt(b *testing.B) {
  126. b.StopTimer()
  127. sample, min, max := initRoundtripBenchmarks()
  128. sampleString := string(sample)
  129. b.ReportAllocs()
  130. tb := (*TB)(b)
  131. db := tb.checkDB(sql.Open("mysql", dsn))
  132. defer db.Close()
  133. b.StartTimer()
  134. var result string
  135. for i := 0; i < b.N; i++ {
  136. length := min + i
  137. if length > max {
  138. length = max
  139. }
  140. test := sampleString[0:length]
  141. rows := tb.checkRows(db.Query(`SELECT "` + test + `"`))
  142. if !rows.Next() {
  143. rows.Close()
  144. b.Fatalf("crashed")
  145. }
  146. err := rows.Scan(&result)
  147. if err != nil {
  148. rows.Close()
  149. b.Fatalf("crashed")
  150. }
  151. if result != test {
  152. rows.Close()
  153. b.Errorf("mismatch")
  154. }
  155. rows.Close()
  156. }
  157. }
  158. func BenchmarkRoundtripBin(b *testing.B) {
  159. b.StopTimer()
  160. sample, min, max := initRoundtripBenchmarks()
  161. b.ReportAllocs()
  162. tb := (*TB)(b)
  163. db := tb.checkDB(sql.Open("mysql", dsn))
  164. defer db.Close()
  165. stmt := tb.checkStmt(db.Prepare("SELECT ?"))
  166. defer stmt.Close()
  167. b.StartTimer()
  168. var result sql.RawBytes
  169. for i := 0; i < b.N; i++ {
  170. length := min + i
  171. if length > max {
  172. length = max
  173. }
  174. test := sample[0:length]
  175. rows := tb.checkRows(stmt.Query(test))
  176. if !rows.Next() {
  177. rows.Close()
  178. b.Fatalf("crashed")
  179. }
  180. err := rows.Scan(&result)
  181. if err != nil {
  182. rows.Close()
  183. b.Fatalf("crashed")
  184. }
  185. if !bytes.Equal(result, test) {
  186. rows.Close()
  187. b.Errorf("mismatch")
  188. }
  189. rows.Close()
  190. }
  191. }
  192. func BenchmarkInterpolation(b *testing.B) {
  193. mc := &mysqlConn{
  194. cfg: &Config{
  195. InterpolateParams: true,
  196. Loc: time.UTC,
  197. },
  198. maxAllowedPacket: maxPacketSize,
  199. maxWriteSize: maxPacketSize - 1,
  200. buf: newBuffer(nil),
  201. }
  202. args := []driver.Value{
  203. int64(42424242),
  204. float64(math.Pi),
  205. false,
  206. time.Unix(1423411542, 807015000),
  207. []byte("bytes containing special chars ' \" \a \x00"),
  208. "string containing special chars ' \" \a \x00",
  209. }
  210. q := "SELECT ?, ?, ?, ?, ?, ?"
  211. b.ReportAllocs()
  212. b.ResetTimer()
  213. for i := 0; i < b.N; i++ {
  214. _, err := mc.interpolateParams(q, args)
  215. if err != nil {
  216. b.Fatal(err)
  217. }
  218. }
  219. }
  220. func benchmarkQueryContext(b *testing.B, db *sql.DB, p int) {
  221. ctx, cancel := context.WithCancel(context.Background())
  222. defer cancel()
  223. db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
  224. tb := (*TB)(b)
  225. stmt := tb.checkStmt(db.PrepareContext(ctx, "SELECT val FROM foo WHERE id=?"))
  226. defer stmt.Close()
  227. b.SetParallelism(p)
  228. b.ReportAllocs()
  229. b.ResetTimer()
  230. b.RunParallel(func(pb *testing.PB) {
  231. var got string
  232. for pb.Next() {
  233. tb.check(stmt.QueryRow(1).Scan(&got))
  234. if got != "one" {
  235. b.Fatalf("query = %q; want one", got)
  236. }
  237. }
  238. })
  239. }
  240. func BenchmarkQueryContext(b *testing.B) {
  241. db := initDB(b,
  242. "DROP TABLE IF EXISTS foo",
  243. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  244. `INSERT INTO foo VALUES (1, "one")`,
  245. `INSERT INTO foo VALUES (2, "two")`,
  246. )
  247. defer db.Close()
  248. for _, p := range []int{1, 2, 3, 4} {
  249. b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
  250. benchmarkQueryContext(b, db, p)
  251. })
  252. }
  253. }
  254. func benchmarkExecContext(b *testing.B, db *sql.DB, p int) {
  255. ctx, cancel := context.WithCancel(context.Background())
  256. defer cancel()
  257. db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
  258. tb := (*TB)(b)
  259. stmt := tb.checkStmt(db.PrepareContext(ctx, "DO 1"))
  260. defer stmt.Close()
  261. b.SetParallelism(p)
  262. b.ReportAllocs()
  263. b.ResetTimer()
  264. b.RunParallel(func(pb *testing.PB) {
  265. for pb.Next() {
  266. if _, err := stmt.ExecContext(ctx); err != nil {
  267. b.Fatal(err)
  268. }
  269. }
  270. })
  271. }
  272. func BenchmarkExecContext(b *testing.B) {
  273. db := initDB(b,
  274. "DROP TABLE IF EXISTS foo",
  275. "CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
  276. `INSERT INTO foo VALUES (1, "one")`,
  277. `INSERT INTO foo VALUES (2, "two")`,
  278. )
  279. defer db.Close()
  280. for _, p := range []int{1, 2, 3, 4} {
  281. b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
  282. benchmarkQueryContext(b, db, p)
  283. })
  284. }
  285. }
  286. // BenchmarkQueryRawBytes benchmarks fetching 100 blobs using sql.RawBytes.
  287. // "size=" means size of each blobs.
  288. func BenchmarkQueryRawBytes(b *testing.B) {
  289. var sizes []int = []int{100, 1000, 2000, 4000, 8000, 12000, 16000, 32000, 64000, 256000}
  290. db := initDB(b,
  291. "DROP TABLE IF EXISTS bench_rawbytes",
  292. "CREATE TABLE bench_rawbytes (id INT PRIMARY KEY, val LONGBLOB)",
  293. )
  294. defer db.Close()
  295. blob := make([]byte, sizes[len(sizes)-1])
  296. for i := range blob {
  297. blob[i] = 42
  298. }
  299. for i := 0; i < 100; i++ {
  300. _, err := db.Exec("INSERT INTO bench_rawbytes VALUES (?, ?)", i, blob)
  301. if err != nil {
  302. b.Fatal(err)
  303. }
  304. }
  305. for _, s := range sizes {
  306. b.Run(fmt.Sprintf("size=%v", s), func(b *testing.B) {
  307. db.SetMaxIdleConns(0)
  308. db.SetMaxIdleConns(1)
  309. b.ReportAllocs()
  310. b.ResetTimer()
  311. for j := 0; j < b.N; j++ {
  312. rows, err := db.Query("SELECT LEFT(val, ?) as v FROM bench_rawbytes", s)
  313. if err != nil {
  314. b.Fatal(err)
  315. }
  316. nrows := 0
  317. for rows.Next() {
  318. var buf sql.RawBytes
  319. err := rows.Scan(&buf)
  320. if err != nil {
  321. b.Fatal(err)
  322. }
  323. if len(buf) != s {
  324. b.Fatalf("size mismatch: expected %v, got %v", s, len(buf))
  325. }
  326. nrows++
  327. }
  328. rows.Close()
  329. if nrows != 100 {
  330. b.Fatalf("numbers of rows mismatch: expected %v, got %v", 100, nrows)
  331. }
  332. }
  333. })
  334. }
  335. }