aboutsummaryrefslogtreecommitdiff
path: root/pkg/metricsx/geohash_test.go
blob: 03cd12eb88c9ee65a9740170bbe30a2a9749a369 (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
package metricsx

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"io"
	"strings"
	"testing"

	"github.com/VictoriaMetrics/metrics"
)

func TestGeoCounter(t *testing.T) {
	var exp string
	if d, err := gzip.NewReader(base64.NewDecoder(base64.StdEncoding, strings.NewReader(``+
		`H4sIAAAAAAAA/4zZSW7lVACG0TmriN6YQZzndlCLwb1s2bjvUPaOEPPKmV9dquDg/8vNVq3bP2XdlHXz67WW9VrWrz+b6u/2r7X99Xp9f3z+8fsjn5+v74/4p0PB6/vj/dOhr9f3x9dPh96v74/gp0OR/OMSuSmVP1MmN+VyUyE3VXKokUOd/Hsa5KZJblrkpk0OHXLo`+
		`kkMPHAo+4b9dEMhNYjwQ40Ekh8R4IMYDMR6I8UCMB2I8EONBJ4fEeDDJITEeiPFAjAdiPBDjX5/wf/DXl9wUyk2x3JTKoVwOlXKolkOt/O16uWmUm2a5aZVDuxw65dANh96fckg8vUM5JJ7e4uktnt7i6S2e3q0cEk/vUQ6Jp7d4eount3h6i6fwP0/hT4cCGKBQNjiU`+
		`DQ4juUk2OJQNDmWDQ9ngUDY4lA0OZYPDTv5MssHhJDfJBoeywaFscCgbHMoGR9KZkXRmJMYjMR5JZ0ZiPBLjkRiPxHgkxiMxHonxSDozEuORdGYkxiMxHonxSIxHYjwW47F0QRzKTdIFsXRBLF0QSxfE0gVxK3876YJ4lJukC2L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inRTwt4mkRT4t4WqQzF/G0SGcu4mkRT4t4WsTTIp5W+T6t0pmroFtlg1fpzFU2eBWZq2zwKnxX2eBVNniVDV6lM1fZ4FU6c5UNXmWDV9ngVTZ4lQ3e5Ju5SWduYnwT45t05ibGNzG+ifFNjG9ifBPjmxjfpDM3Mb5JZ25ifBPjmxjfxPgmxnf5ju/Cd5fO`+
		`3KULdpG5C7pdumCXLtilM3fpgl06c5cu2KULdumCXbpgly445Jt5iKdDOvMQT4d4OsTTIZ4O8XRIZx7i6ZDOPMTTIZ4O8XSIp0M8nfJ9OqUzT0F3ygaf0pmnbPApMk/Z4FP4nrLBp2zwKRt8SmeessGndOYpG3zKBp+ywads8CkbfMk385LOvMT4JcYv6cxLjF9i/BLj`+
		`lxi/xPglxi8xfklnXmL8ks68xPglxi8xfonxS4zf8h2/he8tnXlLF9wi8xZ0t3TBLV1wS2fe0gW3dOYtXXBLF9zSBbd0wS1d8Mg38xFPj3TmI54e8fSIp0c8PeLpkc58xNMjnfmIp0c8PeLpEU/P/57+DQAA//8lgllOY1MAAA==`,
	))); err != nil {
		panic(err)
	} else if x, err := io.ReadAll(d); err != nil {
		panic(err)
	} else {
		exp = strings.TrimSpace(string(x))
	}

	set := metrics.NewSet()
	name := `test{dfgdfg="sdfsdf"}`
	gc1 := NewGeoCounter(set, name, 2)
	gc2 := NewGeoCounter2(name)

	for lat := float64(-90); lat <= 90; lat += 10 {
		for lng := float64(-180); lng <= 180; lng += 10 {
			gc1.Inc(lat, lng)
			gc2.Inc(lat, lng)
		}
	}

	var b1 strings.Builder
	set.WritePrometheus(&b1)

	var b2 strings.Builder
	gc2.WritePrometheus(&b2)

	t.Run("GeoCounter", func(t *testing.T) {
		if a := strings.TrimSpace(b1.String()); a != exp {
			t.Errorf("expected:\n\t%s\n, got\n\t%s", strings.ReplaceAll(exp, "\n", "\n\t"), strings.ReplaceAll(a, "\n", "\n\t"))
		}
	})

	t.Run("GeoCounter2", func(t *testing.T) {
		if a := strings.TrimSpace(b2.String()); a != exp {
			t.Errorf("expected:\n\t%s\n, got\n\t%s", strings.ReplaceAll(exp, "\n", "\n\t"), strings.ReplaceAll(a, "\n", "\n\t"))
		}
	})
}

func BenchmarkGeoCounter2(b *testing.B) {
	var pts [][2]float64
	for lat := float64(-90); lat <= 90; lat += 10 {
		for lng := float64(-180); lng <= 180; lng += 10 {
			pts = append(pts, [2]float64{lat, lng})
		}
	}

	b.Run("GeoCounter", func(b *testing.B) {
		set := metrics.NewSet()
		ctr := NewGeoCounter(set, `test{dfgdfg="sdfsdf"}`, 2)

		b.Run("Inc", func(b *testing.B) {
			for n := 0; n < b.N; n++ {
				pt := pts[n%len(pts)]
				ctr.Inc(pt[0], pt[1])
			}
		})

		b.Run("WritePrometheus", func(b *testing.B) {
			var buf bytes.Buffer
			set.WritePrometheus(&buf)
			b.ResetTimer()

			for n := 0; n < b.N; n++ {
				buf.Reset()
				set.WritePrometheus(&buf)
			}
		})
	})

	b.Run("GeoCounter2", func(b *testing.B) {
		ctr := NewGeoCounter2(`test{dfgdfg="sdfsdf"}`)

		b.Run("Inc", func(b *testing.B) {
			for n := 0; n < b.N; n++ {
				pt := pts[n%len(pts)]
				ctr.Inc(pt[0], pt[1])
			}
		})

		b.Run("WritePrometheus", func(b *testing.B) {
			var buf bytes.Buffer
			ctr.WritePrometheus(&buf)
			b.ResetTimer()

			for n := 0; n < b.N; n++ {
				buf.Reset()
				ctr.WritePrometheus(&buf)
			}
		})
	})
}