Skip to content

Instantly share code, notes, and snippets.

@khoahuynhdev
Forked from alexedwards/main_test.go
Created February 10, 2022 09:55
Show Gist options
  • Save khoahuynhdev/a1d6d17b7438bb5b47800db43bd4a091 to your computer and use it in GitHub Desktop.
Save khoahuynhdev/a1d6d17b7438bb5b47800db43bd4a091 to your computer and use it in GitHub Desktop.
MaxOpenConns benchmark
package main
import (
"database/sql"
"testing"
_ "github.com/lib/pq"
)
func insertRecord(b *testing.B, db *sql.DB) {
_, err := db.Exec("INSERT INTO isbns VALUES ('978-3-598-21500-1')")
if err != nil {
b.Fatal(err)
}
}
func BenchmarkMaxOpenConns1(b *testing.B) {
db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
if err != nil {
b.Fatal(err)
}
db.SetMaxOpenConns(1)
defer db.Close()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
insertRecord(b, db)
}
})
}
func BenchmarkMaxOpenConns5(b *testing.B) {
db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
if err != nil {
b.Fatal(err)
}
db.SetMaxOpenConns(5)
defer db.Close()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
insertRecord(b, db)
}
})
}
func BenchmarkMaxOpenConnsUnlimited(b *testing.B) {
db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
if err != nil {
b.Fatal(err)
}
defer db.Close()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
insertRecord(b, db)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment