mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-10 07:37:36 +08:00
94 lines
2.2 KiB
Go
94 lines
2.2 KiB
Go
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
|
//
|
|
// Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
// +build go1.8
|
|
|
|
package mysql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func benchmarkQueryContext(b *testing.B, db *sql.DB, p int) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
|
|
|
|
tb := (*TB)(b)
|
|
stmt := tb.checkStmt(db.PrepareContext(ctx, "SELECT val FROM foo WHERE id=?"))
|
|
defer stmt.Close()
|
|
|
|
b.SetParallelism(p)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
var got string
|
|
for pb.Next() {
|
|
tb.check(stmt.QueryRow(1).Scan(&got))
|
|
if got != "one" {
|
|
b.Fatalf("query = %q; want one", got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkQueryContext(b *testing.B) {
|
|
db := initDB(b,
|
|
"DROP TABLE IF EXISTS foo",
|
|
"CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
|
|
`INSERT INTO foo VALUES (1, "one")`,
|
|
`INSERT INTO foo VALUES (2, "two")`,
|
|
)
|
|
defer db.Close()
|
|
for _, p := range []int{1, 2, 3, 4} {
|
|
b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
|
|
benchmarkQueryContext(b, db, p)
|
|
})
|
|
}
|
|
}
|
|
|
|
func benchmarkExecContext(b *testing.B, db *sql.DB, p int) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
db.SetMaxIdleConns(p * runtime.GOMAXPROCS(0))
|
|
|
|
tb := (*TB)(b)
|
|
stmt := tb.checkStmt(db.PrepareContext(ctx, "DO 1"))
|
|
defer stmt.Close()
|
|
|
|
b.SetParallelism(p)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
if _, err := stmt.ExecContext(ctx); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func BenchmarkExecContext(b *testing.B) {
|
|
db := initDB(b,
|
|
"DROP TABLE IF EXISTS foo",
|
|
"CREATE TABLE foo (id INT PRIMARY KEY, val CHAR(50))",
|
|
`INSERT INTO foo VALUES (1, "one")`,
|
|
`INSERT INTO foo VALUES (2, "two")`,
|
|
)
|
|
defer db.Close()
|
|
for _, p := range []int{1, 2, 3, 4} {
|
|
b.Run(fmt.Sprintf("%d", p), func(b *testing.B) {
|
|
benchmarkQueryContext(b, db, p)
|
|
})
|
|
}
|
|
}
|