master
go 145 lines 3.05 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package oldmetrix
4
5 import (
6 "fmt"
7 "strconv"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestHyperLogLogUniqueCounter_Value(t *testing.T) {
14 for _, useHLL := range []bool{true, false} {
15 t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
16 c := NewUniqueCounter(useHLL)
17 assert.Equal(t, 0, c.Value())
18
19 c.Insert("foo")
20 assert.Equal(t, 1, c.Value())
21
22 c.Insert("foo")
23 assert.Equal(t, 1, c.Value())
24
25 c.Insert("bar")
26 assert.Equal(t, 2, c.Value())
27
28 c.Insert("baz")
29 assert.Equal(t, 3, c.Value())
30
31 c.Reset()
32 assert.Equal(t, 0, c.Value())
33
34 c.Insert("foo")
35 assert.Equal(t, 1, c.Value())
36 })
37 }
38 }
39
40 func TestHyperLogLogUniqueCounter_WriteTo(t *testing.T) {
41 for _, useHLL := range []bool{true, false} {
42 t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
43 c := NewUniqueCounterVec(useHLL)
44 c.Get("a").Insert("foo")
45 c.Get("a").Insert("bar")
46 c.Get("b").Insert("foo")
47
48 m := map[string]int64{}
49 c.WriteTo(m, "pi", 100, 1)
50 assert.Len(t, m, 2)
51 assert.EqualValues(t, 200, m["pi_a"])
52 assert.EqualValues(t, 100, m["pi_b"])
53 })
54 }
55 }
56
57 func TestUniqueCounterVec_Reset(t *testing.T) {
58 for _, useHLL := range []bool{true, false} {
59 t.Run(fmt.Sprintf("HLL=%v", useHLL), func(t *testing.T) {
60 c := NewUniqueCounterVec(useHLL)
61 c.Get("a").Insert("foo")
62 c.Get("a").Insert("bar")
63 c.Get("b").Insert("foo")
64
65 assert.Equal(t, 2, len(c.Items))
66 assert.Equal(t, 2, c.Get("a").Value())
67 assert.Equal(t, 1, c.Get("b").Value())
68
69 c.Reset()
70 assert.Equal(t, 2, len(c.Items))
71 assert.Equal(t, 0, c.Get("a").Value())
72 assert.Equal(t, 0, c.Get("b").Value())
73 })
74 }
75 }
76
77 func BenchmarkUniqueCounter_Insert(b *testing.B) {
78 benchmarks := []struct {
79 name string
80 same bool
81 hyperloglog bool
82 nop bool
83 }{
84
85 {"map-same", true, false, false},
86 {"hll-same", true, true, false},
87
88 {"nop", false, false, true},
89 {"map-diff", false, false, false},
90 {"hll-diff", false, true, false},
91 }
92 for _, bm := range benchmarks {
93 b.Run(bm.name, func(b *testing.B) {
94 c := NewUniqueCounter(bm.hyperloglog)
95 if bm.same {
96 for i := 0; i < b.N; i++ {
97 c.Insert("foo")
98 }
99 } else if bm.nop {
100 for i := 0; i < b.N; i++ {
101 strconv.Itoa(i)
102 }
103 } else {
104 for i := 0; i < b.N; i++ {
105 c.Insert(strconv.Itoa(i))
106 }
107 }
108 })
109 }
110 }
111
112 func BenchmarkUniqueCounterVec_Insert(b *testing.B) {
113 benchmarks := []struct {
114 name string
115 same bool
116 hyperloglog bool
117 nop bool
118 }{
119
120 {"map-same", true, false, false},
121 {"hll-same", true, true, false},
122
123 {"nop", false, false, true},
124 {"map-diff", false, false, false},
125 {"hll-diff", false, true, false},
126 }
127 for _, bm := range benchmarks {
128 b.Run(bm.name, func(b *testing.B) {
129 c := NewUniqueCounterVec(bm.hyperloglog)
130 if bm.same {
131 for i := 0; i < b.N; i++ {
132 c.Get("a").Insert("foo")
133 }
134 } else if bm.nop {
135 for i := 0; i < b.N; i++ {
136 strconv.Itoa(i)
137 }
138 } else {
139 for i := 0; i < b.N; i++ {
140 c.Get("a").Insert(strconv.Itoa(i))
141 }
142 }
143 })
144 }
145 }