Debian Codegraph @

bridges/otelslog/bench_test.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelslog
5
6 import (
7 "log/slog"
8 "testing"
9 "time"
10 )
11
12 func BenchmarkHandler(b *testing.B) {
13 var (
14 h slog.Handler
15 err error
16 )
17
18 attrs10 := []slog.Attr{
19 slog.String("1", "1"),
20 slog.Int64("2", 2),
21 slog.Int("3", 3),
22 slog.Uint64("4", 4),
23 slog.Float64("5", 5.),
24 slog.Bool("6", true),
25 slog.Time("7", time.Now()),
26 slog.Duration("8", time.Second),
27 slog.Any("9", 9),
28 slog.Any("10", "10"),
29 }
30 attrs5 := attrs10[:5]
31 record := slog.NewRecord(time.Now(), slog.LevelInfo, "body", 0)
32 ctx := b.Context()
33
34 b.Run("Handle", func(b *testing.B) {
35 handlers := make([]*Handler, b.N)
36 for i := range handlers {
37 handlers[i] = NewHandler("")
38 }
39
40 b.ReportAllocs()
41 b.ResetTimer()
42 for n := range b.N {
43 err = handlers[n].Handle(ctx, record)
44 }
45 })
46
47 b.Run("WithAttrs", func(b *testing.B) {
48 b.Run("5", func(b *testing.B) {
49 handlers := make([]*Handler, b.N)
50 for i := range handlers {
51 handlers[i] = NewHandler("")
52 }
53
54 b.ReportAllocs()
55 b.ResetTimer()
56 for n := range b.N {
57 h = handlers[n].WithAttrs(attrs5)
58 }
59 })
60 b.Run("10", func(b *testing.B) {
61 handlers := make([]*Handler, b.N)
62 for i := range handlers {
63 handlers[i] = NewHandler("")
64 }
65
66 b.ReportAllocs()
67 b.ResetTimer()
68 for n := range b.N {
69 h = handlers[n].WithAttrs(attrs10)
70 }
71 })
72 })
73
74 b.Run("WithGroup", func(b *testing.B) {
75 handlers := make([]*Handler, b.N)
76 for i := range handlers {
77 handlers[i] = NewHandler("")
78 }
79
80 b.ReportAllocs()
81 b.ResetTimer()
82 for n := range b.N {
83 h = handlers[n].WithGroup("group")
84 }
85 })
86
87 b.Run("WithGroup.WithAttrs", func(b *testing.B) {
88 b.Run("5", func(b *testing.B) {
89 handlers := make([]*Handler, b.N)
90 for i := range handlers {
91 handlers[i] = NewHandler("")
92 }
93
94 b.ReportAllocs()
95 b.ResetTimer()
96 for n := range b.N {
97 h = handlers[n].WithGroup("group").WithAttrs(attrs5)
98 }
99 })
100 b.Run("10", func(b *testing.B) {
101 handlers := make([]*Handler, b.N)
102 for i := range handlers {
103 handlers[i] = NewHandler("")
104 }
105
106 b.ReportAllocs()
107 b.ResetTimer()
108 for n := range b.N {
109 h = handlers[n].WithGroup("group").WithAttrs(attrs10)
110 }
111 })
112 })
113
114 b.Run("(WithGroup.WithAttrs).Handle", func(b *testing.B) {
115 b.Run("5", func(b *testing.B) {
116 handlers := make([]slog.Handler, b.N)
117 for i := range handlers {
118 handlers[i] = NewHandler("").WithGroup("group").WithAttrs(attrs5)
119 }
120
121 b.ReportAllocs()
122 b.ResetTimer()
123 for n := range b.N {
124 err = handlers[n].Handle(ctx, record)
125 }
126 })
127 b.Run("10", func(b *testing.B) {
128 handlers := make([]slog.Handler, b.N)
129 for i := range handlers {
130 handlers[i] = NewHandler("").WithGroup("group").WithAttrs(attrs10)
131 }
132
133 b.ReportAllocs()
134 b.ResetTimer()
135 for n := range b.N {
136 err = handlers[n].Handle(ctx, record)
137 }
138 })
139 })
140
141 b.Run("(WithSource).Handle", func(b *testing.B) {
142 handlers := make([]*Handler, b.N)
143 for i := range handlers {
144 handlers[i] = NewHandler("", WithSource(true))
145 }
146
147 b.ReportAllocs()
148 b.ResetTimer()
149 for n := range b.N {
150 err = handlers[n].Handle(ctx, record)
151 }
152 })
153
154 _, _ = h, err
155 }