summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/pkg/metrics/unique_counter_test.go
blob: b9439c9a3964c36350b5fd081066d0cc395e353e (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package metrics

import (
	"fmt"
	"strconv"
	"testing"

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

func TestHyperLogLogUniqueCounter_Value(t *testing.T) {
	for _, useHLL := range []bool{true, false} {
		t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
			c := NewUniqueCounter(useHLL)
			assert.Equal(t, 0, c.Value())

			c.Insert("foo")
			assert.Equal(t, 1, c.Value())

			c.Insert("foo")
			assert.Equal(t, 1, c.Value())

			c.Insert("bar")
			assert.Equal(t, 2, c.Value())

			c.Insert("baz")
			assert.Equal(t, 3, c.Value())

			c.Reset()
			assert.Equal(t, 0, c.Value())

			c.Insert("foo")
			assert.Equal(t, 1, c.Value())
		})
	}
}

func TestHyperLogLogUniqueCounter_WriteTo(t *testing.T) {
	for _, useHLL := range []bool{true, false} {
		t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
			c := NewUniqueCounterVec(useHLL)
			c.Get("a").Insert("foo")
			c.Get("a").Insert("bar")
			c.Get("b").Insert("foo")

			m := map[string]int64{}
			c.WriteTo(m, "pi", 100, 1)
			assert.Len(t, m, 2)
			assert.EqualValues(t, 200, m["pi_a"])
			assert.EqualValues(t, 100, m["pi_b"])
		})
	}
}

func TestUniqueCounterVec_Reset(t *testing.T) {
	for _, useHLL := range []bool{true, false} {
		t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
			c := NewUniqueCounterVec(useHLL)
			c.Get("a").Insert("foo")
			c.Get("a").Insert("bar")
			c.Get("b").Insert("foo")

			assert.Equal(t, 2, len(c.Items))
			assert.Equal(t, 2, c.Get("a").Value())
			assert.Equal(t, 1, c.Get("b").Value())

			c.Reset()
			assert.Equal(t, 2, len(c.Items))
			assert.Equal(t, 0, c.Get("a").Value())
			assert.Equal(t, 0, c.Get("b").Value())
		})
	}
}

func BenchmarkUniqueCounter_Insert(b *testing.B) {
	benchmarks := []struct {
		name        string
		same        bool
		hyperloglog bool
		nop         bool
	}{

		{"map-same", true, false, false},
		{"hll-same", true, true, false},

		{"nop", false, false, true},
		{"map-diff", false, false, false},
		{"hll-diff", false, true, false},
	}
	for _, bm := range benchmarks {
		b.Run(bm.name, func(b *testing.B) {
			c := NewUniqueCounter(bm.hyperloglog)
			if bm.same {
				for i := 0; i < b.N; i++ {
					c.Insert("foo")
				}
			} else if bm.nop {
				for i := 0; i < b.N; i++ {
					strconv.Itoa(i)
				}
			} else {
				for i := 0; i < b.N; i++ {
					c.Insert(strconv.Itoa(i))
				}
			}
		})
	}
}

func BenchmarkUniqueCounterVec_Insert(b *testing.B) {
	benchmarks := []struct {
		name        string
		same        bool
		hyperloglog bool
		nop         bool
	}{

		{"map-same", true, false, false},
		{"hll-same", true, true, false},

		{"nop", false, false, true},
		{"map-diff", false, false, false},
		{"hll-diff", false, true, false},
	}
	for _, bm := range benchmarks {
		b.Run(bm.name, func(b *testing.B) {
			c := NewUniqueCounterVec(bm.hyperloglog)
			if bm.same {
				for i := 0; i < b.N; i++ {
					c.Get("a").Insert("foo")
				}
			} else if bm.nop {
				for i := 0; i < b.N; i++ {
					strconv.Itoa(i)
				}
			} else {
				for i := 0; i < b.N; i++ {
					c.Get("a").Insert(strconv.Itoa(i))
				}
			}
		})
	}
}