-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgokit_test.go
113 lines (91 loc) · 2.72 KB
/
gokit_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
package bench
import (
"sync"
"testing"
"github.com/go-kit/kit/log"
)
// Go kit's logger has no concept of dynamically mutable levels. The idiom is to
// predeclare your desired level during construction. This is an example helper
// constructor that performs that work. If positive is true, both info and error
// are logged. Otherwise, only error is logged.
func newLeveledLogger(logger log.Logger, positive bool) *leveledLogger {
infoLogger := log.NewNopLogger()
if positive {
infoLogger = log.With(logger, "level", "info")
}
return &leveledLogger{
Info: infoLogger,
Error: log.With(logger, "level", "error"),
}
}
type leveledLogger struct {
Info log.Logger
Error log.Logger
}
// For now, manually synchronize writes to the stream.
type synchronizedStream struct {
mtx sync.Mutex
blackholeStream
}
func (s *synchronizedStream) Write(p []byte) (int, error) {
s.mtx.Lock()
n, err := s.blackholeStream.Write(p)
s.mtx.Unlock()
return n, err
}
func BenchmarkGokitJSONPositive(b *testing.B) {
stream := &synchronizedStream{}
logger := log.With(log.NewJSONLogger(stream), "ts", log.DefaultTimestampUTC)
lvllog := newLeveledLogger(logger, true)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lvllog.Info.Log("msg", "The quick brown fox jumps over the lazy dog", "rate", 15, "low", 16, "high", 123.2)
}
})
if stream.WriteCount() != uint64(b.N) {
b.Fatalf("Log write count")
}
}
func BenchmarkGokitJSONNegative(b *testing.B) {
stream := &synchronizedStream{}
logger := log.With(log.NewJSONLogger(stream), "ts", log.DefaultTimestampUTC)
lvllog := newLeveledLogger(logger, false)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lvllog.Info.Log("msg", "The quick brown fox jumps over the lazy dog", "rate", 15, "low", 16, "high", 123.2)
}
})
if stream.WriteCount() != uint64(0) {
b.Fatalf("Log write count")
}
}
func BenchmarkGokitTextPositive(b *testing.B) {
stream := &synchronizedStream{}
logger := log.With(log.NewLogfmtLogger(stream), "ts", log.DefaultTimestampUTC)
lvllog := newLeveledLogger(logger, true)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lvllog.Info.Log("msg", "The quick brown fox jumps over the lazy dog")
}
})
if stream.WriteCount() != uint64(b.N) {
b.Fatalf("Log write count")
}
}
func BenchmarkGokitTextNegative(b *testing.B) {
stream := &synchronizedStream{}
logger := log.With(log.NewLogfmtLogger(stream), "ts", log.DefaultTimestampUTC)
lvllog := newLeveledLogger(logger, false)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lvllog.Info.Log("msg", "The quick brown fox jumps over the lazy dog")
}
})
if stream.WriteCount() != uint64(0) {
b.Fatalf("Log write count")
}
}