Skip to content

Instantly share code, notes, and snippets.

@utahta
Created February 3, 2017 12:25
Show Gist options
  • Save utahta/040c8fe81846306da2dc4f65938ff3e9 to your computer and use it in GitHub Desktop.
Save utahta/040c8fe81846306da2dc4f65938ff3e9 to your computer and use it in GitHub Desktop.
Benchmark test echo logger in the same way as uber-bo/zap.
package log_test
import (
"io/ioutil"
"testing"
"github.com/labstack/gommon/log"
)
func withBenchedLogger(b *testing.B, f func(*log.Logger)) {
logger := log.New("-")
logger.SetOutput(ioutil.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
f(logger)
}
})
}
func BenchmarkInfo(b *testing.B) {
withBenchedLogger(b, func(l *log.Logger) {
l.Info("bench")
})
}
func BenchmarkInfof(b *testing.B) {
withBenchedLogger(b, func(l *log.Logger) {
l.Infof("bench %s", "test")
})
}
// tested on MacBook Pro 2016 16GB Corei7 3.3G
//
// BenchmarkLog-4 500000 2747 ns/op
// BenchmarkInfo-4 500000 2704 ns/op
// BenchmarkInfof-4 500000 2785 ns/op
// PASS
// ok github.com/labstack/gommon/log 4.274s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment