Skip to content

Commit

Permalink
otelzap: Add Benchmarks (#5784)
Browse files Browse the repository at this point in the history
Part of #5191

Pre-work #5279

```
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/contrib/bridges/otelzap
cpu: 12th Gen Intel(R) Core(TM) i5-1245U
BenchmarkCoreWrite/10_fields-12         	 1000000	      1116 ns/op	     978 B/op	      13 allocs/op
BenchmarkCoreWrite/20_fields-12         	  407290	      2615 ns/op	    2180 B/op	      22 allocs/op
BenchmarkCoreWrite/Namespace-12         	  725268	      1671 ns/op	    1760 B/op	      16 allocs/op
BenchmarkCoreWrite/With10_fields-12     	 4964038	       260.2 ns/op	     208 B/op	       1 allocs/op
BenchmarkCoreWrite/With20_fields-12     	 2004085	       628.7 ns/op	     640 B/op	       1 allocs/op
BenchmarkCoreWrite/WithNamespace-12     	100000000	        16.95 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	go.opentelemetry.io/contrib/bridges/otelzap	8.619s
```

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
khushijain21 and pellared authored Jun 26, 2024
1 parent 669ca60 commit ff65757
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
1 change: 0 additions & 1 deletion bridges/otelzap/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func (o *Core) Write(ent zapcore.Entry, fields []zapcore.Field) error {
r.SetSeverity(convertLevel(ent.Level))
r.SetSeverityText(ent.Level.String())

// TODO: Handle zap.Namespace.
// TODO: Handle ent.LoggerName.

r.AddAttributes(o.attr...)
Expand Down
102 changes: 102 additions & 0 deletions bridges/otelzap/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package otelzap

import (
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -22,6 +24,10 @@ var (
loggerName = "name"
testKey = "key"
testValue = "value"
testEntry = zapcore.Entry{
Level: zap.InfoLevel,
Message: testMessage,
}
)

func TestCore(t *testing.T) {
Expand Down Expand Up @@ -178,3 +184,99 @@ func TestConvertLevel(t *testing.T) {
}
}
}

func BenchmarkCoreWrite(b *testing.B) {
benchmarks := []struct {
name string
fields []zapcore.Field
}{
{
name: "10 fields",
fields: []zapcore.Field{
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.Array("k", loggable{true}),
zap.String("k", "a"),
zap.Ints("k", []int{1, 2}),
},
},
{
name: "20 fields",
fields: []zapcore.Field{
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.String("k", "a"),
zap.Array("k", loggable{true}),
zap.Ints("k", []int{1, 2}),
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.ByteString("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.Array("k", loggable{true}),
zap.String("k", "a"),
zap.Ints("k", []int{1, 2}),
},
},
{ // Benchmark with nested namespace
name: "Namespace",
fields: []zapcore.Field{
zap.Namespace("a"),
zap.Int16("a", 1),
zap.String("k", "a"),
zap.Bool("k", true),
zap.Time("k", time.Unix(1000, 1000)),
zap.Binary("k", []byte{1, 2}),
zap.Namespace("b"),
zap.Binary("k", []byte{1, 2}),
zap.Object("k", loggable{true}),
zap.String("k", "a"),
zap.Array("k", loggable{true}),
zap.Ints("k", []int{1, 2}),
},
},
}

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
zc := NewCore(loggerName)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := zc.Write(testEntry, bm.fields)
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
})
})
}

for _, bm := range benchmarks {
b.Run(fmt.Sprint("With", bm.name), func(b *testing.B) {
zc := NewCore(loggerName)
zc1 := zc.With(bm.fields)
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := zc1.Write(testEntry, []zapcore.Field{})
if err != nil {
b.Errorf("Unexpected error: %v", err)
}
}
})
})
}
}

0 comments on commit ff65757

Please sign in to comment.