mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-28 21:09:17 +02:00
Add benchmark for logger (#5101)
This commit is contained in:
parent
6033938c87
commit
f1ba32e95e
57
sdk/log/logger_bench_test.go
Normal file
57
sdk/log/logger_bench_test.go
Normal file
@ -0,0 +1,57 @@
|
||||
// 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/assert"
|
||||
|
||||
"go.opentelemetry.io/otel/log"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
)
|
||||
|
||||
func BenchmarkLoggerNewRecord(b *testing.B) {
|
||||
logger := newLogger(NewLoggerProvider(), instrumentation.Scope{})
|
||||
|
||||
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")
|
||||
|
||||
attrs5 := []log.KeyValue{
|
||||
log.String("k1", "str"),
|
||||
log.Float64("k2", 1.0),
|
||||
log.Int("k3", 2),
|
||||
log.Bool("k4", true),
|
||||
log.Bytes("k5", []byte{1}),
|
||||
}
|
||||
r.AddAttributes(attrs5...)
|
||||
|
||||
r10 := r
|
||||
r10.AddAttributes(attrs5...)
|
||||
assert.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.newRecord(context.Background(), r)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
b.Run("10 attributes", func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
logger.newRecord(context.Background(), r10)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
@ -273,3 +273,28 @@ func TestLoggerEnabled(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllocationLimits(t *testing.T) {
|
||||
const runs = 10
|
||||
|
||||
logger := newLogger(NewLoggerProvider(), instrumentation.Scope{})
|
||||
|
||||
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}),
|
||||
)
|
||||
|
||||
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
|
||||
logger.newRecord(context.Background(), r)
|
||||
}), "newRecord")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user