|
14 | 14 | package promsafe_test
|
15 | 15 |
|
16 | 16 | import (
|
| 17 | + "fmt" |
17 | 18 | "log"
|
| 19 | + "testing" |
18 | 20 |
|
19 | 21 | "github.com/prometheus/client_golang/prometheus"
|
20 | 22 | "github.com/prometheus/client_golang/prometheus/promsafe"
|
@@ -89,3 +91,71 @@ func ExampleNewCounterVecT_fast_safe_labels_provider() {
|
89 | 91 |
|
90 | 92 | // Output:
|
91 | 93 | }
|
| 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