14
14
package prometheus
15
15
16
16
import (
17
+ "fmt"
17
18
"testing"
18
19
)
19
20
20
21
func TestDelete (t * testing.T ) {
21
- desc := NewDesc ("test" , "helpless" , []string {"l1" , "l2" }, nil )
22
- vec := MetricVec {
23
- children : map [uint64 ]Metric {},
24
- desc : desc ,
25
- newMetric : func (lvs ... string ) Metric {
26
- return newValue (desc , UntypedValue , 0 , lvs ... )
27
- },
28
- }
22
+ vec := newUntypedMetricVec ("test" , "helpless" , []string {"l1" , "l2" })
29
23
30
24
if got , want := vec .Delete (Labels {"l1" : "v1" , "l2" : "v2" }), false ; got != want {
31
25
t .Errorf ("got %v, want %v" , got , want )
@@ -57,14 +51,7 @@ func TestDelete(t *testing.T) {
57
51
}
58
52
59
53
func TestDeleteLabelValues (t * testing.T ) {
60
- desc := NewDesc ("test" , "helpless" , []string {"l1" , "l2" }, nil )
61
- vec := MetricVec {
62
- children : map [uint64 ]Metric {},
63
- desc : desc ,
64
- newMetric : func (lvs ... string ) Metric {
65
- return newValue (desc , UntypedValue , 0 , lvs ... )
66
- },
67
- }
54
+ vec := newUntypedMetricVec ("test" , "helpless" , []string {"l1" , "l2" })
68
55
69
56
if got , want := vec .DeleteLabelValues ("v1" , "v2" ), false ; got != want {
70
57
t .Errorf ("got %v, want %v" , got , want )
@@ -86,3 +73,77 @@ func TestDeleteLabelValues(t *testing.T) {
86
73
t .Errorf ("got %v, want %v" , got , want )
87
74
}
88
75
}
76
+
77
+ func newUntypedMetricVec (name , help string , labels []string ) * MetricVec {
78
+ desc := NewDesc ("test" , "helpless" , labels , nil )
79
+ vec := newMetricVec (desc , func (lvs ... string ) Metric {
80
+ return newValue (desc , UntypedValue , 0 , lvs ... )
81
+ })
82
+ return & vec
83
+ }
84
+
85
+ func BenchmarkMetricVecWithLabelValuesBasic (B * testing.B ) {
86
+ benchmarkMetricVecWithLabelValues (B , map [string ][]string {
87
+ "l1" : []string {"onevalue" },
88
+ "l2" : []string {"twovalue" },
89
+ })
90
+ }
91
+
92
+ func BenchmarkMetricVecWithLabelValues2Keys10ValueCardinality (B * testing.B ) {
93
+ benchmarkMetricVecWithLabelValuesCardinality (B , 2 , 10 )
94
+ }
95
+
96
+ func BenchmarkMetricVecWithLabelValues4Keys10ValueCardinality (B * testing.B ) {
97
+ benchmarkMetricVecWithLabelValuesCardinality (B , 4 , 10 )
98
+ }
99
+
100
+ func BenchmarkMetricVecWithLabelValues2Keys100ValueCardinality (B * testing.B ) {
101
+ benchmarkMetricVecWithLabelValuesCardinality (B , 2 , 100 )
102
+ }
103
+
104
+ func BenchmarkMetricVecWithLabelValues10Keys100ValueCardinality (B * testing.B ) {
105
+ benchmarkMetricVecWithLabelValuesCardinality (B , 10 , 100 )
106
+ }
107
+
108
+ func BenchmarkMetricVecWithLabelValues10Keys1000ValueCardinality (B * testing.B ) {
109
+ benchmarkMetricVecWithLabelValuesCardinality (B , 10 , 1000 )
110
+ }
111
+
112
+ func benchmarkMetricVecWithLabelValuesCardinality (B * testing.B , nkeys , nvalues int ) {
113
+ labels := map [string ][]string {}
114
+
115
+ for i := 0 ; i < nkeys ; i ++ {
116
+ var (
117
+ k = fmt .Sprintf ("key-%v" , i )
118
+ vs = make ([]string , 0 , nvalues )
119
+ )
120
+ for j := 0 ; j < nvalues ; j ++ {
121
+ vs = append (vs , fmt .Sprintf ("value-%v" , j ))
122
+ }
123
+ labels [k ] = vs
124
+ }
125
+
126
+ benchmarkMetricVecWithLabelValues (B , labels )
127
+ }
128
+
129
+ func benchmarkMetricVecWithLabelValues (B * testing.B , labels map [string ][]string ) {
130
+ var keys []string
131
+ for k := range labels { // map order dependent, who cares though
132
+ keys = append (keys , k )
133
+ }
134
+
135
+ values := make ([]string , len (labels )) // value cache for permutations
136
+ vec := newUntypedMetricVec ("test" , "helpless" , keys )
137
+
138
+ B .ReportAllocs ()
139
+ B .ResetTimer ()
140
+ for i := 0 ; i < B .N ; i ++ {
141
+ // varies input across provide map entries based on key size.
142
+ for j , k := range keys {
143
+ candidates := labels [k ]
144
+ values [j ] = candidates [i % len (candidates )]
145
+ }
146
+
147
+ vec .WithLabelValues (values ... )
148
+ }
149
+ }
0 commit comments