-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from cabify/colega/no-debug-logger
Added NoDebugLogger that doesn't format debug logs
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |