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.

Revisions

  1. @alexedwards alexedwards revised this gist Dec 4, 2019. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion main_test.go
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,10 @@ import (
    )

    func insertRecord(b *testing.B, db *sql.DB) {
    _, err := db.Exec("INSERT INTO isbns VALUES ('978-3-598-21500-1')")
    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    defer cancel()

    _, err := db.ExecContext(ctx, "INSERT INTO isbns VALUES ('978-3-598-21500-1')")
    if err != nil {
    b.Fatal(err)
    }
  2. @alexedwards alexedwards revised this gist Feb 2, 2018. 1 changed file with 0 additions and 31 deletions.
    31 changes: 0 additions & 31 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -238,34 +238,3 @@ func BenchmarkConnMaxLifetime100(b *testing.B) {
    }
    })
    }

    func BenchmarkTotalConnsLimited(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxOpenConns(3)
    db.SetMaxIdleConns(1)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkTotalConnsUnlimited(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(100)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }
  3. @alexedwards alexedwards revised this gist Feb 2, 2018. 1 changed file with 212 additions and 0 deletions.
    212 changes: 212 additions & 0 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@ package main
    import (
    "database/sql"
    "testing"
    "time"

    _ "github.com/lib/pq"
    )
    @@ -29,6 +30,21 @@ func BenchmarkMaxOpenConns1(b *testing.B) {
    })
    }

    func BenchmarkMaxOpenConns2(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxOpenConns(2)
    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 {
    @@ -44,6 +60,21 @@ func BenchmarkMaxOpenConns5(b *testing.B) {
    })
    }

    func BenchmarkMaxOpenConns10(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxOpenConns(10)
    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 {
    @@ -57,3 +88,184 @@ func BenchmarkMaxOpenConnsUnlimited(b *testing.B) {
    }
    })
    }

    func BenchmarkMaxIdleConnsNone(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(0)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkMaxIdleConns1(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(1)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkMaxIdleConns2(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(2)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkMaxIdleConns5(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(5)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkMaxIdleConns10(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(10)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkConnMaxLifetimeUnlimited(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetConnMaxLifetime(0)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkConnMaxLifetime1000(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetConnMaxLifetime(1000 * time.Millisecond)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkConnMaxLifetime500(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetConnMaxLifetime(500 * time.Millisecond)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkConnMaxLifetime200(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetConnMaxLifetime(200 * time.Millisecond)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkConnMaxLifetime100(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetConnMaxLifetime(100 * time.Millisecond)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkTotalConnsLimited(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxOpenConns(3)
    db.SetMaxIdleConns(1)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }

    func BenchmarkTotalConnsUnlimited(b *testing.B) {
    db, err := sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
    b.Fatal(err)
    }
    db.SetMaxIdleConns(100)
    defer db.Close()

    b.RunParallel(func(pb *testing.PB) {
    for pb.Next() {
    insertRecord(b, db)
    }
    })
    }
  4. @alexedwards alexedwards created this gist Feb 2, 2018.
    59 changes: 59 additions & 0 deletions main_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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)
    }
    })
    }