-
-
Save antichris/5c869d7a9609177a1def2237f2872efb to your computer and use it in GitHub Desktop.
benchmark for disabling logs with various packages
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
package main | |
import ( | |
logging "github.com/op/go-logging" | |
"io/ioutil" | |
"log" | |
"os" | |
"testing" | |
) | |
const OFF logging.Level = logging.Level(-1) | |
type NopBackend struct { | |
} | |
func (b *NopBackend) Log(level logging.Level, calldepth int, rec *logging.Record) error { | |
// noop | |
return nil | |
} | |
func BenchmarkGoLogging(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
logging.SetBackend(logging.NewLogBackend(os.Stderr, "", log.LstdFlags)) | |
} else { | |
logging.SetBackend(logging.NewLogBackend(file, "", log.LstdFlags)) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkGoLoggingNullBackend(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
logging.SetBackend(&NopBackend{}) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkGoLoggingNullBackendWithFancyFormatter(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
format := logging.MustStringFormatter( | |
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, | |
) | |
logging.SetBackend(logging.NewBackendFormatter(&NopBackend{}, format)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkGoLoggingOffLevel(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
logging.SetLevel(OFF, "benchmark") | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkGoLoggingNullBackendAndOffLevel(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
logging.SetBackend(&NopBackend{}) | |
logging.SetLevel(OFF, "benchmark") | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkGoLoggingNullBackendWithFancyFormatterAndOffLevel(b *testing.B) { | |
gologger := logging.MustGetLogger("benchmark") | |
format := logging.MustStringFormatter( | |
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`, | |
) | |
logging.SetBackend(logging.NewBackendFormatter(&NopBackend{}, format)) | |
logging.SetLevel(OFF, "benchmark") | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
gologger.Info("Benchmark %d", i) | |
} | |
} |
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
package main | |
import ( | |
log15 "gopkg.in/inconshreveable/log15.v2" | |
"io/ioutil" | |
"testing" | |
) | |
type Nop15Logger struct { | |
} | |
func (l *Nop15Logger) New(ctx ...interface{}) log15.Logger { | |
return l | |
} | |
func (l *Nop15Logger) SetHandler(h log15.Handler) { | |
// no-op | |
} | |
func (l *Nop15Logger) Debug(msg string, ctx ...interface{}) { | |
// no-op | |
} | |
func (l *Nop15Logger) Info(msg string, ctx ...interface{}) { | |
// no-op | |
} | |
func (l *Nop15Logger) Warn(msg string, ctx ...interface{}) { | |
// no-op | |
} | |
func (l *Nop15Logger) Error(msg string, ctx ...interface{}) { | |
// no-op | |
} | |
func (l *Nop15Logger) Crit(msg string, ctx ...interface{}) { | |
// no-op | |
} | |
func BenchmarkLog15(b *testing.B) { | |
logger := log15.Root() | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
logger.SetHandler(log15.StderrHandler) | |
} else { | |
logger.SetHandler(log15.Must.FileHandler(file.Name(), log15.LogfmtFormat())) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark", i) | |
} | |
} | |
func BenchmarkLog15WithDiscardHandler(b *testing.B) { | |
logger := log15.Root() | |
logger.SetHandler(log15.DiscardHandler()) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark-Discard", i) | |
} | |
} | |
func BenchmarkLog15WithDiscardHandlerAndOffLevel(b *testing.B) { | |
logger := log15.Root() | |
logger.SetHandler(log15.LvlFilterHandler(log15.Lvl(-1), log15.DiscardHandler())) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark-Discard&Off", i) | |
} | |
} | |
func BenchmarkLog15WithNopLogger(b *testing.B) { | |
logger := new(Nop15Logger) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark", i) | |
} | |
} | |
func BenchmarkLog15WithNopLoggerDiscardHandlerA(b *testing.B) { | |
logger := new(Nop15Logger) | |
logger.SetHandler(log15.DiscardHandler()) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark-Discard", i) | |
} | |
} | |
func BenchmarkLog15WithNopLoggerAndDiscardHandlerAndOffLevel(b *testing.B) { | |
logger := new(Nop15Logger) | |
logger.SetHandler(log15.LvlFilterHandler(log15.Lvl(-1), log15.DiscardHandler())) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
logger.Info("", "Benchmark-Discard&Off", i) | |
} | |
} |
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
package main | |
import ( | |
"io/ioutil" | |
"log" | |
"os" | |
"testing" | |
) | |
type NullWriter int | |
func (NullWriter) Write([]byte) (int, error) { return 0, nil } | |
type NopLogger struct { | |
*log.Logger | |
} | |
func (l *NopLogger) Printf(v ...interface{}) { | |
// noop | |
} | |
var nop = &NopLogger{ | |
log.New(os.Stderr, "", log.LstdFlags), | |
} | |
func BenchmarkLog(b *testing.B) { | |
log.SetFlags(log.LstdFlags) | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
log.SetOutput(os.Stderr) | |
} else { | |
log.SetOutput(file) | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogIoDiscardWriter(b *testing.B) { | |
log.SetFlags(log.LstdFlags) | |
log.SetOutput(ioutil.Discard) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogIoDiscardWriterWithoutFlags(b *testing.B) { | |
log.SetFlags(0) | |
log.SetOutput(ioutil.Discard) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogCustomNullWriter(b *testing.B) { | |
log.SetFlags(log.LstdFlags) | |
log.SetOutput(new(NullWriter)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogCustomNullWriterWithoutFlags(b *testing.B) { | |
log.SetFlags(0) | |
log.SetOutput(new(NullWriter)) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkNopLogger(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
nop.Printf("Benchmark %d", i) | |
} | |
} | |
func BenchmarkNopLoggerWithoutFlags(b *testing.B) { | |
log.SetFlags(0) | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
nop.Printf("Benchmark %d", i) | |
} | |
} |
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
package main | |
import ( | |
"github.com/Sirupsen/logrus" | |
"io/ioutil" | |
"os" | |
"testing" | |
) | |
type NopFormatter struct { | |
} | |
var emptyByte []byte = []byte{} | |
func (f *NopFormatter) Format(e *logrus.Entry) ([]byte, error) { | |
return emptyByte, nil | |
} | |
func BenchmarkLogrus(b *testing.B) { | |
log := logrus.New() | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
log.Out = os.Stderr | |
} else { | |
log.Out = file | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogrusWithDiscardWriter(b *testing.B) { | |
log := logrus.New() | |
log.Out = ioutil.Discard | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogrusWithNullFormatter(b *testing.B) { | |
logrus.SetFormatter(&NopFormatter{}) | |
log := logrus.New() | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
log.Out = os.Stderr | |
} else { | |
log.Out = file | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogrusWithPanicLevel(b *testing.B) { | |
logrus.SetLevel(logrus.PanicLevel) // there is no -1 or off level here as the minimum possible is PanicLevel; Say hello to uint | |
log := logrus.New() | |
file, err := ioutil.TempFile("", "benchmark-log") | |
if err != nil { | |
log.Out = os.Stderr | |
} else { | |
log.Out = file | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogrusWithDiscardWriterAndPanicLevel(b *testing.B) { | |
logrus.SetLevel(logrus.PanicLevel) // there is no -1 or off level here as the minimum possible is PanicLevel; Say hello to uint | |
log := logrus.New() | |
log.Out = ioutil.Discard | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} | |
func BenchmarkLogrusWithDiscardWriterAndNullFormatterAndPanicLevel(b *testing.B) { | |
logrus.SetFormatter(&NopFormatter{}) | |
logrus.SetLevel(logrus.PanicLevel) // there is no -1 or off level here as the minimum possible is PanicLevel; Say hello to uint | |
log := logrus.New() | |
log.Out = ioutil.Discard | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
log.Info("Benchmark %d", i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment