Skip to content

Commit

Permalink
otellogr: Add benchmarks (#6364)
Browse files Browse the repository at this point in the history
Add benchmarks (similar to otelslog)
Part of
#5192

Benchmark results:
```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/contrib/bridges/otellogr
cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz
BenchmarkLogSink/Info-16                 6114177               186.4 ns/op           160 B/op          1 allocs/op
BenchmarkLogSink/Error-16                6649935               245.2 ns/op           160 B/op          1 allocs/op
BenchmarkLogSink/WithValues-16           3949730               295.6 ns/op           448 B/op          3 allocs/op
BenchmarkLogSink/WithName-16             6813362               172.0 ns/op           133 B/op          2 allocs/op
BenchmarkLogSink/WithName.WithValues-16                  2420191               606.4 ns/op           581 B/op          5 allocs/op
BenchmarkLogSink/(WithName.WithValues).Info-16          12601381                97.32 ns/op            0 B/op          0 allocs/op
PASS
ok      go.opentelemetry.io/contrib/bridges/otellogr    24.125s
```
  • Loading branch information
scorpionknifes authored Nov 26, 2024
1 parent 0da64d1 commit 91a1588
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,92 @@ func TestConvertKVs(t *testing.T) {
})
}
}

func BenchmarkLogSink(b *testing.B) {
message := "body"
keyValues := []any{
"string", "hello",
"int", 42,
"float", 3.14,
"bool", false,
}
err := errors.New("error")

b.Run("Info", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("")
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].Info(0, message, keyValues...)
}
})

b.Run("Error", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("")
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].Error(err, message, keyValues...)
}
})

b.Run("WithValues", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("")
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].WithValues(keyValues...)
}
})

b.Run("WithName", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("")
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].WithName("name")
}
})

b.Run("WithName.WithValues", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("")
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].WithName("name").WithValues(keyValues...)
}
})

b.Run("(WithName.WithValues).Info", func(b *testing.B) {
logSinks := make([]logr.LogSink, b.N)
for i := range logSinks {
logSinks[i] = NewLogSink("").WithName("name").WithValues(keyValues...)
}

b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
logSinks[n].Info(0, message)
}
})
}

0 comments on commit 91a1588

Please sign in to comment.