Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for logger #5101

Merged
merged 6 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions sdk/log/logger_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package log // import "go.opentelemetry.io/otel/sdk/log"

import (
"context"
"testing"
"time"

"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")
r.AddAttributes(
log.String("k1", "str"),
log.Float64("k2", 1.0),
log.Int("k3", 2),
log.Bool("k4", true),
log.Bytes("k5", []byte{1}),
)

b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.newRecord(context.Background(), r)
}
})
}
35 changes: 35 additions & 0 deletions sdk/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,38 @@ func TestLoggerEnabled(t *testing.T) {
})
}
}

func TestAllocationLimits(t *testing.T) {
const runs = 1
XSAM marked this conversation as resolved.
Show resolved Hide resolved

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(t, 10, r10.AttributesLen())

assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
logger.newRecord(context.Background(), r)
}), "newRecord")

// TODO: Optimize this allocation count to 1.
assert.Equal(t, 8.0, testing.AllocsPerRun(runs, func() {
XSAM marked this conversation as resolved.
Show resolved Hide resolved
logger.newRecord(context.Background(), r10)
}), "newRecord with 10 attributes")
}
Loading