From f80064a9786ef82823330fe68a24962ed14372bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Mon, 3 Jun 2024 07:14:52 +0200 Subject: [PATCH] sdk/log: Add processor benchmarks (#5448) Towards https://github.com/open-telemetry/opentelemetry-go/issues/5219 Towards https://github.com/open-telemetry/opentelemetry-go/issues/5054 This benchmarks are supposed - Validate the the `Processor` interface design from performance perspective. E.g. they are used to check if a processor that is modifying a log record is causing an additional heap allocations. - Benchmark the processors supported by the SDK. These are "almost-end-to-end" benchmarks (with noopExporter) so that it checks the performance of the SDK log processing without the actual exporting part. ``` cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz BenchmarkProcessor/Simple-16 1990946 644.6 ns/op 417 B/op 1 allocs/op BenchmarkProcessor/Batch-16 835135 1211 ns/op 597 B/op 0 allocs/op BenchmarkProcessor/ModifyTimestampSimple-16 1782510 644.3 ns/op 417 B/op 1 allocs/op BenchmarkProcessor/ModifyTimestampBatch-16 945699 1222 ns/op 637 B/op 0 allocs/op BenchmarkProcessor/ModifyAttributesSimple-16 1570214 717.6 ns/op 465 B/op 2 allocs/op BenchmarkProcessor/ModifyAttributesBatch-16 768399 1383 ns/op 653 B/op 1 allocs/op ``` --- sdk/log/bench_test.go | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 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..ff5d6fe2bfa --- /dev/null +++ b/sdk/log/bench_test.go @@ -0,0 +1,100 @@ +// 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: "ModifyTimestampSimple", + f: func() Processor { + return timestampDecorator{NewSimpleProcessor(noopExporter{})} + }, + }, + { + 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())) + 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) +} + +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) +}