Skip to content

Instantly share code, notes, and snippets.

@rueian
Last active March 17, 2020 16:31
Show Gist options
  • Save rueian/e4a7e002a142fcbda366cf054d943ae9 to your computer and use it in GitHub Desktop.
Save rueian/e4a7e002a142fcbda366cf054d943ae9 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/rand"
"database/sql"
_ "github.com/lib/pq"
"testing"
)
type test struct {
name string
url string
}
func Benchmark(b *testing.B) {
pgURL := "postgres://postgres@localhost:5432/postgres?sslmode=disable"
query := "INSERT INTO kv (key, value) VALUES ($1::text, $2::bytea) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value RETURNING *;"
k := "foo"
v := make([]byte, 10000)
rand.Read(v)
for _, c := range []test{
{
name: "Text Representation",
url: pgURL + "&disable_prepared_binary_result=yes",
},
{
name: "Binary Representation",
url: pgURL + "&binary_parameters=yes",
},
} {
b.Run(c.name, func(b *testing.B) {
db, _ := sql.Open("postgres", c.url)
defer db.Close()
stmt, _ := db.Prepare(query)
defer stmt.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := stmt.Exec(k, v); err != nil {
panic(err)
}
}
b.StopTimer()
})
}
}
// Result:
//
// goos: darwin
// goarch: amd64
// pkg: github.com/rueian/pg-extended-query-lab/cmd/std
// Benchmark
// Benchmark/Text_Representation
// Benchmark/Text_Representation-12 277 3669195 ns/op
// Benchmark/Binary_Representation
// Benchmark/Binary_Representation-12 554 2212138 ns/op
// PASS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment