aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/compress/compress_test.go
blob: c3a7dfec7ad1019fe02c607113e9d84855021564 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package compress

import (
	"bytes"
	"io"
	"math/rand"
	"os"
	"strings"
	"testing"

	"github.com/go-faster/city"
	"github.com/stretchr/testify/require"

	"github.com/ClickHouse/ch-go/internal/gold"
)

func TestMain(m *testing.M) {
	// Explicitly registering flags for golden files.
	gold.Init()

	os.Exit(m.Run())
}

func TestCompress(t *testing.T) {
	data := []byte(strings.Repeat("Hello!\n", 25))
	gold.Bytes(t, data, "data_raw")

	for i := range MethodValues() {
		m := MethodValues()[i]
		t.Run(m.String(), func(t *testing.T) {
			w := NewWriter()
			require.NoError(t, w.Compress(m, data))

			gold.Bytes(t, w.Data, "data_compressed_"+strings.ToLower(m.String()))

			br := bytes.NewReader(nil)
			r := NewReader(br)
			out := make([]byte, len(data))

			for i := 0; i < 10; i++ {
				br.Reset(w.Data)
				_, err := io.ReadFull(r, out)
				require.NoError(t, err)
				require.Equal(t, data, out)
			}

			t.Run("NoShortRead", func(t *testing.T) {
				for i := 0; i < len(w.Data); i++ {
					b := w.Data[:i]
					r := NewReader(bytes.NewReader(b))
					_, err := io.ReadFull(r, out)
					require.Error(t, err)
				}
			})
			t.Run("CheckHash", func(t *testing.T) {
				// Corrupt bytes of data or checksum.
				var ref city.U128
				for i := 0; i < len(w.Data); i++ {
					b := append([]byte{}, w.Data...) // clone
					b[i]++
					r := NewReader(bytes.NewReader(b))
					_, err := io.ReadFull(r, out)
					require.Error(t, err)
					if i > headerSize {
						var badData *CorruptedDataErr
						require.ErrorAs(t, err, &badData)
						if ref.High == 0 && ref.Low == 0 {
							// Pick first reference.
							ref = badData.Reference
						}
						require.Equal(t, FormatU128(ref), FormatU128(badData.Reference))
						require.Equal(t, ref, badData.Reference)
					}
				}
			})
		})
	}
}

func BenchmarkWriter_Compress(b *testing.B) {
	// Highly compressible data.
	data := bytes.Repeat([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, 1800)

	for i := range MethodValues() {
		m := MethodValues()[i]
		b.Run(m.String(), func(b *testing.B) {
			b.ReportAllocs()
			b.SetBytes(int64(len(data)))

			w := NewWriter()

			// First round to warmup.
			if err := w.Compress(m, data); err != nil {
				b.Fatal(err)
			}

			for i := 0; i < b.N; i++ {
				if err := w.Compress(m, data); err != nil {
					b.Fatal(err)
				}
			}
		})
	}
}

func randData(n int) []byte {
	s := rand.NewSource(10)
	r := rand.New(s)
	buf := make([]byte, n)
	if _, err := io.ReadFull(r, buf); err != nil {
		panic(err)
	}
	return buf
}

func BenchmarkReader_Read(b *testing.B) {
	// Not compressible data.
	data := randData(1024 * 20)

	for i := range MethodValues() {
		m := MethodValues()[i]
		b.Run(m.String(), func(b *testing.B) {
			w := NewWriter()
			if err := w.Compress(m, data); err != nil {
				b.Fatal(err)
			}
			b.ReportAllocs()
			b.SetBytes(int64(len(w.Data)))

			out := make([]byte, len(data))

			br := bytes.NewReader(nil)
			r := NewReader(br)

			b.ResetTimer()
			for i := 0; i < b.N; i++ {
				br.Reset(w.Data)
				if _, err := io.ReadFull(r, out); err != nil {
					b.Fatal(err)
				}
			}
		})
	}
}