MCPcopy
hub / github.com/grafana/tempo / TestTokenForIDCollision

Function TestTokenForIDCollision

pkg/model/trace/combine_test.go:126–164  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

124}
125
126func TestTokenForIDCollision(t *testing.T) {
127 // Estimate the hash collision rate of tokenForID.
128
129 n := 1_000_000
130 h := util.NewTokenHasher()
131 buf := make([]byte, 4)
132
133 tokens := map[token]struct{}{}
134 IDs := [][]byte{}
135
136 spanID := make([]byte, 8)
137 for i := 0; i < n; i++ {
138 _, err := crand.Read(spanID)
139 require.NoError(t, err)
140
141 cpy := append([]byte(nil), spanID...)
142 IDs = append(IDs, cpy)
143
144 tokens[token(util.TokenForID(h, buf, 0, spanID))] = struct{}{}
145 }
146
147 // Ensure no duplicate span IDs accidentally generated
148 sort.Slice(IDs, func(i, j int) bool {
149 return bytes.Compare(IDs[i], IDs[j]) == -1
150 })
151 for i := 1; i < len(IDs); i++ {
152 if bytes.Equal(IDs[i-1], IDs[i]) {
153 panic("same span ID was generated, oops")
154 }
155 }
156
157 missing := n - len(tokens)
158 if missing > 0 {
159 fmt.Printf("missing 1 out of every %.2f spans", float32(n)/float32(missing))
160 }
161
162 // There shouldn't be any collisions.
163 require.Equal(t, n, len(tokens))
164}
165
166func BenchmarkCombine(b *testing.B) {
167 parts := []int{2, 3, 4, 8}

Callers

nothing calls this directly

Calls 6

NewTokenHasherFunction · 0.92
TokenForIDFunction · 0.92
tokenTypeAlias · 0.70
ReadMethod · 0.65
CompareMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected