From 91a1588d44a691c9568f88a2d9a8efb0158663b7 Mon Sep 17 00:00:00 2001 From: "Cheng-Zhen (CZ) Yang" Date: Tue, 26 Nov 2024 20:39:38 +1100 Subject: [PATCH] otellogr: Add benchmarks (#6364) Add benchmarks (similar to otelslog) Part of https://github.com/open-telemetry/opentelemetry-go-contrib/issues/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 ``` --- bridges/otellogr/logsink_test.go | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/bridges/otellogr/logsink_test.go b/bridges/otellogr/logsink_test.go index 78d3bf491c4..e904fef00bf 100644 --- a/bridges/otellogr/logsink_test.go +++ b/bridges/otellogr/logsink_test.go @@ -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) + } + }) +}