-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathformatter_bench_test.go
More file actions
172 lines (143 loc) · 4 KB
/
formatter_bench_test.go
File metadata and controls
172 lines (143 loc) · 4 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package logrus_test
import (
"fmt"
"testing"
"time"
"github.com/sirupsen/logrus"
)
type benchStringer string
func (s benchStringer) String() string { return string(s) }
var numericFields = logrus.Fields{
"i": int(42),
"i64": int64(-1234567890),
"u": uint(99),
"u64": uint64(18446744073709551615),
"f32": float32(3.1415927),
"f64": float64(-1.2345e6),
}
var boolFields = logrus.Fields{
"t": true,
"f": false,
"x": true,
"y": false,
"yes": true,
"no": false,
}
var stringerFields = logrus.Fields{
"s1": benchStringer("alpha"),
"s2": benchStringer("beta"),
"s3": benchStringer("gamma-delta"), // includes '-' (still unquoted)
"s4": benchStringer("needs quote"), // includes space -> quoted path
"s5": benchStringer(`needs "quote"`),
"s6": benchStringer(`needs 'quote'`),
}
// 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 BenchmarkZeroTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, logrus.Fields{})
}
func BenchmarkOneStringTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, logrus.Fields{"foo": "bar"})
}
func BenchmarkOneNumericTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, logrus.Fields{"i": int(42)})
}
func BenchmarkOneBoolTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, logrus.Fields{"t": true})
}
func BenchmarkNumericTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, numericFields)
}
func BenchmarkBoolTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, boolFields)
}
func BenchmarkStringerTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, stringerFields)
}
func BenchmarkErrorTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, errorFields)
}
func BenchmarkSmallTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, smallFields)
}
func BenchmarkLargeTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{DisableColors: true}, largeFields)
}
func BenchmarkSmallColoredTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{ForceColors: true}, smallFields)
}
func BenchmarkLargeColoredTextFormatter(b *testing.B) {
doBenchmark(b, &logrus.TextFormatter{ForceColors: true}, largeFields)
}
func BenchmarkSmallJSONFormatter(b *testing.B) {
doBenchmark(b, &logrus.JSONFormatter{}, smallFields)
}
func BenchmarkLargeJSONFormatter(b *testing.B) {
doBenchmark(b, &logrus.JSONFormatter{}, largeFields)
}
var sink []byte
func doBenchmark(b *testing.B, formatter logrus.Formatter, fields logrus.Fields) {
logger := logrus.New()
entry := &logrus.Entry{
Time: time.Time{},
Level: logrus.InfoLevel,
Message: "message",
Data: fields,
Logger: logger,
}
// Warm once to determine output size and validate.
d, err := formatter.Format(entry)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(d)))
b.ReportAllocs()
b.ResetTimer()
for range b.N {
d, err = formatter.Format(entry)
if err != nil {
b.Fatal(err)
}
}
sink = d
}