Skip to content

Commit

Permalink
sdk/log: Change BenchmarkLoggerNewRecord to BenchmarkLoggerEmit (#6315)
Browse files Browse the repository at this point in the history
I find having benchmark for `Emit` more useful than just for
`newRecord`.
It can be used to showcase the performance benefit of using `Enabled`
even for a record with 10 attributes.

```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/log
cpu: 13th Gen Intel(R) Core(TM) i7-13800H
BenchmarkLoggerEmit/5_attributes-20               511827              2609 ns/op           41947 B/op          1 allocs/op
BenchmarkLoggerEmit/10_attributes-20             1000000              3520 ns/op           46905 B/op          5 allocs/op
BenchmarkLoggerEnabled-20                       263691399                4.549 ns/op           0 B/op          0 allocs/op
```

---------

Co-authored-by: Damien Mathieu <[email protected]>
  • Loading branch information
pellared and dmathieu authored Feb 14, 2025
1 parent 8261e7b commit 55ff06f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions sdk/log/logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import (
"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/sdk/instrumentation"
)

func BenchmarkLoggerNewRecord(b *testing.B) {
logger := newLogger(NewLoggerProvider(), instrumentation.Scope{})
func BenchmarkLoggerEmit(b *testing.B) {
logger := newTestLogger(b)

r := log.Record{}
r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
Expand Down Expand Up @@ -48,7 +47,7 @@ func BenchmarkLoggerNewRecord(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.newRecord(context.Background(), r)
logger.Emit(context.Background(), r)
}
})
})
Expand All @@ -57,18 +56,14 @@ func BenchmarkLoggerNewRecord(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.newRecord(context.Background(), r10)
logger.Emit(context.Background(), r10)
}
})
})
}

func BenchmarkLoggerEnabled(b *testing.B) {
provider := NewLoggerProvider(
WithProcessor(newFltrProcessor("0", false)),
WithProcessor(newFltrProcessor("1", true)),
)
logger := provider.Logger(b.Name())
logger := newTestLogger(b)
ctx := context.Background()
param := log.EnabledParameters{Severity: log.SeverityDebug}
var enabled bool
Expand All @@ -82,3 +77,11 @@ func BenchmarkLoggerEnabled(b *testing.B) {

_ = enabled
}

func newTestLogger(t testing.TB) log.Logger {
provider := NewLoggerProvider(
WithProcessor(newFltrProcessor("0", false)),
WithProcessor(newFltrProcessor("1", true)),
)
return provider.Logger(t.Name())
}

0 comments on commit 55ff06f

Please sign in to comment.