3 // Copyright 2021 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Package metrics provides tracking arbitrary metrics composed of
8 // values of comparable types.
17 // _Metric1 tracks metrics of values of some type.
18 type _Metric1[T comparable] struct {
23 // Add adds another instance of some value.
24 func (m *_Metric1[T]) Add(v T) {
33 // Count returns the number of instances we've seen of v.
34 func (m *_Metric1[T]) Count(v T) int {
40 // Metrics returns all the values we've seen, in an indeterminate order.
41 func (m *_Metric1[T]) Metrics() []T {
45 type key2[T1, T2 comparable] struct {
50 // _Metric2 tracks metrics of pairs of values.
51 type _Metric2[T1, T2 comparable] struct {
53 m map[key2[T1, T2]]int
56 // Add adds another instance of some pair of values.
57 func (m *_Metric2[T1, T2]) Add(v1 T1, v2 T2) {
61 m.m = make(map[key2[T1, T2]]int)
63 m.m[key2[T1, T2]{v1, v2}]++
66 // Count returns the number of instances we've seen of v1/v2.
67 func (m *_Metric2[T1, T2]) Count(v1 T1, v2 T2) int {
70 return m.m[key2[T1, T2]{v1, v2}]
73 // Metrics returns all the values we've seen, in an indeterminate order.
74 func (m *_Metric2[T1, T2]) Metrics() (r1 []T1, r2 []T2) {
75 for _, k := range _Keys(m.m) {
82 type key3[T1, T2, T3 comparable] struct {
88 // _Metric3 tracks metrics of triplets of values.
89 type _Metric3[T1, T2, T3 comparable] struct {
91 m map[key3[T1, T2, T3]]int
94 // Add adds another instance of some triplet of values.
95 func (m *_Metric3[T1, T2, T3]) Add(v1 T1, v2 T2, v3 T3) {
99 m.m = make(map[key3[T1, T2, T3]]int)
101 m.m[key3[T1, T2, T3]{v1, v2, v3}]++
104 // Count returns the number of instances we've seen of v1/v2/v3.
105 func (m *_Metric3[T1, T2, T3]) Count(v1 T1, v2 T2, v3 T3) int {
108 return m.m[key3[T1, T2, T3]{v1, v2, v3}]
111 // Metrics returns all the values we've seen, in an indeterminate order.
112 func (m *_Metric3[T1, T2, T3]) Metrics() (r1 []T1, r2 []T2, r3 []T3) {
114 r1 = append(r1, k.f1)
115 r2 = append(r2, k.f2)
116 r3 = append(r3, k.f3)
121 type S struct{ a, b, c string }
124 m1 := _Metric1[string]{}
125 if got := m1.Count("a"); got != 0 {
126 panic(fmt.Sprintf("Count(%q) = %d, want 0", "a", got))
130 if got := m1.Count("a"); got != 2 {
131 panic(fmt.Sprintf("Count(%q) = %d, want 2", "a", got))
133 if got, want := m1.Metrics(), []string{"a"}; !_SlicesEqual(got, want) {
134 panic(fmt.Sprintf("Metrics = %v, want %v", got, want))
137 m2 := _Metric2[int, float64]{}
142 k1, k2 := m2.Metrics()
146 if !_SlicesEqual(k1, w1) {
147 panic(fmt.Sprintf("_Metric2.Metrics first slice = %v, want %v", k1, w1))
151 w2 := []float64{1, 2, 3}
152 if !_SlicesEqual(k2, w2) {
153 panic(fmt.Sprintf("_Metric2.Metrics first slice = %v, want %v", k2, w2))
156 m3 := _Metric3[string, S, S]{}
157 m3.Add("a", S{"d", "e", "f"}, S{"g", "h", "i"})
158 m3.Add("a", S{"d", "e", "f"}, S{"g", "h", "i"})
159 m3.Add("a", S{"d", "e", "f"}, S{"g", "h", "i"})
160 m3.Add("b", S{"d", "e", "f"}, S{"g", "h", "i"})
161 if got := m3.Count("a", S{"d", "e", "f"}, S{"g", "h", "i"}); got != 3 {
162 panic(fmt.Sprintf("Count(%v, %v, %v) = %d, want 3", "a", S{"d", "e", "f"}, S{"g", "h", "i"}, got))
170 // _Equal reports whether two slices are equal: the same length and all
171 // elements equal. All floating point NaNs are considered equal.
172 func _SlicesEqual[Elem comparable](s1, s2 []Elem) bool {
173 if len(s1) != len(s2) {
176 for i, v1 := range s1 {
179 isNaN := func(f Elem) bool { return f != f }
180 if !isNaN(v1) || !isNaN(v2) {
188 // _Keys returns the keys of the map m.
189 // The keys will be an indeterminate order.
190 func _Keys[K comparable, V any](m map[K]V) []K {
191 r := make([]K, 0, len(m))