-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathsymbols_test.go
150 lines (139 loc) · 3.28 KB
/
symbols_test.go
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
145
146
147
148
149
150
package chunkenc
import (
"bytes"
"encoding/binary"
"fmt"
"testing"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/v3/pkg/compression"
)
func TestSymbolizer(t *testing.T) {
for _, tc := range []struct {
name string
labelsToAdd []labels.Labels
expectedSymbols []symbols
expectedNumLabels int
expectedCheckpointSize int
expectedUncompressedSize int
}{
{
name: "no labels",
expectedCheckpointSize: binary.MaxVarintLen32,
},
{
name: "no duplicate labels",
labelsToAdd: []labels.Labels{
labels.FromStrings("foo", "bar"),
labels.FromStrings(
"fizz", "buzz",
"ping", "pong",
),
},
expectedSymbols: []symbols{
{
symbol{
Name: 0,
Value: 1,
},
},
{
symbol{
Name: 2,
Value: 3,
},
symbol{
Name: 4,
Value: 5,
},
},
},
expectedNumLabels: 6,
expectedCheckpointSize: binary.MaxVarintLen32 + 6*binary.MaxVarintLen32 + 22,
expectedUncompressedSize: 22,
},
{
name: "with duplicate labels",
labelsToAdd: []labels.Labels{
labels.FromStrings(
"foo", "bar",
"bar", "foo",
),
labels.FromStrings(
"foo", "bar",
"fizz", "buzz",
"ping", "pong",
),
},
expectedSymbols: []symbols{
{
symbol{
Name: 0,
Value: 1,
},
symbol{
Name: 1,
Value: 0,
},
},
{
symbol{
Name: 2,
Value: 3,
},
symbol{
Name: 1,
Value: 0,
},
symbol{
Name: 4,
Value: 5,
},
},
},
expectedNumLabels: 6,
expectedCheckpointSize: binary.MaxVarintLen32 + 6*binary.MaxVarintLen32 + 22,
expectedUncompressedSize: 22,
},
} {
for _, encoding := range testEncodings {
t.Run(fmt.Sprintf("%s - %s", tc.name, encoding), func(t *testing.T) {
s := newSymbolizer()
for i, lbls := range tc.labelsToAdd {
symbols := s.Add(lbls)
require.Equal(t, tc.expectedSymbols[i], symbols)
require.Equal(t, lbls, s.Lookup(symbols, nil))
}
// Test that Lookup returns empty labels if no symbols are provided.
if len(tc.labelsToAdd) == 0 {
ret := s.Lookup([]symbol{
{
Name: 0,
Value: 0,
},
}, nil)
require.Equal(t, `{""=""}`, ret.String())
}
require.Equal(t, tc.expectedNumLabels, len(s.labels))
require.Equal(t, tc.expectedCheckpointSize, s.CheckpointSize())
require.Equal(t, tc.expectedUncompressedSize, s.UncompressedSize())
buf := bytes.NewBuffer(nil)
numBytesWritten, _, err := s.CheckpointTo(buf)
require.NoError(t, err)
require.LessOrEqual(t, numBytesWritten, tc.expectedCheckpointSize)
loaded := symbolizerFromCheckpoint(buf.Bytes())
for i, symbols := range tc.expectedSymbols {
require.Equal(t, tc.labelsToAdd[i], loaded.Lookup(symbols, nil))
}
buf.Reset()
_, _, err = s.SerializeTo(buf, compression.GetWriterPool(encoding))
require.NoError(t, err)
loaded, err = symbolizerFromEnc(buf.Bytes(), compression.GetReaderPool(encoding))
require.NoError(t, err)
for i, symbols := range tc.expectedSymbols {
require.Equal(t, tc.labelsToAdd[i], loaded.Lookup(symbols, nil))
}
})
}
}
}