aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/jackc/pgx/v5/pgconn/benchmark_private_test.go
blob: 9ea036ec796092e9b0d02a441504542543acf1d0 (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
package pgconn

import (
	"strings"
	"testing"
)

func BenchmarkCommandTagRowsAffected(b *testing.B) {
	benchmarks := []struct {
		commandTag   string
		rowsAffected int64
	}{
		{"UPDATE 1", 1},
		{"UPDATE 123456789", 123456789},
		{"INSERT 0 1", 1},
		{"INSERT 0 123456789", 123456789},
	}

	for _, bm := range benchmarks {
		ct := CommandTag{s: bm.commandTag}
		b.Run(bm.commandTag, func(b *testing.B) {
			var n int64
			for i := 0; i < b.N; i++ {
				n = ct.RowsAffected()
			}
			if n != bm.rowsAffected {
				b.Errorf("expected %d got %d", bm.rowsAffected, n)
			}
		})
	}
}

func BenchmarkCommandTagTypeFromString(b *testing.B) {
	ct := CommandTag{s: "UPDATE 1"}

	var update bool
	for i := 0; i < b.N; i++ {
		update = strings.HasPrefix(ct.String(), "UPDATE")
	}
	if !update {
		b.Error("expected update")
	}
}

func BenchmarkCommandTagInsert(b *testing.B) {
	benchmarks := []struct {
		commandTag string
		is         bool
	}{
		{"INSERT 1", true},
		{"INSERT 1234567890", true},
		{"UPDATE 1", false},
		{"UPDATE 1234567890", false},
		{"DELETE 1", false},
		{"DELETE 1234567890", false},
		{"SELECT 1", false},
		{"SELECT 1234567890", false},
		{"UNKNOWN 1234567890", false},
	}

	for _, bm := range benchmarks {
		ct := CommandTag{s: bm.commandTag}
		b.Run(bm.commandTag, func(b *testing.B) {
			var is bool
			for i := 0; i < b.N; i++ {
				is = ct.Insert()
			}
			if is != bm.is {
				b.Errorf("expected %v got %v", bm.is, is)
			}
		})
	}
}