Skip to content

Commit 8db2a5e

Browse files
committed
refactoring
1 parent 1aa6bf0 commit 8db2a5e

File tree

5 files changed

+168
-1
lines changed

5 files changed

+168
-1
lines changed

bridges/otelzap/encoder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func getObjectEncoder() (obj *objectEncoder, free func()) {
4242
obj.root.attrs = obj.root.attrs[:0]
4343
obj.root.name = ""
4444
obj.root.next = nil
45-
obj.cur.attrs = obj.cur.attrs[:0]
45+
obj.cur = obj.root
4646
objectEncoderPool.Put(obj)
4747
}
4848
}

bridges/otelzap/encoder_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package otelzap
77

88
import (
99
"errors"
10+
"fmt"
1011
"testing"
1112
"time"
1213

@@ -55,6 +56,7 @@ func TestObjectEncoder(t *testing.T) {
5556
arr.AppendBool(true)
5657
return nil
5758
})), "Expected AddArray to succeed.")
59+
5860
},
5961
expected: []interface{}{true, false, true},
6062
},
@@ -65,6 +67,13 @@ func TestObjectEncoder(t *testing.T) {
6567
},
6668
expected: []interface{}{wantTurducken, wantTurducken},
6769
},
70+
{
71+
desc: "AddArray-with AppendArray",
72+
f: func(e zapcore.ObjectEncoder) {
73+
assert.NoError(t, e.AddArray("k", number(2)), "Expected AddArray to succeed.")
74+
},
75+
expected: []interface{}{[]interface{}{"1"}, []interface{}{"2"}},
76+
},
6877
{
6978
desc: "AddReflected",
7079
f: func(e zapcore.ObjectEncoder) {
@@ -361,6 +370,23 @@ func (t turduckens) MarshalLogArray(enc zapcore.ArrayEncoder) error {
361370
return err
362371
}
363372

373+
type number int
374+
375+
func (t number) MarshalLogArray(enc zapcore.ArrayEncoder) error {
376+
var err error
377+
for i := 0; i < int(t); i++ {
378+
err = errors.Join(err, enc.AppendArray(numberString(fmt.Sprint(i+1))))
379+
}
380+
return err
381+
}
382+
383+
type numberString string
384+
385+
func (t numberString) MarshalLogArray(enc zapcore.ArrayEncoder) error {
386+
enc.AppendString(string(t))
387+
return nil
388+
}
389+
364390
type loggable struct{ bool }
365391

366392
func (l loggable) MarshalLogObject(enc zapcore.ObjectEncoder) error {

bridges/otelzap/new_bench.txt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
goos: linux
2+
goarch: amd64
3+
pkg: go.opentelemetry.io/contrib/bridges/otelzap
4+
cpu: 12th Gen Intel(R) Core(TM) i5-1245U
5+
BenchmarkCoreWrite/10_fields-12 3094474 416.9 ns/op 307 B/op 10 allocs/op
6+
BenchmarkCoreWrite/10_fields-12 2861959 419.0 ns/op 307 B/op 10 allocs/op
7+
BenchmarkCoreWrite/10_fields-12 2747382 491.8 ns/op 307 B/op 10 allocs/op
8+
BenchmarkCoreWrite/10_fields-12 2361176 669.9 ns/op 307 B/op 10 allocs/op
9+
BenchmarkCoreWrite/10_fields-12 1403024 1379 ns/op 306 B/op 10 allocs/op
10+
BenchmarkCoreWrite/10_fields-12 1492218 977.6 ns/op 307 B/op 10 allocs/op
11+
BenchmarkCoreWrite/10_fields-12 1333086 971.8 ns/op 307 B/op 10 allocs/op
12+
BenchmarkCoreWrite/10_fields-12 1556096 1019 ns/op 307 B/op 10 allocs/op
13+
BenchmarkCoreWrite/10_fields-12 1541168 684.7 ns/op 307 B/op 10 allocs/op
14+
BenchmarkCoreWrite/10_fields-12 1525130 814.2 ns/op 307 B/op 10 allocs/op
15+
BenchmarkCoreWrite/20_fields-12 705718 2284 ns/op 817 B/op 17 allocs/op
16+
BenchmarkCoreWrite/20_fields-12 643626 1567 ns/op 817 B/op 17 allocs/op
17+
BenchmarkCoreWrite/20_fields-12 1000000 1747 ns/op 816 B/op 17 allocs/op
18+
BenchmarkCoreWrite/20_fields-12 691274 1911 ns/op 817 B/op 17 allocs/op
19+
BenchmarkCoreWrite/20_fields-12 670533 1872 ns/op 816 B/op 17 allocs/op
20+
BenchmarkCoreWrite/20_fields-12 842696 1786 ns/op 817 B/op 17 allocs/op
21+
BenchmarkCoreWrite/20_fields-12 611466 1732 ns/op 817 B/op 17 allocs/op
22+
BenchmarkCoreWrite/20_fields-12 744956 1787 ns/op 817 B/op 17 allocs/op
23+
BenchmarkCoreWrite/20_fields-12 860582 1311 ns/op 818 B/op 17 allocs/op
24+
BenchmarkCoreWrite/20_fields-12 796299 1638 ns/op 816 B/op 17 allocs/op
25+
BenchmarkCoreWrite/Namespace-12 1230918 1129 ns/op 609 B/op 12 allocs/op
26+
BenchmarkCoreWrite/Namespace-12 1035723 1098 ns/op 609 B/op 12 allocs/op
27+
BenchmarkCoreWrite/Namespace-12 1264596 1001 ns/op 609 B/op 12 allocs/op
28+
BenchmarkCoreWrite/Namespace-12 1240990 1192 ns/op 609 B/op 12 allocs/op
29+
BenchmarkCoreWrite/Namespace-12 962514 1213 ns/op 609 B/op 12 allocs/op
30+
BenchmarkCoreWrite/Namespace-12 1202300 1131 ns/op 609 B/op 12 allocs/op
31+
BenchmarkCoreWrite/Namespace-12 1325752 987.1 ns/op 609 B/op 12 allocs/op
32+
BenchmarkCoreWrite/Namespace-12 1000000 1277 ns/op 609 B/op 12 allocs/op
33+
BenchmarkCoreWrite/Namespace-12 1119668 965.0 ns/op 609 B/op 12 allocs/op
34+
BenchmarkCoreWrite/Namespace-12 1000000 1041 ns/op 609 B/op 12 allocs/op
35+
BenchmarkCoreWrite/With10_fields-12 97766108 219.5 ns/op 208 B/op 1 allocs/op
36+
BenchmarkCoreWrite/With10_fields-12 6008856 203.6 ns/op 208 B/op 1 allocs/op
37+
BenchmarkCoreWrite/With10_fields-12 6272293 193.0 ns/op 208 B/op 1 allocs/op
38+
BenchmarkCoreWrite/With10_fields-12 6555595 210.7 ns/op 208 B/op 1 allocs/op
39+
BenchmarkCoreWrite/With10_fields-12 6299338 195.7 ns/op 208 B/op 1 allocs/op
40+
BenchmarkCoreWrite/With10_fields-12 6111976 200.9 ns/op 208 B/op 1 allocs/op
41+
BenchmarkCoreWrite/With10_fields-12 4072090 279.1 ns/op 208 B/op 1 allocs/op
42+
BenchmarkCoreWrite/With10_fields-12 6066678 201.0 ns/op 208 B/op 1 allocs/op
43+
BenchmarkCoreWrite/With10_fields-12 6148065 195.8 ns/op 208 B/op 1 allocs/op
44+
BenchmarkCoreWrite/With10_fields-12 5007072 225.5 ns/op 208 B/op 1 allocs/op
45+
BenchmarkCoreWrite/With20_fields-12 2744989 577.3 ns/op 640 B/op 1 allocs/op
46+
BenchmarkCoreWrite/With20_fields-12 1951476 540.6 ns/op 640 B/op 1 allocs/op
47+
BenchmarkCoreWrite/With20_fields-12 2507559 466.7 ns/op 640 B/op 1 allocs/op
48+
BenchmarkCoreWrite/With20_fields-12 2725330 471.3 ns/op 640 B/op 1 allocs/op
49+
BenchmarkCoreWrite/With20_fields-12 2536676 458.4 ns/op 640 B/op 1 allocs/op
50+
BenchmarkCoreWrite/With20_fields-12 2431834 499.8 ns/op 640 B/op 1 allocs/op
51+
BenchmarkCoreWrite/With20_fields-12 2171806 654.2 ns/op 640 B/op 1 allocs/op
52+
BenchmarkCoreWrite/With20_fields-12 2314248 568.2 ns/op 640 B/op 1 allocs/op
53+
BenchmarkCoreWrite/With20_fields-12 2099668 639.0 ns/op 640 B/op 1 allocs/op
54+
BenchmarkCoreWrite/With20_fields-12 2064679 615.6 ns/op 640 B/op 1 allocs/op
55+
BenchmarkCoreWrite/WithNamespace-12 100000000 12.76 ns/op 0 B/op 0 allocs/op
56+
BenchmarkCoreWrite/WithNamespace-12 100000000 17.25 ns/op 0 B/op 0 allocs/op
57+
BenchmarkCoreWrite/WithNamespace-12 70474212 20.52 ns/op 0 B/op 0 allocs/op
58+
BenchmarkCoreWrite/WithNamespace-12 85111983 13.75 ns/op 0 B/op 0 allocs/op
59+
BenchmarkCoreWrite/WithNamespace-12 88211587 14.40 ns/op 0 B/op 0 allocs/op
60+
BenchmarkCoreWrite/WithNamespace-12 94344598 12.74 ns/op 0 B/op 0 allocs/op
61+
BenchmarkCoreWrite/WithNamespace-12 95041298 12.36 ns/op 0 B/op 0 allocs/op
62+
BenchmarkCoreWrite/WithNamespace-12 81045355 13.36 ns/op 0 B/op 0 allocs/op
63+
BenchmarkCoreWrite/WithNamespace-12 98573088 13.55 ns/op 0 B/op 0 allocs/op
64+
BenchmarkCoreWrite/WithNamespace-12 94569451 12.76 ns/op 0 B/op 0 allocs/op
65+
PASS
66+
ok go.opentelemetry.io/contrib/bridges/otelzap 121.129s

bridges/otelzap/old_bench.txt

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
goos: linux
2+
goarch: amd64
3+
pkg: go.opentelemetry.io/contrib/bridges/otelzap
4+
cpu: 12th Gen Intel(R) Core(TM) i5-1245U
5+
BenchmarkCoreWrite/10_fields-12 1735168 763.3 ns/op 978 B/op 13 allocs/op
6+
BenchmarkCoreWrite/10_fields-12 1773304 719.1 ns/op 978 B/op 13 allocs/op
7+
BenchmarkCoreWrite/10_fields-12 1661734 836.2 ns/op 978 B/op 13 allocs/op
8+
BenchmarkCoreWrite/10_fields-12 1508242 763.3 ns/op 978 B/op 13 allocs/op
9+
BenchmarkCoreWrite/10_fields-12 1630060 891.2 ns/op 978 B/op 13 allocs/op
10+
BenchmarkCoreWrite/10_fields-12 1593124 854.1 ns/op 978 B/op 13 allocs/op
11+
BenchmarkCoreWrite/10_fields-12 1000000 1242 ns/op 978 B/op 13 allocs/op
12+
BenchmarkCoreWrite/10_fields-12 880255 2101 ns/op 978 B/op 13 allocs/op
13+
BenchmarkCoreWrite/10_fields-12 712956 1514 ns/op 978 B/op 13 allocs/op
14+
BenchmarkCoreWrite/10_fields-12 974938 1110 ns/op 978 B/op 13 allocs/op
15+
BenchmarkCoreWrite/20_fields-12 505944 2004 ns/op 2180 B/op 22 allocs/op
16+
BenchmarkCoreWrite/20_fields-12 606094 2288 ns/op 2180 B/op 22 allocs/op
17+
BenchmarkCoreWrite/20_fields-12 517266 2310 ns/op 2180 B/op 22 allocs/op
18+
BenchmarkCoreWrite/20_fields-12 607160 1986 ns/op 2180 B/op 22 allocs/op
19+
BenchmarkCoreWrite/20_fields-12 568624 2169 ns/op 2180 B/op 22 allocs/op
20+
BenchmarkCoreWrite/20_fields-12 633820 2062 ns/op 2180 B/op 22 allocs/op
21+
BenchmarkCoreWrite/20_fields-12 569409 2093 ns/op 2180 B/op 22 allocs/op
22+
BenchmarkCoreWrite/20_fields-12 571132 1987 ns/op 2180 B/op 22 allocs/op
23+
BenchmarkCoreWrite/20_fields-12 570067 1931 ns/op 2180 B/op 22 allocs/op
24+
BenchmarkCoreWrite/20_fields-12 665530 1988 ns/op 2180 B/op 22 allocs/op
25+
BenchmarkCoreWrite/Namespace-12 869845 1506 ns/op 1760 B/op 16 allocs/op
26+
BenchmarkCoreWrite/Namespace-12 850566 1619 ns/op 1760 B/op 16 allocs/op
27+
BenchmarkCoreWrite/Namespace-12 709729 1661 ns/op 1760 B/op 16 allocs/op
28+
BenchmarkCoreWrite/Namespace-12 767745 1853 ns/op 1760 B/op 16 allocs/op
29+
BenchmarkCoreWrite/Namespace-12 731061 1971 ns/op 1760 B/op 16 allocs/op
30+
BenchmarkCoreWrite/Namespace-12 549476 2535 ns/op 1760 B/op 16 allocs/op
31+
BenchmarkCoreWrite/Namespace-12 741138 1567 ns/op 1760 B/op 16 allocs/op
32+
BenchmarkCoreWrite/Namespace-12 810478 1633 ns/op 1760 B/op 16 allocs/op
33+
BenchmarkCoreWrite/Namespace-12 717468 1442 ns/op 1760 B/op 16 allocs/op
34+
BenchmarkCoreWrite/Namespace-12 721518 2151 ns/op 1760 B/op 16 allocs/op
35+
BenchmarkCoreWrite/With10_fields-12 6153540 209.6 ns/op 208 B/op 1 allocs/op
36+
BenchmarkCoreWrite/With10_fields-12 4717338 244.2 ns/op 208 B/op 1 allocs/op
37+
BenchmarkCoreWrite/With10_fields-12 3795830 293.2 ns/op 208 B/op 1 allocs/op
38+
BenchmarkCoreWrite/With10_fields-12 4357881 261.0 ns/op 208 B/op 1 allocs/op
39+
BenchmarkCoreWrite/With10_fields-12 4981755 237.9 ns/op 208 B/op 1 allocs/op
40+
BenchmarkCoreWrite/With10_fields-12 5230407 230.3 ns/op 208 B/op 1 allocs/op
41+
BenchmarkCoreWrite/With10_fields-12 5746520 190.0 ns/op 208 B/op 1 allocs/op
42+
BenchmarkCoreWrite/With10_fields-12 6603648 195.0 ns/op 208 B/op 1 allocs/op
43+
BenchmarkCoreWrite/With10_fields-12 6572586 187.2 ns/op 208 B/op 1 allocs/op
44+
BenchmarkCoreWrite/With10_fields-12 6856964 193.3 ns/op 208 B/op 1 allocs/op
45+
BenchmarkCoreWrite/With20_fields-12 2605052 425.6 ns/op 640 B/op 1 allocs/op
46+
BenchmarkCoreWrite/With20_fields-12 2796920 531.1 ns/op 640 B/op 1 allocs/op
47+
BenchmarkCoreWrite/With20_fields-12 2751646 446.7 ns/op 640 B/op 1 allocs/op
48+
BenchmarkCoreWrite/With20_fields-12 2467056 476.8 ns/op 640 B/op 1 allocs/op
49+
BenchmarkCoreWrite/With20_fields-12 2682984 454.6 ns/op 640 B/op 1 allocs/op
50+
BenchmarkCoreWrite/With20_fields-12 2718877 428.8 ns/op 640 B/op 1 allocs/op
51+
BenchmarkCoreWrite/With20_fields-12 2443371 566.9 ns/op 640 B/op 1 allocs/op
52+
BenchmarkCoreWrite/With20_fields-12 2254570 460.3 ns/op 640 B/op 1 allocs/op
53+
BenchmarkCoreWrite/With20_fields-12 2283828 534.3 ns/op 640 B/op 1 allocs/op
54+
BenchmarkCoreWrite/With20_fields-12 2611831 688.4 ns/op 640 B/op 1 allocs/op
55+
BenchmarkCoreWrite/WithNamespace-12 100000000 12.78 ns/op 0 B/op 0 allocs/op
56+
BenchmarkCoreWrite/WithNamespace-12 91935628 16.84 ns/op 0 B/op 0 allocs/op
57+
BenchmarkCoreWrite/WithNamespace-12 100000000 12.62 ns/op 0 B/op 0 allocs/op
58+
BenchmarkCoreWrite/WithNamespace-12 69816597 14.46 ns/op 0 B/op 0 allocs/op
59+
BenchmarkCoreWrite/WithNamespace-12 80979823 14.83 ns/op 0 B/op 0 allocs/op
60+
BenchmarkCoreWrite/WithNamespace-12 85284979 14.82 ns/op 0 B/op 0 allocs/op
61+
BenchmarkCoreWrite/WithNamespace-12 86791058 14.20 ns/op 0 B/op 0 allocs/op
62+
BenchmarkCoreWrite/WithNamespace-12 83396280 15.44 ns/op 0 B/op 0 allocs/op
63+
BenchmarkCoreWrite/WithNamespace-12 78602044 14.82 ns/op 0 B/op 0 allocs/op
64+
BenchmarkCoreWrite/WithNamespace-12 83880076 13.69 ns/op 0 B/op 0 allocs/op
65+
PASS
66+
ok go.opentelemetry.io/contrib/bridges/otelzap 96.530s

bridges/otelzap/run_benchmarh.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# Run benchmarks for previous commit
4+
git checkout zapbench
5+
for i in {1..10}; do go test -bench=. -benchmem >> old_bench.txt; done
6+
7+
# Run benchmarks for latest commit
8+
git checkout zappool
9+
for i in {1..10}; do go test -bench=. -benchmem >> new_bench.txt; done

0 commit comments

Comments
 (0)