-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblip_test.go
More file actions
217 lines (197 loc) · 6.05 KB
/
blip_test.go
File metadata and controls
217 lines (197 loc) · 6.05 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
package blip_test
import (
"bytes"
"context"
"encoding/json"
"io"
"strings"
"testing"
"time"
"github.com/localhots/blip"
"github.com/localhots/blip/ctx/log"
)
//
// Fuzz
//
func FuzzBlip(f *testing.F) {
cfg := blip.DefaultConfig()
cfg.Encoder = blip.NewJSONEncoder()
ctx := context.Background()
// Seed inputs
f.Add("test message with key-value pair", "key", "value")
f.Add("test message with spaces in value", "key", "value with spaces")
// Different alphabets
f.Add("message with special chars", "yay", "!@#$%^&*()")
f.Add("消息带有Unicode字符", "键", "你好")
f.Add("メッセージにUnicode文字が含まれています", "キー", "こんにちは")
f.Add("Сообщение с Unicode символами", "ключ", "Привет")
f.Add("رسالة تحتوي على أحرف Unicode", "مفتاح", "مرحبا")
f.Add("Besked med Unicode-tegn", "nøgle", "Hej")
// Invalid UTF-8 sequences
f.Add("message with invalid UTF-8 chars \xff\xfe\xfd", "key", string([]byte{0xff, 0xfe, 0xfd}))
f.Add("message with invalid UTF-8 chars \x80\x81\x82", "key", string([]byte{0x80, 0x81, 0x82}))
f.Add("message with invalid UTF-8 chars \xED\xA0\x80", "key", string([]byte{0xED, 0xA0, 0x80}))
f.Add("message with invalid UTF-8 chars \xED\xB0\x80", "key", string([]byte{0xED, 0xB0, 0x80}))
f.Add("message with invalid UTF-8 sequence \xED\xA0\x80\xED\xB0\x80", "key", string([]byte{0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80}))
f.Add("message with repeated invalid UTF-8 sequence \xED\xA0\x80\xED\xB0\x80\xED\xA0\x80", "key", string([]byte{0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80}))
f.Add("message with long invalid UTF-8 sequence \xED\xA0\x80\xED\xB0\x80\xED\xA0\x80\xED\xB0\x80", "key", string([]byte{0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80, 0xED, 0xA0, 0x80, 0xED, 0xB0, 0x80}))
// Very long strings
f.Add("message with long string "+strings.Repeat("a", 1000), "key", "value with a long string "+strings.Repeat("a", 1000))
f.Add("message with very long string "+strings.Repeat("a", 10000), "key", "value with a very long string "+strings.Repeat("a", 10000))
f.Fuzz(func(t *testing.T, msg string, fkey string, fval string) {
var buf bytes.Buffer
cfg.Output = &buf
logger := blip.New(cfg)
logger.Info(ctx, msg, log.F{fkey: fval})
// Validate the output
if buf.Len() == 0 {
t.Error("Expected non-empty buffer")
}
var out any
if err := json.Unmarshal(buf.Bytes(), &out); err != nil {
t.Logf("msg=%q key=%q val=%q", msg, fkey, fval)
t.Log(buf.String())
t.Errorf("Failed to unmarshal JSON: %v", err)
}
if _, ok := out.(map[string]any); !ok {
t.Errorf("Expected JSON object, got %T", out)
}
})
}
//
// Benchmarks
//
func BenchmarkJSON(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
StackTraceLevel: blip.LevelError,
Encoder: blip.NewJSONEncoder(),
})
ctx := context.Background()
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}
func BenchmarkBare(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
StackTraceLevel: blip.LevelError,
Encoder: &blip.ConsoleEncoder{},
})
ctx := context.Background()
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}
func BenchmarkOptimized(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
Encoder: &blip.ConsoleEncoder{
TimeFormat: "2006-01-02 15:04:05.000",
TimePrecision: 1 * time.Millisecond,
Color: false,
MinMessageWidth: 0,
SortFields: false,
},
StackTraceLevel: blip.LevelError,
})
ctx := context.Background()
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}
func BenchmarkPretty(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
Encoder: &blip.ConsoleEncoder{
TimeFormat: "2006-01-02 15:04:05.000",
TimePrecision: 1 * time.Millisecond,
Color: true,
MinMessageWidth: 40,
SortFields: false,
},
StackTraceLevel: blip.LevelError,
})
ctx := context.Background()
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}
func BenchmarkPrettySorted(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
Encoder: &blip.ConsoleEncoder{
TimeFormat: "2006-01-02 15:04:05.000",
TimePrecision: 1 * time.Millisecond,
Color: true,
MinMessageWidth: 40,
SortFields: true,
},
StackTraceLevel: blip.LevelError,
})
ctx := context.Background()
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}
func BenchmarkPrettySortedContext(b *testing.B) {
log.Setup(blip.Config{
Level: blip.LevelDebug,
Output: io.Discard,
Encoder: &blip.ConsoleEncoder{
TimeFormat: "2006-01-02 15:04:05.000",
TimePrecision: 1 * time.Millisecond,
Color: true,
MinMessageWidth: 40,
SortFields: true,
},
StackTraceLevel: blip.LevelError,
})
ctx := context.Background()
ctx = blip.ContextWithFields(ctx, log.F{"foo": "bar"})
ctx = blip.ContextWithFields(ctx, log.F{"one": "two"})
b.ResetTimer()
for range b.N {
log.Info(ctx, "Starting task", log.F{
"device_unique_id": "G4000E-1000-F",
"task_id": 123456,
"status": "success",
"template_name": "index.tpl",
})
}
}