mirror of https://github.com/jackc/pgx.git
parent
4829cd4ebc
commit
75ca2b3b1c
|
@ -2,6 +2,7 @@ package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/JackC/pgx"
|
"github.com/JackC/pgx"
|
||||||
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"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, "getNarrowById", "select * from narrow where id=$1")
|
||||||
mustPrepare(b, conn, "getMultipleNarrowById", "select * from narrow where id between $1 and $2")
|
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
|
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) {
|
func createJoinsTestData(b *testing.B, conn *pgx.Connection) {
|
||||||
if testJoinsDataLoaded {
|
if testJoinsDataLoaded {
|
||||||
return
|
return
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os/user"
|
"os/user"
|
||||||
"time"
|
"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) {
|
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
|
var columnCount int16
|
||||||
err = binary.Read(buf, binary.BigEndian, &columnCount)
|
err = binary.Read(c.reader, binary.BigEndian, &columnCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.die(err)
|
c.die(err)
|
||||||
return
|
return
|
||||||
|
@ -408,7 +403,7 @@ func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) {
|
||||||
|
|
||||||
if columnCount != 1 {
|
if columnCount != 1 {
|
||||||
// Read the rest of the data row so it can be discarded
|
// 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)
|
c.die(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -417,7 +412,7 @@ func (c *Connection) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var valueSize int32
|
var valueSize int32
|
||||||
err = binary.Read(buf, binary.BigEndian, &valueSize)
|
err = binary.Read(c.reader, binary.BigEndian, &valueSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.die(err)
|
c.die(err)
|
||||||
return
|
return
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pgx_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/JackC/pgx"
|
"github.com/JackC/pgx"
|
||||||
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -56,3 +57,9 @@ func mustSelectValue(t testing.TB, conn *pgx.Connection, sql string, arguments .
|
||||||
}
|
}
|
||||||
return
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue