-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
131 lines (111 loc) · 2.57 KB
/
Copy pathbenchmark_test.go
File metadata and controls
131 lines (111 loc) · 2.57 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
package logfmtr_test
import (
"fmt"
"testing"
"github.com/go-logr/logr"
"github.com/iand/logfmtr"
)
// Benchmarks copied from github.com/go-logr/logr/benchmark
//go:noinline
func doInfoOneArg(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Info("this is", "a", "string")
}
}
//go:noinline
func doInfoSeveralArgs(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Info("multi",
"bool", true, "string", "str", "int", 42,
"float", 3.14, "struct", struct{ X, Y int }{93, 76})
}
}
//go:noinline
func doV0Info(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.V(0).Info("multi",
"bool", true, "string", "str", "int", 42,
"float", 3.14, "struct", struct{ X, Y int }{93, 76})
}
}
//go:noinline
func doV9Info(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.V(9).Info("multi",
"bool", true, "string", "str", "int", 42,
"float", 3.14, "struct", struct{ X, Y int }{93, 76})
}
}
//go:noinline
func doError(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
err := fmt.Errorf("error message")
for i := 0; i < b.N; i++ {
log.Error(err, "multi",
"bool", true, "string", "str", "int", 42,
"float", 3.14, "struct", struct{ X, Y int }{93, 76})
}
}
//go:noinline
func doWithValues(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l := log.WithValues("k1", "v1", "k2", "v2")
_ = l
}
}
//go:noinline
func doWithName(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l := log.WithName("name")
_ = l
}
}
//go:noinline
func doWithCallDepth(b *testing.B, log logr.Logger) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l := log.WithCallDepth(1)
_ = l
}
}
func newLogger() logr.Logger {
return logfmtr.NewWithOptions(discard())
}
func BenchmarkLogfmtrInfoOneArg(b *testing.B) {
doInfoOneArg(b, newLogger())
}
func BenchmarkLogfmtrInfoSeveralArgs(b *testing.B) {
doInfoSeveralArgs(b, newLogger())
}
func BenchmarkLogfmtrV0Info(b *testing.B) {
doV0Info(b, newLogger())
}
func BenchmarkLogfmtrV9Info(b *testing.B) {
doV9Info(b, newLogger())
}
func BenchmarkLogfmtrError(b *testing.B) {
doError(b, newLogger())
}
func BenchmarkLogfmtrWithValues(b *testing.B) {
doWithValues(b, newLogger())
}
func BenchmarkLogfmtrWithName(b *testing.B) {
doWithName(b, newLogger())
}
func BenchmarkLogfmtrWithCallDepth(b *testing.B) {
doWithCallDepth(b, newLogger())
}