From acb2471488f2ccf5c59a454d849af962fe43f0c2 Mon Sep 17 00:00:00 2001 From: Damien Mathieu Date: Thu, 27 Jun 2024 13:39:58 +0200 Subject: [PATCH] Add benchmark retrieving a new logger (#5548) This adds a benchmark to create a logger from a logger provider. Related: #5054. ``` BenchmarkLoggerProviderLogger-10 3145390 548.8 ns/op 330 B/op 1 allocs/op ``` --- sdk/log/provider_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/sdk/log/provider_test.go b/sdk/log/provider_test.go index bfa8afcda1d..55dc516a7b1 100644 --- a/sdk/log/provider_test.go +++ b/sdk/log/provider_test.go @@ -5,6 +5,7 @@ package log // import "go.opentelemetry.io/otel/sdk/log" import ( "context" + "fmt" "strconv" "sync" "testing" @@ -17,6 +18,7 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/internal/global" + "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/noop" "go.opentelemetry.io/otel/sdk/resource" ) @@ -287,3 +289,22 @@ func TestLoggerProviderForceFlush(t *testing.T) { assert.ErrorIs(t, p.ForceFlush(ctx), assert.AnError, "processor error not returned") }) } + +func BenchmarkLoggerProviderLogger(b *testing.B) { + p := NewLoggerProvider() + names := make([]string, b.N) + for i := 0; i < b.N; i++ { + names[i] = fmt.Sprintf("%d logger", i) + } + + b.ResetTimer() + b.ReportAllocs() + + loggers := make([]log.Logger, b.N) + for i := 0; i < b.N; i++ { + loggers[i] = p.Logger(names[i]) + } + + b.StopTimer() + loggers[0].Enabled(context.Background(), log.Record{}) +}