-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
217 lines (203 loc) · 5.3 KB
/
format_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
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 loggo
import (
"bytes"
"strings"
"sync"
"testing"
"time"
)
func test(format Formatter, record *Record) []byte {
return test1(format, record)
}
func test1(format Formatter, record *Record) []byte {
return test2(format, record)
}
func test2(format Formatter, record *Record) []byte {
return test3(format, record)
}
func test3(format Formatter, record *Record) []byte {
return test4(format, record)
}
func test4(format Formatter, record *Record) []byte {
return test5(format, record)
}
func test5(format Formatter, record *Record) []byte {
return test6(format, record)
}
func test6(format Formatter, record *Record) []byte {
output := &bytes.Buffer{}
format.Format(0, record, output)
return output.Bytes()
}
var (
aaa = "Hello,%s!"
testTime, _ = time.Parse(time.RFC3339Nano, "2018-04-27T19:22:01.40731219+08:00")
testRecord = []*Record{
&Record{DEBUG, 123, testTime, &aaa, []interface{}{"world"}},
&Record{WARNING, 23652523, testTime, nil, []interface{}{"Hello,world!"}},
}
testFormat = []string{
"%{message}",
"[%{level}] %{callpath:3} ===",
"%{time:01-02T15:04:05.999999} %{level} %{shortfile} %{shortfunc} %{message}",
"%{%{}}",
"%{unkown}",
}
formater = MustStringFormatter(testFormat[2])
)
func TestFormater(t *testing.T) {
t.Parallel()
result := test(formater, testRecord[0])
if string(result) != "04-27T19:22:01.407312 DEBUG format_test.go:31 test6 Hello,world!\n" {
t.Fatalf("failt: %s", result)
}
t.Logf(string(result))
}
func TestFormatter(t *testing.T) {
//tests := []struct{
// record *TestRecord
// format string
// result string
//}
}
func TestNewFormatter(t *testing.T) {
//"%{message}",
//"[%{level}] %{callpath:3} ===",
//"%{time:01-02T15:04:05.999999} %{level} %{shortfile} %{shortfunc} %{message}",
//"%{%{}}",
testData := []struct {
format string
parts []part
err string
}{
{testFormat[0], []part{{fmtVerbMessage, "%s"}}, ""},
{testFormat[1], []part{
{fmtVerbStatic, "["},
{fmtVerbLevel, "%s"},
{fmtVerbStatic, "] "},
{fmtVerbCallpath, "3"},
{fmtVerbStatic, " ==="},
}, ""},
{testFormat[3], nil, "invalid log format"},
{testFormat[4], nil, "unknown variable"},
}
for _, test := range testData {
formatter, err := NewStringFormatter(test.format)
if test.err != "" {
if !strings.Contains(err.Error(), test.err) {
t.Fatalf("New fail. %v %v", err, test.err)
}
continue
} else if err != nil {
t.Fatalf("New fail. %v", err)
}
f := formatter.(*stringFormatter)
if len(f.parts) != len(test.parts) {
t.Fatalf("%s fatal. len %d != %d", test.format, len(f.parts), len(test.parts))
}
for i := 0; i < len(f.parts); i++ {
if f.parts[i].verb != test.parts[i].verb {
t.Fatalf("%s verb fatal. %#v", test.format, f.parts[i].verb)
}
if f.parts[i].layout != test.parts[i].layout {
t.Fatalf("%s layout fatal. %#v", test.format, f.parts[i].layout)
}
}
}
}
func TestParallel(t *testing.T) {
testfunc := func(t *testing.T) {
t.Parallel()
result := test(formater, testRecord[0])
if strings.Compare(string(result), "04-27T19:22:01.407312 DEBUG format_test.go:31 test6 Hello,world!\n") != 0 {
t.Fatalf("failt: %s", result)
}
}
t.Run("group", func(t *testing.T) {
t.Run("Test1", testfunc)
t.Run("Test2", testfunc)
t.Run("Test3", testfunc)
t.Run("Test4", testfunc)
})
}
func BenchmarkParallel(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = test(formater, testRecord[0])
}
})
}
func BenchmarkFormatStringsBuilder(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
var output strings.Builder
formater.Format(0, testRecord[0], &output)
output.String()
}
}
func BenchmarkFormatByetsBuffer(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
var output bytes.Buffer
formater.Format(0, testRecord[0], &output)
output.String()
}
}
var (
builder_pool = sync.Pool{
New: func() interface{} {
return &strings.Builder{}
},
}
buffer_pool_ = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
)
func BenchmarkFormatStringsBuilderPoolNoDefer(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
output := builder_pool.Get().(*strings.Builder)
output.Reset()
formater.Format(0, testRecord[0], output)
output.String()
builder_pool.Put(output)
}
}
func BenchmarkFormatByetsBufferPoolNoDefer(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
output := buffer_pool_.Get().(*bytes.Buffer)
output.Reset()
formater.Format(0, testRecord[0], output)
output.String()
buffer_pool_.Put(output)
}
}
func BenchmarkFormatStringsBuilderPool(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
output := builder_pool.Get().(*strings.Builder)
defer builder_pool.Put(output)
output.Reset()
formater.Format(0, testRecord[0], output)
output.String()
}
}
func BenchmarkFormatByetsBufferPool(b *testing.B) {
//formater := MustStringFormatter(testFormat[2])
b.ResetTimer()
for i := 0; i < b.N; i++ {
output := buffer_pool_.Get().(*bytes.Buffer)
defer buffer_pool_.Put(output)
output.Reset()
formater.Format(0, testRecord[0], output)
output.String()
}
}