aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/col_raw_test.go
blob: c276d4dd9b9de175cc607c5932b341447cd8f67d (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
package proto

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestColRaw(t *testing.T) {
	v := ColRaw{
		T:     ColumnType("Foo(5)"),
		Size:  5,
		Data:  []byte{1, 2, 3, 4, 5},
		Count: 1,
	}

	r := require.New(t)
	r.Equal(v.T, v.Type())
	r.Equal(1, v.Rows())

	b := new(Buffer)
	v.EncodeColumn(b)

	dec := ColRaw{T: v.T, Size: v.Size}
	r.NoError(dec.DecodeColumn(b.Reader(), 1))
	r.Equal(v, dec)

	dec.Reset()
	r.Equal(0, dec.Rows())
	r.Len(dec.Data, 0)
}

func BenchmarkColRaw_EncodeColumn(b *testing.B) {
	buf := new(Buffer)
	v := ColRaw{
		Data: make([]byte, 1024),
	}

	b.ReportAllocs()
	b.SetBytes(1024)

	for i := 0; i < b.N; i++ {
		buf.Reset()
		v.EncodeColumn(buf)
	}
}

func BenchmarkColRaw_DecodeColumn(b *testing.B) {
	const (
		rows = 1_000
		size = 64
		data = size * rows
	)

	raw := make([]byte, data)
	br := bytes.NewReader(raw)
	r := NewReader(br)

	b.ReportAllocs()
	b.SetBytes(data)

	dec := ColRaw{
		T:    ColumnTypeUInt64,
		Size: size,
	}
	for i := 0; i < b.N; i++ {
		br.Reset(raw)
		r.raw.Reset(br)
		dec.Reset()

		if err := dec.DecodeColumn(r, rows); err != nil {
			b.Fatal(err)
		}
	}
}