Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Jul 1, 2024
1 parent 1aa6bf0 commit 8db2a5e
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bridges/otelzap/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func getObjectEncoder() (obj *objectEncoder, free func()) {
obj.root.attrs = obj.root.attrs[:0]
obj.root.name = ""
obj.root.next = nil
obj.cur.attrs = obj.cur.attrs[:0]
obj.cur = obj.root
objectEncoderPool.Put(obj)
}
}
Expand Down
26 changes: 26 additions & 0 deletions bridges/otelzap/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package otelzap

import (
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -55,6 +56,7 @@ func TestObjectEncoder(t *testing.T) {
arr.AppendBool(true)
return nil
})), "Expected AddArray to succeed.")

},
expected: []interface{}{true, false, true},
},
Expand All @@ -65,6 +67,13 @@ func TestObjectEncoder(t *testing.T) {
},
expected: []interface{}{wantTurducken, wantTurducken},
},
{
desc: "AddArray-with AppendArray",
f: func(e zapcore.ObjectEncoder) {
assert.NoError(t, e.AddArray("k", number(2)), "Expected AddArray to succeed.")
},
expected: []interface{}{[]interface{}{"1"}, []interface{}{"2"}},
},
{
desc: "AddReflected",
f: func(e zapcore.ObjectEncoder) {
Expand Down Expand Up @@ -361,6 +370,23 @@ func (t turduckens) MarshalLogArray(enc zapcore.ArrayEncoder) error {
return err
}

type number int

func (t number) MarshalLogArray(enc zapcore.ArrayEncoder) error {
var err error
for i := 0; i < int(t); i++ {
err = errors.Join(err, enc.AppendArray(numberString(fmt.Sprint(i+1))))
}
return err
}

type numberString string

func (t numberString) MarshalLogArray(enc zapcore.ArrayEncoder) error {
enc.AppendString(string(t))
return nil
}

type loggable struct{ bool }

func (l loggable) MarshalLogObject(enc zapcore.ObjectEncoder) error {
Expand Down
66 changes: 66 additions & 0 deletions bridges/otelzap/new_bench.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/contrib/bridges/otelzap
cpu: 12th Gen Intel(R) Core(TM) i5-1245U
BenchmarkCoreWrite/10_fields-12 3094474 416.9 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 2861959 419.0 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 2747382 491.8 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 2361176 669.9 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1403024 1379 ns/op 306 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1492218 977.6 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1333086 971.8 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1556096 1019 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1541168 684.7 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/10_fields-12 1525130 814.2 ns/op 307 B/op 10 allocs/op
BenchmarkCoreWrite/20_fields-12 705718 2284 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 643626 1567 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 1000000 1747 ns/op 816 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 691274 1911 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 670533 1872 ns/op 816 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 842696 1786 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 611466 1732 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 744956 1787 ns/op 817 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 860582 1311 ns/op 818 B/op 17 allocs/op
BenchmarkCoreWrite/20_fields-12 796299 1638 ns/op 816 B/op 17 allocs/op
BenchmarkCoreWrite/Namespace-12 1230918 1129 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1035723 1098 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1264596 1001 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1240990 1192 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 962514 1213 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1202300 1131 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1325752 987.1 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1000000 1277 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1119668 965.0 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/Namespace-12 1000000 1041 ns/op 609 B/op 12 allocs/op
BenchmarkCoreWrite/With10_fields-12 97766108 219.5 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6008856 203.6 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6272293 193.0 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6555595 210.7 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6299338 195.7 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6111976 200.9 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 4072090 279.1 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6066678 201.0 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6148065 195.8 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 5007072 225.5 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2744989 577.3 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 1951476 540.6 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2507559 466.7 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2725330 471.3 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2536676 458.4 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2431834 499.8 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2171806 654.2 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2314248 568.2 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2099668 639.0 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2064679 615.6 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/WithNamespace-12 100000000 12.76 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 100000000 17.25 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 70474212 20.52 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 85111983 13.75 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 88211587 14.40 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 94344598 12.74 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 95041298 12.36 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 81045355 13.36 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 98573088 13.55 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 94569451 12.76 ns/op 0 B/op 0 allocs/op
PASS
ok go.opentelemetry.io/contrib/bridges/otelzap 121.129s
66 changes: 66 additions & 0 deletions bridges/otelzap/old_bench.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/contrib/bridges/otelzap
cpu: 12th Gen Intel(R) Core(TM) i5-1245U
BenchmarkCoreWrite/10_fields-12 1735168 763.3 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1773304 719.1 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1661734 836.2 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1508242 763.3 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1630060 891.2 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1593124 854.1 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 1000000 1242 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 880255 2101 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 712956 1514 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/10_fields-12 974938 1110 ns/op 978 B/op 13 allocs/op
BenchmarkCoreWrite/20_fields-12 505944 2004 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 606094 2288 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 517266 2310 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 607160 1986 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 568624 2169 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 633820 2062 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 569409 2093 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 571132 1987 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 570067 1931 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/20_fields-12 665530 1988 ns/op 2180 B/op 22 allocs/op
BenchmarkCoreWrite/Namespace-12 869845 1506 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 850566 1619 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 709729 1661 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 767745 1853 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 731061 1971 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 549476 2535 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 741138 1567 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 810478 1633 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 717468 1442 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/Namespace-12 721518 2151 ns/op 1760 B/op 16 allocs/op
BenchmarkCoreWrite/With10_fields-12 6153540 209.6 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 4717338 244.2 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 3795830 293.2 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 4357881 261.0 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 4981755 237.9 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 5230407 230.3 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 5746520 190.0 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6603648 195.0 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6572586 187.2 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With10_fields-12 6856964 193.3 ns/op 208 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2605052 425.6 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2796920 531.1 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2751646 446.7 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2467056 476.8 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2682984 454.6 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2718877 428.8 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2443371 566.9 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2254570 460.3 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2283828 534.3 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/With20_fields-12 2611831 688.4 ns/op 640 B/op 1 allocs/op
BenchmarkCoreWrite/WithNamespace-12 100000000 12.78 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 91935628 16.84 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 100000000 12.62 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 69816597 14.46 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 80979823 14.83 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 85284979 14.82 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 86791058 14.20 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 83396280 15.44 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 78602044 14.82 ns/op 0 B/op 0 allocs/op
BenchmarkCoreWrite/WithNamespace-12 83880076 13.69 ns/op 0 B/op 0 allocs/op
PASS
ok go.opentelemetry.io/contrib/bridges/otelzap 96.530s
9 changes: 9 additions & 0 deletions bridges/otelzap/run_benchmarh.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Run benchmarks for previous commit
git checkout zapbench
for i in {1..10}; do go test -bench=. -benchmem >> old_bench.txt; done

# Run benchmarks for latest commit
git checkout zappool
for i in {1..10}; do go test -bench=. -benchmem >> new_bench.txt; done

0 comments on commit 8db2a5e

Please sign in to comment.