@@ -9,11 +9,12 @@ import (
99 "time"
1010
1111 "github.com/go-kit/log"
12- "github.com/go-kit/log/level"
13- "github.com/grafana/alloy/internal/util/zapadapter"
1412 "github.com/stretchr/testify/require"
1513 "go.uber.org/zap"
1614 "go.uber.org/zap/zapcore"
15+
16+ "github.com/grafana/alloy/internal/runtime/logging"
17+ "github.com/grafana/alloy/internal/util/zapadapter"
1718)
1819
1920func Test (t * testing.T ) {
@@ -121,6 +122,39 @@ func Test(t *testing.T) {
121122 }
122123}
123124
125+ /*
126+ As of 2025-06-04:
127+
128+ goos: darwin
129+ goarch: arm64
130+ pkg: github.com/grafana/alloy/internal/util/zapadapter
131+ cpu: Apple M2
132+ Benchmark
133+ Benchmark/No_fields_enabled-8 1352374 864.7 ns/op
134+ Benchmark/No_fields_disabled-8 6223372 193.1 ns/op
135+ Benchmark/Any_enabled-8 1000000 1332 ns/op
136+ Benchmark/Any_disabled-8 4654744 240.5 ns/op
137+ Benchmark/Bool_enabled-8 1000000 1015 ns/op
138+ Benchmark/Bool_disabled-8 5353936 253.2 ns/op
139+ Benchmark/Duration_enabled-8 1000000 1062 ns/op
140+ Benchmark/Duration_disabled-8 5175646 238.0 ns/op
141+ Benchmark/Error_enabled-8 1000000 1105 ns/op
142+ Benchmark/Error_disabled-8 4905226 267.0 ns/op
143+ Benchmark/Float32_enabled-8 1000000 1203 ns/op
144+ Benchmark/Float32_disabled-8 4813323 233.6 ns/op
145+ Benchmark/Float64_enabled-8 1000000 1037 ns/op
146+ Benchmark/Float64_disabled-8 5130016 232.8 ns/op
147+ Benchmark/Int_enabled-8 1000000 1065 ns/op
148+ Benchmark/Int_disabled-8 5154585 241.0 ns/op
149+ Benchmark/String_enabled-8 1000000 1025 ns/op
150+ Benchmark/String_disabled-8 5105998 233.3 ns/op
151+ Benchmark/Time_enabled-8 1000000 1143 ns/op
152+ Benchmark/Time_disabled-8 4857289 248.3 ns/op
153+ Benchmark/Array_enabled-8 327018 3529 ns/op
154+ Benchmark/Array_disabled-8 4889307 252.8 ns/op
155+ Benchmark/Object_enabled-8 285597 4187 ns/op
156+ Benchmark/Object_disabled-8 4864752 245.3 ns/op
157+ */
124158func Benchmark (b * testing.B ) {
125159 // Benchmark various fields that may be commonly printed.
126160
@@ -134,30 +168,45 @@ func Benchmark(b *testing.B) {
134168 runBenchmark (b , "Int" , zap .Int ("key" , 1234 ))
135169 runBenchmark (b , "String" , zap .String ("key" , "test" ))
136170 runBenchmark (b , "Time" , zap .Time ("key" , time .Date (2022 , 12 , 1 , 1 , 1 , 1 , 1 , time .UTC )))
137- runBenchmark (b , "Array" , zap .Strings ("key" , []string {"foo" , "bar" }))
171+ runBenchmark (b , "Array" , zap .Strings ("key" , []string {"foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" , "foo" , "bar" }))
138172 runBenchmark (b , "Object" , zap .Object ("key" , testObject {
139173 obj : map [string ]any {
140- "foo" : "bar" ,
141- "bar" : 123 ,
142- "baz" : true ,
174+ "foo" : "car" ,
175+ "bar" : 123 ,
176+ "baz" : true ,
177+ "foo2" : "bar2" ,
178+ "bar2" : 123 ,
179+ "baz2" : true ,
143180 "qux" : map [string ]any {
144- "foo" : "car" ,
181+ "foo" : "car" ,
182+ "bar" : 123 ,
183+ "baz" : true ,
184+ "foo2" : "bar2" ,
185+ "bar2" : 123 ,
186+ "baz2" : true ,
145187 },
146188 },
147189 }))
148190}
149191
150192func runBenchmark (b * testing.B , name string , fields ... zap.Field ) {
151- innerLogger := log .NewLogfmtLogger (io .Discard )
152- innerLogger = level .NewFilter (innerLogger , level .AllowAll ())
193+ innerLogger , err := logging .NewDeferred (io .Discard )
194+ require .NoError (b , err )
195+ err = innerLogger .Update (logging.Options {Level : logging .LevelInfo , Format : logging .FormatLogfmt })
196+ require .NoError (b , err )
153197
154198 zapLogger := zapadapter .New (innerLogger )
155199
156- b .Run (name , func (b * testing.B ) {
200+ b .Run (name + " enabled" , func (b * testing.B ) {
157201 for i := 0 ; i < b .N ; i ++ {
158202 zapLogger .Info ("Hello, world!" , fields ... )
159203 }
160204 })
205+ b .Run (name + " disabled" , func (b * testing.B ) {
206+ for i := 0 ; i < b .N ; i ++ {
207+ zapLogger .Debug ("Hello, world!" , fields ... )
208+ }
209+ })
161210}
162211
163212type testObject struct {
0 commit comments