Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Jul 23, 2024
1 parent dc90524 commit 273af4f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions spanlogger/spanlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,77 @@ func BenchmarkSpanLogger(b *testing.B) {
})
}

func BenchmarkSpanLoggerWithRealLogger(b *testing.B) {
testCases := map[string]bool{
"all levels allowed": true,
"info and above allowed": false,
}

for name, debugEnabled := range testCases {
b.Run(name, func(b *testing.B) {
buf := bytes.NewBuffer(nil)
logger := dskit_log.NewGoKitWithWriter("logfmt", buf)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.Caller(5))

if debugEnabled {
logger = level.NewFilter(logger, level.AllowAll())
} else {
logger = level.NewFilter(logger, level.AllowInfo())
}

logger = loggerWithDebugEnabled{
Logger: logger,
debugEnabled: debugEnabled,
}

resolver := fakeResolver{}
sl, _ := New(context.Background(), logger, "test", resolver, "bar")

b.Run("log", func(b *testing.B) {
buf.Reset()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = sl.Log("msg", "foo", "more", "data")
}
})

b.Run("level.debug", func(b *testing.B) {
buf.Reset()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = level.Debug(sl).Log("msg", "foo", "more", "data")
}
})

b.Run("debuglog", func(b *testing.B) {
buf.Reset()
b.ResetTimer()

for i := 0; i < b.N; i++ {
sl.DebugLog("msg", "foo", "more", "data")
}
})
})
}

}

// Logger which does nothing and implements the DebugEnabled interface used by SpanLogger.
type noDebugNoopLogger struct{}

func (noDebugNoopLogger) Log(...interface{}) error { return nil }
func (noDebugNoopLogger) DebugEnabled() bool { return false }

// Logger which delegates to the inner log.Logger, and implements the DebugEnabled interface used by SpanLogger.
type loggerWithDebugEnabled struct {
log.Logger
debugEnabled bool
}

func (l loggerWithDebugEnabled) DebugEnabled() bool { return l.debugEnabled }

func TestSpanLogger_CallerInfo(t *testing.T) {
testCases := map[string]func(w io.Writer) log.Logger{
// This is based on Mimir's default logging configuration: https://github.com/grafana/mimir/blob/50d1c27b4ad82b265ff5a865345bec2d726f64ef/pkg/util/log/log.go#L45-L46
Expand Down

0 comments on commit 273af4f

Please sign in to comment.