- 
      
- 
        Save khoahuynhdev/a1d6d17b7438bb5b47800db43bd4a091 to your computer and use it in GitHub Desktop. 
    MaxOpenConns benchmark
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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