Skip to content

Commit

Permalink
Do not allocate memory when there's no constraints
Browse files Browse the repository at this point in the history
Signed-off-by: Quentin Devos <4972091+Okhoshi@users.noreply.github.com>
  • Loading branch information
Okhoshi committed Jun 16, 2023
1 parent c3fa5a7 commit 3c01a8d
Show file tree
Hide file tree
Showing 15 changed files with 582 additions and 103 deletions.
101 changes: 101 additions & 0 deletions prometheus/benchmark_test.go
Expand Up @@ -33,6 +33,29 @@ func BenchmarkCounterWithLabelValues(b *testing.B) {
}
}

func BenchmarkCounterWithLabelValuesAndConstraints(b *testing.B) {
m := V2.NewCounterVec(
CounterVecOpts{
CounterOpts: CounterOpts{
Name: "benchmark_counter",
Help: "A counter to benchmark it.",
},
VariableLabels: ConstrainedLabels{
ConstrainedLabel{Name: "one"},
ConstrainedLabel{Name: "two", Constraint: func(s string) string {
return "two"
}},
ConstrainedLabel{Name: "three"},
},
},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.WithLabelValues("eins", "zwei", "drei").Inc()
}
}

func BenchmarkCounterWithLabelValuesConcurrent(b *testing.B) {
m := NewCounterVec(
CounterOpts{
Expand All @@ -56,6 +79,37 @@ func BenchmarkCounterWithLabelValuesConcurrent(b *testing.B) {
wg.Wait()
}

func BenchmarkCounterWithLabelValuesAndConstraintsConcurrent(b *testing.B) {
m := V2.NewCounterVec(
CounterVecOpts{
CounterOpts: CounterOpts{
Name: "benchmark_counter",
Help: "A counter to benchmark it.",
},
VariableLabels: ConstrainedLabels{
ConstrainedLabel{Name: "one"},
ConstrainedLabel{Name: "two", Constraint: func(s string) string {
return "two"
}},
ConstrainedLabel{Name: "three"},
},
},
)
b.ReportAllocs()
b.ResetTimer()
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for j := 0; j < b.N/10; j++ {
m.WithLabelValues("eins", "zwei", "drei").Inc()
}
wg.Done()
}()
}
wg.Wait()
}

func BenchmarkCounterWithMappedLabels(b *testing.B) {
m := NewCounterVec(
CounterOpts{
Expand All @@ -71,6 +125,29 @@ func BenchmarkCounterWithMappedLabels(b *testing.B) {
}
}

func BenchmarkCounterWithMappedLabelsAndConstraints(b *testing.B) {
m := V2.NewCounterVec(
CounterVecOpts{
CounterOpts: CounterOpts{
Name: "benchmark_counter",
Help: "A counter to benchmark it.",
},
VariableLabels: ConstrainedLabels{
ConstrainedLabel{Name: "one"},
ConstrainedLabel{Name: "two", Constraint: func(s string) string {
return "two"
}},
ConstrainedLabel{Name: "three"},
},
},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
m.With(Labels{"two": "zwei", "one": "eins", "three": "drei"}).Inc()
}
}

func BenchmarkCounterWithPreparedMappedLabels(b *testing.B) {
m := NewCounterVec(
CounterOpts{
Expand All @@ -87,6 +164,30 @@ func BenchmarkCounterWithPreparedMappedLabels(b *testing.B) {
}
}

func BenchmarkCounterWithPreparedMappedLabelsAndConstraints(b *testing.B) {
m := V2.NewCounterVec(
CounterVecOpts{
CounterOpts: CounterOpts{
Name: "benchmark_counter",
Help: "A counter to benchmark it.",
},
VariableLabels: ConstrainedLabels{
ConstrainedLabel{Name: "one"},
ConstrainedLabel{Name: "two", Constraint: func(s string) string {
return "two"
}},
ConstrainedLabel{Name: "three"},
},
},
)
b.ReportAllocs()
b.ResetTimer()
labels := Labels{"two": "zwei", "one": "eins", "three": "drei"}
for i := 0; i < b.N; i++ {
m.With(labels).Inc()
}
}

func BenchmarkCounterNoLabels(b *testing.B) {
m := NewCounter(CounterOpts{
Name: "benchmark_counter",
Expand Down

0 comments on commit 3c01a8d

Please sign in to comment.