Remove unnecessary read

* Add benchmark for SelectValueTo
pgx-vs-pq
Jack Christensen 2014-04-25 13:09:19 -06:00
parent 4829cd4ebc
commit 75ca2b3b1c
3 changed files with 45 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}
}