From df371c6d99206091965d31c22343e8fee2cc7446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 31 May 2024 11:04:58 +0200 Subject: [PATCH 1/2] sdk/log: Add processor benchmarks --- sdk/log/bench_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 sdk/log/bench_test.go diff --git a/sdk/log/bench_test.go b/sdk/log/bench_test.go new file mode 100644 index 00000000000..0736ccb7518 --- /dev/null +++ b/sdk/log/bench_test.go @@ -0,0 +1,78 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package log + +import ( + "context" + "testing" + "time" + + "go.opentelemetry.io/otel/log" + + "github.com/stretchr/testify/assert" +) + +func BenchmarkProcessor(b *testing.B) { + for _, tc := range []struct { + name string + f func() Processor + }{ + { + name: "Simple", + f: func() Processor { + return NewSimpleProcessor(noopExporter{}) + }, + }, + { + name: "Batch", + f: func() Processor { + return NewBatchProcessor(noopExporter{}) + }, + }, + { + name: "ModifySimple", + f: func() Processor { + return timestampDecorator{NewSimpleProcessor(noopExporter{})} + }, + }, + { + name: "ModifyBatch", + f: func() Processor { + return timestampDecorator{NewBatchProcessor(noopExporter{})} + }, + }, + } { + b.Run(tc.name, func(b *testing.B) { + provider := NewLoggerProvider(WithProcessor(tc.f())) + b.Cleanup(func() { assert.NoError(b, provider.Shutdown(context.Background())) }) + logger := provider.Logger(b.Name()) + + b.ReportAllocs() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + r := log.Record{} + r.SetBody(log.StringValue("message")) + r.SetSeverity(log.SeverityInfo) + r.AddAttributes( + log.String("foo", "bar"), + log.Float64("float", 3.14), + log.Int("int", 123), + log.Bool("bool", true), + ) + logger.Emit(context.Background(), r) + } + }) + }) + } +} + +type timestampDecorator struct { + Processor +} + +func (e timestampDecorator) OnEmit(ctx context.Context, r Record) error { + r = r.Clone() + r.SetObservedTimestamp(time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC)) + return e.Processor.OnEmit(ctx, r) +} From 7dad83905bdad68037e5efe4feaf1162d11954f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Fri, 31 May 2024 11:14:58 +0200 Subject: [PATCH 2/2] Add more cases --- sdk/log/bench_test.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/sdk/log/bench_test.go b/sdk/log/bench_test.go index 0736ccb7518..ff5d6fe2bfa 100644 --- a/sdk/log/bench_test.go +++ b/sdk/log/bench_test.go @@ -31,17 +31,29 @@ func BenchmarkProcessor(b *testing.B) { }, }, { - name: "ModifySimple", + name: "ModifyTimestampSimple", f: func() Processor { return timestampDecorator{NewSimpleProcessor(noopExporter{})} }, }, { - name: "ModifyBatch", + name: "ModifyTimestampBatch", f: func() Processor { return timestampDecorator{NewBatchProcessor(noopExporter{})} }, }, + { + name: "ModifyAttributesSimple", + f: func() Processor { + return attrDecorator{NewSimpleProcessor(noopExporter{})} + }, + }, + { + name: "ModifyAttributesBatch", + f: func() Processor { + return attrDecorator{NewBatchProcessor(noopExporter{})} + }, + }, } { b.Run(tc.name, func(b *testing.B) { provider := NewLoggerProvider(WithProcessor(tc.f())) @@ -76,3 +88,13 @@ func (e timestampDecorator) OnEmit(ctx context.Context, r Record) error { r.SetObservedTimestamp(time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC)) return e.Processor.OnEmit(ctx, r) } + +type attrDecorator struct { + Processor +} + +func (e attrDecorator) OnEmit(ctx context.Context, r Record) error { + r = r.Clone() + r.SetAttributes(log.String("replace", "me")) + return e.Processor.OnEmit(ctx, r) +}