Skip to content

Commit ab106e7

Browse files
committed
Add BenchmarkConvertMetricFamily comparing OpenMetrics 1.0 and 2.0 encoders
Signed-off-by: David Ashpole <dashpole@google.com>
1 parent 42ca72f commit ab106e7

1 file changed

Lines changed: 226 additions & 0 deletions

File tree

expfmt/benchmark_test.go

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
// Copyright The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package expfmt
15+
16+
import (
17+
"bytes"
18+
"math"
19+
"testing"
20+
21+
dto "github.com/prometheus/client_model/go"
22+
"google.golang.org/protobuf/proto"
23+
"google.golang.org/protobuf/types/known/timestamppb"
24+
)
25+
26+
func benchmarkMetricFamilies() []*dto.MetricFamily {
27+
return []*dto.MetricFamily{
28+
{
29+
Name: proto.String("http_requests_total"),
30+
Help: proto.String("Total number of HTTP requests."),
31+
Type: dto.MetricType_COUNTER.Enum(),
32+
Unit: proto.String("requests"),
33+
Metric: []*dto.Metric{
34+
{
35+
Label: []*dto.LabelPair{
36+
{Name: proto.String("method"), Value: proto.String("GET")},
37+
{Name: proto.String("handler"), Value: proto.String("/api/v1/query")},
38+
{Name: proto.String("status"), Value: proto.String("200")},
39+
},
40+
Counter: &dto.Counter{
41+
Value: proto.Float64(1027),
42+
CreatedTimestamp: &timestamppb.Timestamp{Seconds: 1234567890},
43+
Exemplar: &dto.Exemplar{
44+
Label: []*dto.LabelPair{
45+
{Name: proto.String("trace_id"), Value: proto.String("1234567890abcdef")},
46+
},
47+
Value: proto.Float64(1.5),
48+
Timestamp: &timestamppb.Timestamp{Seconds: 1234567890, Nanos: 500000000},
49+
},
50+
},
51+
TimestampMs: proto.Int64(1234567891000),
52+
},
53+
{
54+
Label: []*dto.LabelPair{
55+
{Name: proto.String("method"), Value: proto.String("POST")},
56+
{Name: proto.String("handler"), Value: proto.String("/api/v1/query")},
57+
{Name: proto.String("status"), Value: proto.String("500")},
58+
},
59+
Counter: &dto.Counter{
60+
Value: proto.Float64(3.25),
61+
CreatedTimestamp: &timestamppb.Timestamp{Seconds: 1234567890},
62+
},
63+
TimestampMs: proto.Int64(1234567891000),
64+
},
65+
},
66+
},
67+
{
68+
Name: proto.String("process_cpu_seconds_total"),
69+
Help: proto.String("Total user and system CPU time spent in seconds."),
70+
Type: dto.MetricType_GAUGE.Enum(),
71+
Unit: proto.String("seconds"),
72+
Metric: []*dto.Metric{
73+
{
74+
Label: []*dto.LabelPair{
75+
{Name: proto.String("mode"), Value: proto.String("user")},
76+
},
77+
Gauge: &dto.Gauge{Value: proto.Float64(123.45)},
78+
TimestampMs: proto.Int64(1234567891000),
79+
},
80+
{
81+
Label: []*dto.LabelPair{
82+
{Name: proto.String("mode"), Value: proto.String("system")},
83+
},
84+
Gauge: &dto.Gauge{Value: proto.Float64(12)},
85+
},
86+
},
87+
},
88+
{
89+
Name: proto.String("syslog_events_raw"),
90+
Help: proto.String("Raw untyped event count."),
91+
Type: dto.MetricType_UNTYPED.Enum(),
92+
Metric: []*dto.Metric{
93+
{
94+
Label: []*dto.LabelPair{
95+
{Name: proto.String("facility"), Value: proto.String("auth")},
96+
},
97+
Untyped: &dto.Untyped{Value: proto.Float64(42.5)},
98+
TimestampMs: proto.Int64(1234567891000),
99+
},
100+
},
101+
},
102+
{
103+
Name: proto.String("rpc_duration_seconds"),
104+
Help: proto.String("RPC latency summary."),
105+
Type: dto.MetricType_SUMMARY.Enum(),
106+
Metric: []*dto.Metric{
107+
{
108+
Label: []*dto.LabelPair{
109+
{Name: proto.String("service"), Value: proto.String("auth")},
110+
},
111+
Summary: &dto.Summary{
112+
SampleCount: proto.Uint64(100),
113+
SampleSum: proto.Float64(25.5),
114+
CreatedTimestamp: &timestamppb.Timestamp{Seconds: 1234567890},
115+
Quantile: []*dto.Quantile{
116+
{Quantile: proto.Float64(0.5), Value: proto.Float64(0.12)},
117+
{Quantile: proto.Float64(0.9), Value: proto.Float64(0.45)},
118+
{Quantile: proto.Float64(0.99), Value: proto.Float64(0.89)},
119+
},
120+
},
121+
TimestampMs: proto.Int64(1234567891000),
122+
},
123+
},
124+
},
125+
{
126+
Name: proto.String("request_duration_microseconds"),
127+
Help: proto.String("The response latency."),
128+
Type: dto.MetricType_HISTOGRAM.Enum(),
129+
Metric: []*dto.Metric{
130+
{
131+
Label: []*dto.LabelPair{
132+
{Name: proto.String("handler"), Value: proto.String("query")},
133+
},
134+
Histogram: &dto.Histogram{
135+
SampleCount: proto.Uint64(2693),
136+
SampleSum: proto.Float64(1756047.3),
137+
CreatedTimestamp: &timestamppb.Timestamp{Seconds: 1234567890},
138+
Bucket: []*dto.Bucket{
139+
{UpperBound: proto.Float64(100), CumulativeCount: proto.Uint64(123)},
140+
{
141+
UpperBound: proto.Float64(120),
142+
CumulativeCount: proto.Uint64(412),
143+
Exemplar: &dto.Exemplar{
144+
Label: []*dto.LabelPair{
145+
{Name: proto.String("trace_id"), Value: proto.String("abcdef123456")},
146+
},
147+
Value: proto.Float64(115.2),
148+
Timestamp: &timestamppb.Timestamp{Seconds: 1234567890},
149+
},
150+
},
151+
{UpperBound: proto.Float64(144), CumulativeCount: proto.Uint64(592)},
152+
{UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(2693)},
153+
},
154+
},
155+
TimestampMs: proto.Int64(1234567891000),
156+
},
157+
},
158+
},
159+
{
160+
Name: proto.String("queue_size_histogram"),
161+
Help: proto.String("Gauge histogram of queue sizes."),
162+
Type: dto.MetricType_GAUGE_HISTOGRAM.Enum(),
163+
Metric: []*dto.Metric{
164+
{
165+
Label: []*dto.LabelPair{
166+
{Name: proto.String("queue"), Value: proto.String("priority")},
167+
},
168+
Histogram: &dto.Histogram{
169+
SampleCount: proto.Uint64(500),
170+
SampleSum: proto.Float64(12500.0),
171+
Bucket: []*dto.Bucket{
172+
{UpperBound: proto.Float64(10), CumulativeCount: proto.Uint64(100)},
173+
{UpperBound: proto.Float64(20), CumulativeCount: proto.Uint64(350)},
174+
{UpperBound: proto.Float64(math.Inf(+1)), CumulativeCount: proto.Uint64(500)},
175+
},
176+
},
177+
TimestampMs: proto.Int64(1234567891000),
178+
},
179+
},
180+
},
181+
}
182+
}
183+
184+
func BenchmarkConvertMetricFamily(b *testing.B) {
185+
mfs := benchmarkMetricFamilies()
186+
187+
for _, mf := range mfs {
188+
b.Run("TEXT/"+mf.GetType().String(), func(b *testing.B) {
189+
out := bytes.NewBuffer(make([]byte, 0, 1024))
190+
b.ResetTimer()
191+
for i := 0; i < b.N; i++ {
192+
_, err := MetricFamilyToText(out, mf)
193+
if err != nil {
194+
b.Fatal(err)
195+
}
196+
out.Reset()
197+
}
198+
})
199+
b.Run("OM1.0/"+mf.GetType().String(), func(b *testing.B) {
200+
out := bytes.NewBuffer(make([]byte, 0, 1024))
201+
b.ResetTimer()
202+
for i := 0; i < b.N; i++ {
203+
_, err := MetricFamilyToOpenMetrics(out, mf)
204+
if err != nil {
205+
b.Fatal(err)
206+
}
207+
out.Reset()
208+
}
209+
})
210+
b.Run("OM2.0/"+mf.GetType().String(), func(b *testing.B) {
211+
out := bytes.NewBuffer(make([]byte, 0, 1024))
212+
if _, err := MetricFamilyToOpenMetrics20(out, mf); err != nil {
213+
b.Skipf("skipping unsupported type: %v", err)
214+
}
215+
out.Reset()
216+
b.ResetTimer()
217+
for i := 0; i < b.N; i++ {
218+
_, err := MetricFamilyToOpenMetrics20(out, mf)
219+
if err != nil {
220+
b.Fatal(err)
221+
}
222+
out.Reset()
223+
}
224+
})
225+
}
226+
}

0 commit comments

Comments
 (0)