aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/jackc/pgx/v5/pgxpool/bench_test.go
blob: c2d58a3879865595df9cc6c35c5edbaf8aa9d10a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package pgxpool_test

import (
	"context"
	"os"
	"testing"

	"github.com/jackc/pgx/v5"
	"github.com/jackc/pgx/v5/pgxpool"
	"github.com/stretchr/testify/require"
)

func BenchmarkAcquireAndRelease(b *testing.B) {
	pool, err := pgxpool.New(context.Background(), os.Getenv("PGX_TEST_DATABASE"))
	require.NoError(b, err)
	defer pool.Close()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		c, err := pool.Acquire(context.Background())
		if err != nil {
			b.Fatal(err)
		}
		c.Release()
	}
}

func BenchmarkMinimalPreparedSelectBaseline(b *testing.B) {
	config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
	require.NoError(b, err)

	config.AfterConnect = func(ctx context.Context, c *pgx.Conn) error {
		_, err := c.Prepare(ctx, "ps1", "select $1::int8")
		return err
	}

	db, err := pgxpool.NewWithConfig(context.Background(), config)
	require.NoError(b, err)

	conn, err := db.Acquire(context.Background())
	require.NoError(b, err)
	defer conn.Release()

	var n int64

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err = conn.QueryRow(context.Background(), "ps1", i).Scan(&n)
		if err != nil {
			b.Fatal(err)
		}

		if n != int64(i) {
			b.Fatalf("expected %d, got %d", i, n)
		}
	}
}

func BenchmarkMinimalPreparedSelect(b *testing.B) {
	config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
	require.NoError(b, err)

	config.AfterConnect = func(ctx context.Context, c *pgx.Conn) error {
		_, err := c.Prepare(ctx, "ps1", "select $1::int8")
		return err
	}

	db, err := pgxpool.NewWithConfig(context.Background(), config)
	require.NoError(b, err)

	var n int64

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		err = db.QueryRow(context.Background(), "ps1", i).Scan(&n)
		if err != nil {
			b.Fatal(err)
		}

		if n != int64(i) {
			b.Fatalf("expected %d, got %d", i, n)
		}
	}
}