Skip to content

Commit

Permalink
Remove AllocsPerRun for non-zero allocation cases
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM committed Mar 25, 2024
1 parent 50b1031 commit fe5f410
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
36 changes: 27 additions & 9 deletions sdk/log/logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/sdk/instrumentation"
)
Expand All @@ -21,19 +23,35 @@ func BenchmarkLoggerNewRecord(b *testing.B) {
r.SetBody(log.StringValue("testing body value"))
r.SetSeverity(log.SeverityInfo)
r.SetSeverityText("testing text")
r.AddAttributes(

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}),
)

b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.newRecord(context.Background(), r)
}
}
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)
}
})
})
}
15 changes: 2 additions & 13 deletions sdk/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,25 +286,14 @@ func TestAllocationLimits(t *testing.T) {
r.SetSeverity(log.SeverityInfo)
r.SetSeverityText("testing text")

attrs5 := []log.KeyValue{
log.String("k1", "str"),
r.AddAttributes(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() {
logger.newRecord(context.Background(), r10)
}), "newRecord with 10 attributes")
}

0 comments on commit fe5f410

Please sign in to comment.