From 33612859fc88192e4849fb5da80e60422bfe41f6 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Fri, 29 Mar 2024 12:58:16 -0700 Subject: [PATCH] Add benchmarks to otelslog (#5330) * Add benchmarks to otelslog * Bench different attr lengths --- bridges/otelslog/handler_test.go | 132 +++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/bridges/otelslog/handler_test.go b/bridges/otelslog/handler_test.go index 5b7c85a9897..49ee1d51f10 100644 --- a/bridges/otelslog/handler_test.go +++ b/bridges/otelslog/handler_test.go @@ -467,3 +467,135 @@ func TestHandlerEnabled(t *testing.T) { ctx = context.WithValue(ctx, enableKey, true) assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed") } + +func BenchmarkHandler(b *testing.B) { + var ( + h slog.Handler + err error + ) + + attrs10 := []slog.Attr{ + slog.String("1", "1"), + slog.Int64("2", 2), + slog.Int("3", 3), + slog.Uint64("4", 4), + slog.Float64("5", 5.), + slog.Bool("6", true), + slog.Time("7", time.Now()), + slog.Duration("8", time.Second), + slog.Any("9", 9), + slog.Any("10", "10"), + } + attrs5 := attrs10[:5] + record := slog.NewRecord(time.Now(), slog.LevelInfo, "body", 0) + ctx := context.Background() + + b.Run("Handle", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + err = handlers[n].Handle(ctx, record) + } + }) + + b.Run("WithAttrs", func(b *testing.B) { + b.Run("5", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + h = handlers[n].WithAttrs(attrs5) + } + }) + b.Run("10", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + h = handlers[n].WithAttrs(attrs10) + } + }) + }) + + b.Run("WithGroup", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + h = handlers[n].WithGroup("group") + } + }) + + b.Run("WithGroup.WithAttrs", func(b *testing.B) { + b.Run("5", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + h = handlers[n].WithGroup("group").WithAttrs(attrs5) + } + }) + b.Run("10", func(b *testing.B) { + handlers := make([]*Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler() + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + h = handlers[n].WithGroup("group").WithAttrs(attrs10) + } + }) + }) + + b.Run("(WithGroup.WithAttrs).Handle", func(b *testing.B) { + b.Run("5", func(b *testing.B) { + handlers := make([]slog.Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler().WithGroup("group").WithAttrs(attrs5) + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + err = handlers[n].Handle(ctx, record) + } + }) + b.Run("10", func(b *testing.B) { + handlers := make([]slog.Handler, b.N) + for i := range handlers { + handlers[i] = NewHandler().WithGroup("group").WithAttrs(attrs10) + } + + b.ReportAllocs() + b.ResetTimer() + for n := 0; n < b.N; n++ { + err = handlers[n].Handle(ctx, record) + } + }) + }) + + _, _ = h, err +}