-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
186 lines (147 loc) · 4.21 KB
/
benchmark_test.go
File metadata and controls
186 lines (147 loc) · 4.21 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package mtlog
import (
"io"
"testing"
"time"
"github.com/willibrandon/mtlog/core"
"github.com/willibrandon/mtlog/sinks"
)
// discardSink is a sink that discards all events (for benchmarking)
type discardSink struct{}
func (d *discardSink) Emit(event *core.LogEvent) {}
func (d *discardSink) Close() error { return nil }
func (d *discardSink) EmitSimple(timestamp time.Time, level core.LogEventLevel, message string) {}
// Benchmark simple logging without properties
func BenchmarkSimpleLog(b *testing.B) {
logger := New(WithSink(&discardSink{}))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("This is a simple log message")
}
}
// Benchmark logging with properties
func BenchmarkLogWithProperties(b *testing.B) {
logger := New(WithSink(&discardSink{}))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("User {UserId} performed action {Action}", 123, "login")
}
}
// Benchmark logging with multiple properties
func BenchmarkLogWithManyProperties(b *testing.B) {
logger := New(WithSink(&discardSink{}))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("User {UserId} from {Country} using {Browser} on {OS} performed {Action}",
123, "USA", "Chrome", "Windows", "login")
}
}
// Benchmark logging with context
func BenchmarkLogWithContext(b *testing.B) {
logger := New(WithSink(&discardSink{}))
ctxLogger := logger.ForContext("Environment", "Production").
ForContext("Version", "1.0.0").
ForContext("Region", "us-east-1")
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
ctxLogger.Information("Processing request")
}
}
// Benchmark logging below minimum level (should be very fast)
func BenchmarkLogBelowMinimumLevel(b *testing.B) {
logger := New(
WithSink(&discardSink{}),
WithMinimumLevel(core.InformationLevel),
)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Debug("This should not be processed")
}
}
// Benchmark with enrichers
func BenchmarkLogWithEnrichers(b *testing.B) {
logger := New(
WithSink(&discardSink{}),
// Add test enrichers
WithEnricher(&benchEnricher{name: "Prop1", value: "Value1"}),
WithEnricher(&benchEnricher{name: "Prop2", value: "Value2"}),
WithEnricher(&benchEnricher{name: "Prop3", value: "Value3"}),
)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("Test message")
}
}
// Benchmark console sink
func BenchmarkConsoleSink(b *testing.B) {
logger := New(WithSink(sinks.NewConsoleSinkWithWriter(io.Discard)))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("User {UserId} logged in", 123)
}
}
// Benchmark parallel logging
func BenchmarkParallelLogging(b *testing.B) {
logger := New(WithSink(&discardSink{}))
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Information("Parallel log message {ThreadId}", 1)
}
})
}
// Benchmark template parsing (cached vs uncached)
func BenchmarkTemplateParsing(b *testing.B) {
logger := New(WithSink(&discardSink{}))
// Use different templates to avoid caching
templates := []string{
"User {UserId} logged in",
"Order {OrderId} processed",
"Payment {PaymentId} received",
"Item {ItemId} shipped",
"Customer {CustomerId} registered",
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
template := templates[i%len(templates)]
logger.Information(template, i)
}
}
// Benchmark structured object logging
func BenchmarkStructuredObject(b *testing.B) {
logger := New(WithSink(&discardSink{}))
user := struct {
ID int
Name string
Email string
Role string
}{
ID: 123,
Name: "Alice",
Email: "alice@example.com",
Role: "Admin",
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
logger.Information("Processing user {@User}", user)
}
}
// Helper enricher for benchmarks
type benchEnricher struct {
name string
value any
}
func (be *benchEnricher) Enrich(event *core.LogEvent, propertyFactory core.LogEventPropertyFactory) {
prop := propertyFactory.CreateProperty(be.name, be.value)
event.Properties[prop.Name] = prop.Value
}