-
Notifications
You must be signed in to change notification settings - Fork 6
/
stdemuxerhook_bench_test.go
121 lines (108 loc) · 2.48 KB
/
stdemuxerhook_bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package stdemuxerhook
import (
"fmt"
"github.com/Sirupsen/logrus"
"os"
"testing"
"time"
)
// smallFields is a small size data set for benchmarking
var smallFields = logrus.Fields{
"foo": "bar",
"baz": "qux",
"one": "two",
"three": "four",
}
// largeFields is a large size data set for benchmarking
var largeFields = logrus.Fields{
"foo": "bar",
"baz": "qux",
"one": "two",
"three": "four",
"five": "six",
"seven": "eight",
"nine": "ten",
"eleven": "twelve",
"thirteen": "fourteen",
"fifteen": "sixteen",
"seventeen": "eighteen",
"nineteen": "twenty",
"a": "b",
"c": "d",
"e": "f",
"g": "h",
"i": "j",
"k": "l",
"m": "n",
"o": "p",
"q": "r",
"s": "t",
"u": "v",
"w": "x",
"y": "z",
"this": "will",
"make": "thirty",
"entries": "yeah",
}
var errorFields = logrus.Fields{
"foo": fmt.Errorf("bar"),
"baz": fmt.Errorf("qux"),
}
func BenchmarkSmallNopFormatter(b *testing.B) {
doFormatterBenchmark(b, &NopFormatter{}, smallFields)
}
func BenchmarkLargeNopFormatter(b *testing.B) {
doFormatterBenchmark(b, &NopFormatter{}, largeFields)
}
func doFormatterBenchmark(b *testing.B, formatter logrus.Formatter, fields logrus.Fields) {
entry := &logrus.Entry{
Time: time.Time{},
Level: logrus.InfoLevel,
Message: "message",
Data: fields,
}
var d []byte
var err error
for i := 0; i < b.N; i++ {
d, err = formatter.Format(entry)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(d)))
}
}
func BenchmarkLoggerWithoutHook(b *testing.B) {
logger := logrus.New()
nullfs, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
if err != nil {
b.Fatalf("%v", err)
}
defer nullfs.Close()
logger.Out = nullfs
doLoggerBenchmark(b, logger, smallFields)
}
func BenchmarkLoggerWithHook(b *testing.B) {
nullfs1, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
if err != nil {
b.Fatalf("%v", err)
}
defer nullfs1.Close()
nullfs2, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
if err != nil {
b.Fatalf("%v", err)
}
defer nullfs2.Close()
logger := logrus.New()
hook := New(logger)
hook.SetOutput(nullfs1, nullfs2)
logger.Hooks.Add(hook)
doLoggerBenchmark(b, logger, smallFields)
}
func doLoggerBenchmark(b *testing.B, logger *logrus.Logger, fields logrus.Fields) {
entry := logger.WithFields(fields)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
entry.Info("aaa")
}
})
}