-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterminal_writer_bench_test.go
More file actions
248 lines (206 loc) · 5.33 KB
/
Copy pathterminal_writer_bench_test.go
File metadata and controls
248 lines (206 loc) · 5.33 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package zlog
import (
"bytes"
"fmt"
"io"
"testing"
)
func BenchmarkTerminalWriter(b *testing.B) {
tw := NewTerminalWriter(io.Discard)
l := New()
l.SetWriter(tw)
l.SetLevel(LevelInfo)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Info("The quick brown fox jumps over the lazy dog")
}
}
func BenchmarkTerminalWriterWithFields(b *testing.B) {
// Create binary log data with fields
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
tmpLogger.Info("User logged in",
String("username", "john_doe"),
Int("user_id", 42),
String("ip", "192.168.1.1"),
Bool("success", true))
data := buf.Bytes()
// Benchmark terminal writer
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
}
func BenchmarkTerminalWriterColorOutput(b *testing.B) {
// Create binary log data
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
tmpLogger.Info("Colored output test",
String("status", "active"),
Int("count", 100))
data := buf.Bytes()
// Force color mode
tw := &TerminalWriter{
out: io.Discard,
useColor: true,
timeFormat: termTimeFormat,
buf: make([]byte, 0, 2048),
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
}
func BenchmarkTerminalWriterEscapeString(b *testing.B) {
// Create binary log data with string that needs escaping
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
tmpLogger.Info("Test message",
String("escaped", "This \"string\" has\nspecial\tcharacters"))
data := buf.Bytes()
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
}
func BenchmarkTerminalWriterLongMessage(b *testing.B) {
// Create binary log data
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
longMsg := "This is a very long message that might cause buffer reallocation when formatting"
tmpLogger.Info(longMsg,
String("key1", "value1"),
String("key2", "value2"),
String("key3", "value3"))
data := buf.Bytes()
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
}
func BenchmarkTerminalWriterManyFields(b *testing.B) {
// Create binary log data with many fields
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
tmpLogger.Info("Many fields",
String("field1", "value1"),
String("field2", "value2"),
String("field3", "value3"),
String("field4", "value4"),
String("field5", "value5"),
Int("num1", 100),
Int("num2", 200),
Int("num3", 300),
Float64("float1", 3.14159),
Float64("float2", 2.71828))
data := buf.Bytes()
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
}
// Test escape string performance specifically
func BenchmarkEscapeString(b *testing.B) {
cases := []struct {
name string
str string
}{
{"NoEscape", "simple string"},
{"WithSpaces", "string with spaces"},
{"WithQuotes", `string "with" quotes`},
{"WithNewlines", "string\nwith\nnewlines"},
{"WithTabs", "string\twith\ttabs"},
{"Mixed", `complex "string" with\nnewlines\tand\rcarriage returns`},
{"Long", "very long string that might exceed stack buffer very long string that might exceed stack buffer very long string"},
}
for _, tc := range cases {
b.Run(tc.name, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = escapeString(tc.str)
}
})
}
}
// Benchmark different sizes
func BenchmarkTerminalWriterSize(b *testing.B) {
sizes := []int{1, 5, 10, 20}
for _, size := range sizes {
b.Run(fmt.Sprintf("Fields%d", size), func(b *testing.B) {
// Create binary log data
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
fields := make([]Field, size)
for i := 0; i < size; i++ {
fields[i] = String(fmt.Sprintf("field%d", i), fmt.Sprintf("value%d", i))
}
tmpLogger.Info("Test message", fields...)
data := buf.Bytes()
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
})
}
}
// Benchmark vs zerolog terminal writer equivalent
func BenchmarkTerminalWriterComparison(b *testing.B) {
b.Run("zlog", func(b *testing.B) {
tw := NewTerminalWriter(io.Discard)
l := NewStructured()
l.SetWriter(tw)
l.SetLevel(LevelInfo)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Info("Request handled",
String("method", "POST"),
String("path", "/api/users"),
Int("status", 200),
Float64("duration", 1.234))
}
})
b.Run("zlog-direct-write", func(b *testing.B) {
// Create binary log data once
var buf bytes.Buffer
tmpLogger := NewStructured()
tmpLogger.SetWriter(&buf)
tmpLogger.SetLevel(LevelInfo)
tmpLogger.Info("Request handled",
String("method", "POST"),
String("path", "/api/users"),
Int("status", 200),
Float64("duration", 1.234))
data := buf.Bytes()
tw := NewTerminalWriter(io.Discard)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tw.Write(data)
}
})
}