From 75ca2b3b1ce8606e42cef63bb0f36f006e1eaf77 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 25 Apr 2014 13:09:19 -0600 Subject: [PATCH] Remove unnecessary read * Add benchmark for SelectValueTo --- bench_test.go | 34 ++++++++++++++++++++++++++++++++++ connection.go | 13 ++++--------- helper_test.go | 7 +++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/bench_test.go b/bench_test.go index 01042886..98c4238c 100644 --- a/bench_test.go +++ b/bench_test.go @@ -2,6 +2,7 @@ package pgx_test import ( "github.com/JackC/pgx" + "io/ioutil" "math/rand" "testing" ) @@ -41,6 +42,7 @@ func createNarrowTestData(b *testing.B, conn *pgx.Connection) { mustPrepare(b, conn, "getNarrowById", "select * from narrow where id=$1") mustPrepare(b, conn, "getMultipleNarrowById", "select * from narrow where id between $1 and $2") + mustPrepare(b, conn, "getMultipleNarrowByIdAsJSON", "select json_agg(row_to_json(narrow)) from narrow where id between $1 and $2") narrowTestDataLoaded = true } @@ -124,6 +126,38 @@ func BenchmarkSelectRowsPreparedNarrow(b *testing.B) { } } +func BenchmarkSelectValuePreparedNarrow(b *testing.B) { + conn := getSharedConnection(b) + createNarrowTestData(b, conn) + + // Get random ids outside of timing + ids := make([]int32, b.N) + for i := 0; i < b.N; i++ { + ids[i] = 1 + rand.Int31n(9999) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + mustSelectValue(b, conn, "getMultipleNarrowByIdAsJSON", ids[i], ids[i]+10) + } +} + +func BenchmarkSelectValueToPreparedNarrow(b *testing.B) { + conn := getSharedConnection(b) + createNarrowTestData(b, conn) + + // Get random ids outside of timing + ids := make([]int32, b.N) + for i := 0; i < b.N; i++ { + ids[i] = 1 + rand.Int31n(9999) + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + mustSelectValueTo(b, conn, ioutil.Discard, "getMultipleNarrowByIdAsJSON", ids[i], ids[i]+10) + } +} + func createJoinsTestData(b *testing.B, conn *pgx.Connection) { if testJoinsDataLoaded { return diff --git a/connection.go b/connection.go index b43c2b1b..12965bce 100644 --- a/connection.go +++ b/connection.go @@ -13,6 +13,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net" "os/user" "time" @@ -393,14 +394,8 @@ func (c *Connection) SelectValueTo(w io.Writer, sql string, arguments ...interfa } func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { - buf := c.getBuf() - if _, err = io.CopyN(buf, c.reader, 6); err != nil { - c.die(err) - return - } - var columnCount int16 - err = binary.Read(buf, binary.BigEndian, &columnCount) + err = binary.Read(c.reader, binary.BigEndian, &columnCount) if err != nil { c.die(err) return @@ -408,7 +403,7 @@ func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { if columnCount != 1 { // Read the rest of the data row so it can be discarded - if _, err = io.CopyN(buf, c.reader, int64(bodySize-6)); err != nil { + if _, err = io.CopyN(ioutil.Discard, c.reader, int64(bodySize-2)); err != nil { c.die(err) return } @@ -417,7 +412,7 @@ func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) { } var valueSize int32 - err = binary.Read(buf, binary.BigEndian, &valueSize) + err = binary.Read(c.reader, binary.BigEndian, &valueSize) if err != nil { c.die(err) return diff --git a/helper_test.go b/helper_test.go index 527164ff..aa8c12a6 100644 --- a/helper_test.go +++ b/helper_test.go @@ -2,6 +2,7 @@ package pgx_test import ( "github.com/JackC/pgx" + "io" "testing" ) @@ -56,3 +57,9 @@ func mustSelectValue(t testing.TB, conn *pgx.Connection, sql string, arguments . } return } + +func mustSelectValueTo(t testing.TB, conn *pgx.Connection, w io.Writer, sql string, arguments ...interface{}) { + if err := conn.SelectValueTo(w, sql, arguments...); err != nil { + t.Fatalf("SelectValueTo unexpectedly failed with %v: %v", sql, err) + } +}