1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-27 22:49:15 +02:00
Files
opentelemetry-go/sdk/log/logger_bench_test.go
Robert Pająk 55ff06fbdd sdk/log: Change BenchmarkLoggerNewRecord to BenchmarkLoggerEmit (#6315)
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 <42@dmathieu.com>
2025-02-14 09:40:02 +01:00

88 lines
1.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package log // import "go.opentelemetry.io/otel/sdk/log"
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/log"
)
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))
r.SetObservedTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
r.SetBody(log.StringValue("testing body value"))
r.SetSeverity(log.SeverityInfo)
r.SetSeverityText("testing text")
r.AddAttributes(
log.String("k1", "str"),
log.Float64("k2", 1.0),
log.Int("k3", 2),
log.Bool("k4", true),
log.Bytes("k5", []byte{1}),
)
r10 := r
r10.AddAttributes(
log.String("k6", "str"),
log.Float64("k7", 1.0),
log.Int("k8", 2),
log.Bool("k9", true),
log.Bytes("k10", []byte{1}),
)
require.Equal(b, 5, r.AttributesLen())
require.Equal(b, 10, r10.AttributesLen())
b.Run("5 attributes", func(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Emit(context.Background(), r)
}
})
})
b.Run("10 attributes", func(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Emit(context.Background(), r10)
}
})
})
}
func BenchmarkLoggerEnabled(b *testing.B) {
logger := newTestLogger(b)
ctx := context.Background()
param := log.EnabledParameters{Severity: log.SeverityDebug}
var enabled bool
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
enabled = logger.Enabled(ctx, param)
}
_ = enabled
}
func newTestLogger(t testing.TB) log.Logger {
provider := NewLoggerProvider(
WithProcessor(newFltrProcessor("0", false)),
WithProcessor(newFltrProcessor("1", true)),
)
return provider.Logger(t.Name())
}