From cb2bda2c54ef6b2cb42e5f5e30bc7420ae40ab11 Mon Sep 17 00:00:00 2001 From: plan Date: Sat, 18 Jun 2016 22:20:51 +0800 Subject: [PATCH] Add benchmark for logger --- logger_bench_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 logger_bench_test.go diff --git a/logger_bench_test.go b/logger_bench_test.go new file mode 100644 index 0000000..28a1a26 --- /dev/null +++ b/logger_bench_test.go @@ -0,0 +1,37 @@ +package logrus + +import ( + "os" + "testing" +) + +// smallFields is a small size data set for benchmarking +var loggerFields = Fields{ + "foo": "bar", + "baz": "qux", + "one": "two", + "three": "four", +} + +func BenchmarkDummyLogger(b *testing.B) { + nullf, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666) + if err != nil { + b.Fatalf("%v", err) + } + defer nullf.Close() + doLoggerBenchmark(b, nullf, &TextFormatter{DisableColors: true}, smallFields) +} + +func doLoggerBenchmark(b *testing.B, out *os.File, formatter Formatter, fields Fields) { + logger := Logger{ + Out: out, + Level: InfoLevel, + Formatter: formatter, + } + entry := logger.WithFields(fields) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + entry.Info("aaa") + } + }) +}