summaryrefslogtreecommitdiff
path: root/util/metrics/tagcounter_test.go
blob: ec6ab09058c153814a4ef9ebf3fd588788f9c5d9 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package metrics

import (
	"fmt"
	"strings"
	"sync"
	"testing"

	"github.com/algorand/go-algorand/test/partitiontest"
	"github.com/stretchr/testify/require"
)

func TestTagCounter(t *testing.T) {
	partitiontest.PartitionTest(t)

	tags := make([]string, 17)
	for i := range tags {
		tags[i] = fmt.Sprintf("A%c", 'A'+i)
	}
	//t.Logf("tags %v", tags)
	countsIn := make([]uint64, len(tags))
	for i := range countsIn {
		countsIn[i] = uint64(10 * (i + 1))
	}

	tc := NewTagCounter("tc", "wat")
	DefaultRegistry().Deregister(tc)

	// check that empty TagCounter cleanly returns no results
	var sb strings.Builder
	tc.WriteMetric(&sb, "")
	require.Equal(t, "", sb.String())

	result := make(map[string]float64)
	tc.AddMetric(result)
	require.Equal(t, 0, len(result))

	var wg sync.WaitGroup
	wg.Add(len(tags))

	runf := func(tag string, count uint64) {
		for i := 0; i < int(count); i++ {
			tc.Add(tag, 1)
		}
		wg.Done()
	}

	for i, tag := range tags {
		go runf(tag, countsIn[i])
	}
	wg.Wait()

	endtags := tc.tagptr.Load().(map[string]*uint64)
	for i, tag := range tags {
		countin := countsIn[i]
		endcountp := endtags[tag]
		if endcountp == nil {
			t.Errorf("tag[%d] %s nil counter", i, tag)
			continue
		}
		endcount := *endcountp
		if endcount != countin {
			t.Errorf("tag[%d] %v wanted %d got %d", i, tag, countin, endcount)
		}
	}
}

func TestTagCounterFilter(t *testing.T) {
	partitiontest.PartitionTest(t)

	tags := make([]string, 17)
	for i := range tags {
		tags[i] = fmt.Sprintf("A%c", 'A'+i)
	}
	goodTags := tags[:10]
	badTags := tags[10:]
	badCount := uint64(0)
	//t.Logf("tags %v", tags)
	countsIn := make([]uint64, len(tags))
	for i := range countsIn {
		countsIn[i] = uint64(10 * (i + 1))
		if i >= 10 {
			badCount += countsIn[i]
		}
	}

	tc := NewTagCounterFiltered("tc", "wat", goodTags, "UNK")
	DefaultRegistry().Deregister(tc)

	// check that empty TagCounter cleanly returns no results
	var sb strings.Builder
	tc.WriteMetric(&sb, "")
	require.Equal(t, "", sb.String())

	result := make(map[string]float64)
	tc.AddMetric(result)
	require.Equal(t, 0, len(result))

	var wg sync.WaitGroup
	wg.Add(len(tags))

	runf := func(tag string, count uint64) {
		for i := 0; i < int(count); i++ {
			tc.Add(tag, 1)
		}
		wg.Done()
	}

	for i, tag := range tags {
		go runf(tag, countsIn[i])
	}
	wg.Wait()

	endtags := tc.tagptr.Load().(map[string]*uint64)
	for i, tag := range goodTags {
		countin := countsIn[i]
		endcountp := endtags[tag]
		if endcountp == nil {
			t.Errorf("tag[%d] %s nil counter", i, tag)
			continue
		}
		endcount := *endcountp
		if endcount != countin {
			t.Errorf("tag[%d] %v wanted %d got %d", i, tag, countin, endcount)
		}
	}
	for _, tag := range badTags {
		endcountp := endtags[tag]
		if endcountp == nil {
			// best, nil entry, never touched the struct
			continue
		}
		endcount := *endcountp
		if endcount != 0 {
			t.Errorf("bad tag %v wanted %d got %d", tag, 0, endcount)
		}
	}
	endcountp := endtags["UNK"]
	endcount := uint64(0)
	if endcountp != nil {
		endcount = *endcountp
	}
	require.Equal(t, badCount, endcount)
}

