Skip to content

Commit 30f9c92

Browse files
committed
promsafe benchmarks
1 parent c9abe92 commit 30f9c92

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

prometheus/promsafe/safe_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
package promsafe_test
1515

1616
import (
17+
"fmt"
1718
"log"
19+
"testing"
1820

1921
"github.com/prometheus/client_golang/prometheus"
2022
"github.com/prometheus/client_golang/prometheus/promsafe"
@@ -89,3 +91,71 @@ func ExampleNewCounterVecT_fast_safe_labels_provider() {
8991

9092
// Output:
9193
}
94+
95+
// ====================
96+
// Benchmark Tests
97+
// ====================
98+
99+
type TestLabels struct {
100+
promsafe.StructLabelProvider
101+
Label1 string
102+
Label2 int
103+
Label3 bool
104+
}
105+
106+
type TestLabelsFast struct {
107+
promsafe.StructLabelProvider
108+
Label1 string
109+
Label2 int
110+
Label3 bool
111+
}
112+
113+
func (t TestLabelsFast) ToPrometheusLabels() prometheus.Labels {
114+
return prometheus.Labels{
115+
"label1": t.Label1,
116+
"label2": fmt.Sprintf("%d", t.Label2),
117+
"label3": fmt.Sprintf("%t", t.Label3),
118+
}
119+
}
120+
func (t TestLabelsFast) ToLabelNames() []string {
121+
return []string{"label1", "label2", "label3"}
122+
}
123+
124+
func BenchmarkCompareCreatingMetric(b *testing.B) {
125+
126+
// Note: on stage of creation metrics, Unique metric names are not required,
127+
// but let it be for consistency
128+
129+
b.Run("Prometheus NewCounterVec", func(b *testing.B) {
130+
for i := 0; i < b.N; i++ {
131+
uniqueMetricName := fmt.Sprintf("test_counter_prometheus_%d_%d", b.N, i)
132+
133+
_ = prometheus.NewCounterVec(prometheus.CounterOpts{
134+
Name: uniqueMetricName,
135+
Help: "A test counter created just using prometheus.NewCounterVec",
136+
}, []string{"label1", "label2", "label3"})
137+
}
138+
})
139+
140+
b.Run("Promsafe (reflect) NewCounterVec", func(b *testing.B) {
141+
for i := 0; i < b.N; i++ {
142+
uniqueMetricName := fmt.Sprintf("test_counter_promsafe_%d_%d", b.N, i)
143+
144+
_ = promsafe.NewCounterVec[TestLabels](prometheus.CounterOpts{
145+
Name: uniqueMetricName,
146+
Help: "A test counter created using promauto.NewCounterVec",
147+
})
148+
}
149+
})
150+
151+
b.Run("Promsafe (fast) NewCounterVec", func(b *testing.B) {
152+
for i := 0; i < b.N; i++ {
153+
uniqueMetricName := fmt.Sprintf("test_counter_promsafe_fast_%d_%d", b.N, i)
154+
155+
_ = promsafe.NewCounterVec[TestLabelsFast](prometheus.CounterOpts{
156+
Name: uniqueMetricName,
157+
Help: "A test counter created using promauto.NewCounterVec",
158+
})
159+
}
160+
})
161+
}

0 commit comments

Comments
 (0)