From 96680d6489095577d9c9988bc81607df8d435e5b Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Tue, 1 Jan 2019 14:10:24 -0600 Subject: [PATCH] Add benchmark to pgconn --- pgconn/benchmark_test.go | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 pgconn/benchmark_test.go diff --git a/pgconn/benchmark_test.go b/pgconn/benchmark_test.go new file mode 100644 index 00000000..da5bd4fc --- /dev/null +++ b/pgconn/benchmark_test.go @@ -0,0 +1,52 @@ +package pgconn_test + +import ( + "context" + "os" + "testing" + + "github.com/jackc/pgx/pgconn" + "github.com/stretchr/testify/require" +) + +func BenchmarkConnect(b *testing.B) { + benchmarks := []struct { + name string + env string + }{ + {"Unix socket", "PGX_TEST_UNIX_SOCKET_CONN_STRING"}, + {"TCP", "PGX_TEST_TCP_CONN_STRING"}, + } + + for _, bm := range benchmarks { + b.Run(bm.name, func(b *testing.B) { + connString := os.Getenv(bm.env) + if connString == "" { + b.Skipf("Skipping due to missing environment variable %v", bm.env) + } + + for i := 0; i < b.N; i++ { + conn, err := pgconn.Connect(context.Background(), connString) + require.Nil(b, err) + + err = conn.Close(context.Background()) + require.Nil(b, err) + } + }) + } +} + +func BenchmarkExecPrepared(b *testing.B) { + conn, err := pgconn.Connect(context.Background(), os.Getenv("PGX_TEST_DATABASE")) + require.Nil(b, err) + defer closeConn(b, conn) + + err = conn.Prepare(context.Background(), "ps1", "select 'hello'::text as a, 42::int4 as b, '2019-01-01'::date", nil) + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + _, err := conn.ExecPrepared(context.Background(), "ps1", nil, nil, nil) + require.Nil(b, err) + } +}