Skip to content

Commit

Permalink
Merge pull request #4 from cabify/colega/no-debug-logger
Browse files Browse the repository at this point in the history
Added NoDebugLogger that doesn't format debug logs
  • Loading branch information
colega authored Feb 21, 2019
2 parents ec9d7cc + 7aabe92 commit 2e59705
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions no_debug_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package log

// NoDebugLogger embeds a Logger, but in calls to debug functions it does nothing.
// It avoids doing fmt.Sprintf() for those calls as they will be discarded anyways.
// This makes those calls like 50 times faster (see benchmark file)
type NoDebugLogger struct {
Logger
}

func (NoDebugLogger) Debug(args ...interface{}) {}
func (NoDebugLogger) Debugf(format string, args ...interface{}) {}
func (NoDebugLogger) Debugln(args ...interface{}) {}
50 changes: 50 additions & 0 deletions no_debug_logger_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package log

import (
"testing"
)

/*
$ go test -bench=. ./...
| goos: darwin
| goarch: amd64
| pkg: github.com/cabify/go-logging
| BenchmarkPlainLoggerDebugSpeed-4 30000000 45.9 ns/op
| BenchmarkNoDebugLoggerDebugSpeed-4 2000000000 0.33 ns/op
| BenchmarkPlainLoggerDebugfSpeed-4 20000000 80.8 ns/op
| BenchmarkNoDebugLoggerDebugfSpeed-4 50000000 36.5 ns/op
*/

type someStruct struct {
value float64
}

var (
structValue = someStruct{value: 10.0}
plainLoggerInstance = NewLogger("test")
noDebugLoggerInstance = NoDebugLogger{Logger: plainLoggerInstance}
)

func BenchmarkPlainLoggerDebugSpeed(b *testing.B) {
for i := 0; i < b.N; i++ {
plainLoggerInstance.Debug("Something")
}
}

func BenchmarkNoDebugLoggerDebugSpeed(b *testing.B) {
for i := 0; i < b.N; i++ {
noDebugLoggerInstance.Debug("Something")
}
}

func BenchmarkPlainLoggerDebugfSpeed(b *testing.B) {
for i := 0; i < b.N; i++ {
plainLoggerInstance.Debugf("Something %v, %d", structValue, i)
}
}

func BenchmarkNoDebugLoggerDebugfSpeed(b *testing.B) {
for i := 0; i < b.N; i++ {
noDebugLoggerInstance.Debugf("Something %v, %d", structValue, i)
}
}

0 comments on commit 2e59705

Please sign in to comment.