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