From 8db2a5e54fa345fe19c9befcea198607b776a97e Mon Sep 17 00:00:00 2001 From: khushijain21 Date: Mon, 1 Jul 2024 10:39:08 +0530 Subject: [PATCH] refactoring --- bridges/otelzap/encoder.go | 2 +- bridges/otelzap/encoder_test.go | 26 +++++++++++++ bridges/otelzap/new_bench.txt | 66 ++++++++++++++++++++++++++++++++ bridges/otelzap/old_bench.txt | 66 ++++++++++++++++++++++++++++++++ bridges/otelzap/run_benchmarh.sh | 9 +++++ 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 bridges/otelzap/new_bench.txt create mode 100644 bridges/otelzap/old_bench.txt create mode 100755 bridges/otelzap/run_benchmarh.sh diff --git a/bridges/otelzap/encoder.go b/bridges/otelzap/encoder.go index 9ee01f86a18..ff2ccb6d10c 100644 --- a/bridges/otelzap/encoder.go +++ b/bridges/otelzap/encoder.go @@ -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) } } diff --git a/bridges/otelzap/encoder_test.go b/bridges/otelzap/encoder_test.go index 4e284b82d35..71bc20181c7 100644 --- a/bridges/otelzap/encoder_test.go +++ b/bridges/otelzap/encoder_test.go @@ -7,6 +7,7 @@ package otelzap import ( "errors" + "fmt" "testing" "time" @@ -55,6 +56,7 @@ func TestObjectEncoder(t *testing.T) { arr.AppendBool(true) return nil })), "Expected AddArray to succeed.") + }, expected: []interface{}{true, false, true}, }, @@ -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) { @@ -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 { diff --git a/bridges/otelzap/new_bench.txt b/bridges/otelzap/new_bench.txt new file mode 100644 index 00000000000..7b156a875e9 --- /dev/null +++ b/bridges/otelzap/new_bench.txt @@ -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 diff --git a/bridges/otelzap/old_bench.txt b/bridges/otelzap/old_bench.txt new file mode 100644 index 00000000000..12aad2445f4 --- /dev/null +++ b/bridges/otelzap/old_bench.txt @@ -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 diff --git a/bridges/otelzap/run_benchmarh.sh b/bridges/otelzap/run_benchmarh.sh new file mode 100755 index 00000000000..48ea7fcb2ba --- /dev/null +++ b/bridges/otelzap/run_benchmarh.sh @@ -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