func TestTagCounterWriteMetric(t *testing.T) {
	partitiontest.PartitionTest(t)

	tc := NewTagCounter("count_msgs_{TAG}", "number of {TAG} messages")
	DefaultRegistry().Deregister(tc)

	tc.Add("TX", 100)
	tc.Add("TX", 1)
	tc.Add("RX", 0)

	var sbOut strings.Builder
	tc.WriteMetric(&sbOut, `host="myhost"`)
	txExpected := `# HELP count_msgs_TX number of TX messages
# TYPE count_msgs_TX counter
count_msgs_TX{host="myhost"} 101
`
	rxExpected := `# HELP count_msgs_RX number of RX messages
# TYPE count_msgs_RX counter
count_msgs_RX{host="myhost"} 0
`
	expfmt := sbOut.String()
	require.True(t, expfmt == txExpected+rxExpected || expfmt == rxExpected+txExpected, "bad fmt: %s", expfmt)

	tc2 := NewTagCounter("declared", "number of {TAG}s", "A", "B")
	DefaultRegistry().Deregister(tc2)
	aExpected := "# HELP declared_A number of As\n# TYPE declared_A counter\ndeclared_A{host=\"h\"} 0\n"
	bExpected := "# HELP declared_B number of Bs\n# TYPE declared_B counter\ndeclared_B{host=\"h\"} 0\n"
	sbOut = strings.Builder{}
	tc2.WriteMetric(&sbOut, `host="h"`)
	expfmt = sbOut.String()
	require.True(t, expfmt == aExpected+bExpected || expfmt == bExpected+aExpected, "bad fmt: %s", expfmt)
}

func BenchmarkTagCounter(b *testing.B) {
	b.Logf("b.N = %d", b.N)
	t := b
	tags := make([]string, 17)
	for i := range tags {
		tags[i] = fmt.Sprintf("A%c", 'A'+i)
	}
	//t.Logf("tags %v", tags)
	triangle := make([]int, len(tags))
	tsum := 0
	for i := range triangle {
		triangle[i] = i + 1
		tsum += i + 1
	}
	wholeN := b.N / tsum
	remainder := b.N - (tsum * wholeN)
	rchunk := (remainder / len(tags)) + 1
	countsIn := make([]uint64, len(tags))
	csum := uint64(0)
	for i := range countsIn {
		rcc := rchunk
		if remainder < rcc {
			rcc = remainder
			remainder = 0
		} else {
			remainder -= rchunk
		}
		countsIn[i] = uint64((triangle[i] * wholeN) + rcc)
		csum += countsIn[i]
	}
	if csum != uint64(b.N) {
		b.Errorf("b.N = %d, but total = %d", b.N, csum)
	}

	tc := NewTagCounter("tc", "wat")
	//var wg sync.WaitGroup
	//wg.Add(len(tags))

	runf := func(tag string, count uint64) {
		for i := 0; i < int(count); i++ {
			tc.Add(tag, 1)
		}
		//wg.Done()
	}

	for i, tag := range tags {
		// don't run in threads so that we can benchmark time
		runf(tag, countsIn[i])
	}
	//wg.Wait()

	endtags := tc.tagptr.Load().(map[string]*uint64)
	for i, tag := range tags {
		countin := countsIn[i]
		endcount := uint64(0)
		endcountp := endtags[tag]
		if endcountp != nil {
			endcount = *endcountp
			//t.Errorf("tag[%d] %s nil counter", i, tag)
			//continue
		}
		//endcount := *endcountp
		if endcount != countin {
			t.Errorf("tag[%d] %v wanted %d got %d", i, tag, countin, endcount)
		}
	}
}