...
1 package logrus
2
3 import (
4 "io/ioutil"
5 "os"
6 "testing"
7 )
8
9
10 var loggerFields = Fields{
11 "foo": "bar",
12 "baz": "qux",
13 "one": "two",
14 "three": "four",
15 }
16
17 func BenchmarkDummyLogger(b *testing.B) {
18 nullf, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
19 if err != nil {
20 b.Fatalf("%v", err)
21 }
22 defer nullf.Close()
23 doLoggerBenchmark(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
24 }
25
26 func BenchmarkDummyLoggerNoLock(b *testing.B) {
27 nullf, err := os.OpenFile("/dev/null", os.O_WRONLY|os.O_APPEND, 0666)
28 if err != nil {
29 b.Fatalf("%v", err)
30 }
31 defer nullf.Close()
32 doLoggerBenchmarkNoLock(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
33 }
34
35 func doLoggerBenchmark(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
36 logger := Logger{
37 Out: out,
38 Level: InfoLevel,
39 Formatter: formatter,
40 }
41 entry := logger.WithFields(fields)
42 b.RunParallel(func(pb *testing.PB) {
43 for pb.Next() {
44 entry.Info("aaa")
45 }
46 })
47 }
48
49 func doLoggerBenchmarkNoLock(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
50 logger := Logger{
51 Out: out,
52 Level: InfoLevel,
53 Formatter: formatter,
54 }
55 logger.SetNoLock()
56 entry := logger.WithFields(fields)
57 b.RunParallel(func(pb *testing.PB) {
58 for pb.Next() {
59 entry.Info("aaa")
60 }
61 })
62 }
63
64 func BenchmarkLoggerJSONFormatter(b *testing.B) {
65 doLoggerBenchmarkWithFormatter(b, &JSONFormatter{})
66 }
67
68 func BenchmarkLoggerTextFormatter(b *testing.B) {
69 doLoggerBenchmarkWithFormatter(b, &TextFormatter{})
70 }
71
72 func doLoggerBenchmarkWithFormatter(b *testing.B, f Formatter) {
73 b.SetParallelism(100)
74 log := New()
75 log.Formatter = f
76 log.Out = ioutil.Discard
77 b.RunParallel(func(pb *testing.PB) {
78 for pb.Next() {
79 log.
80 WithField("foo1", "bar1").
81 WithField("foo2", "bar2").
82 Info("this is a dummy log")
83 }
84 })
85 }
86
View as plain text