-
Notifications
You must be signed in to change notification settings - Fork 46
/
async_test.go
142 lines (124 loc) · 2.75 KB
/
async_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
package log
import (
"io"
"os"
"testing"
)
func TestAsyncWriterZero(t *testing.T) {
w := &AsyncWriter{
ChannelSize: 0,
Writer: IOWriter{os.Stderr},
}
for i := 0; i < 10; i++ {
_, _ = wlprintf(w, InfoLevel, "%s, %d during async writer 1k buff size\n", timeNow(), i)
}
if err := w.Close(); err != nil {
t.Errorf("async close error: %+v", err)
}
}
func TestAsyncWriterSmall(t *testing.T) {
w := &AsyncWriter{
ChannelSize: 5,
Writer: IOWriter{os.Stderr},
}
for i := 0; i < 10; i++ {
_, _ = wlprintf(w, InfoLevel, "%s, %d during async writer 1k buff size\n", timeNow(), i)
}
if err := w.Close(); err != nil {
t.Errorf("async close error: %+v", err)
}
}
func TestAsyncWriterSize(t *testing.T) {
writer1 := &FileWriter{
Filename: "async_file_test1.log",
}
writer2 := &AsyncWriter{
ChannelSize: 4096,
DisableWritev: false,
DiscardOnFull: false,
Writer: &FileWriter{
Filename: "async_file_test2.log",
},
}
logger := Logger{
Writer: &MultiEntryWriter{
writer1,
writer2,
},
}
for i := 0; i < 100000; i++ {
logger.Info().Msg("hello file writer")
}
if err := writer1.Close(); err != nil {
t.Errorf("file writer close error: %+v", err)
}
if err := writer2.Close(); err != nil {
t.Errorf("async file writer close error: %+v", err)
}
fi1, err := os.Stat(writer1.Filename)
if err != nil {
t.Errorf("file writer stat error: %+v", err)
}
fi2, err := os.Stat(writer2.Writer.(*FileWriter).Filename)
if err != nil {
t.Errorf("async file writer stat error: %+v", err)
}
if fi1.Size() != fi2.Size() {
t.Errorf("filesize not equal: %v != %v", fi1.Size(), fi2.Size())
}
}
func BenchmarkSyncFileWriter(b *testing.B) {
logger := Logger{
Writer: &FileWriter{
Filename: "sync_file_test.log",
},
}
defer logger.Writer.(io.Closer).Close()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(b *testing.PB) {
for b.Next() {
logger.Info().Msg("hello file writer")
}
})
}
func BenchmarkAsyncFileWriter(b *testing.B) {
logger := Logger{
Writer: &AsyncWriter{
ChannelSize: 4096,
DisableWritev: false,
DiscardOnFull: false,
Writer: &FileWriter{
Filename: "async_file_test2.log",
},
},
}
defer logger.Writer.(io.Closer).Close()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(b *testing.PB) {
for b.Next() {
logger.Info().Msg("hello file writer")
}
})
}
func BenchmarkAsyncIOWriter(b *testing.B) {
logger := TSVLogger{
Writer: &AsyncWriter{
ChannelSize: 4096,
DisableWritev: false,
DiscardOnFull: false,
Writer: &FileWriter{
Filename: "async_file_test3.log",
},
},
}
defer logger.Writer.(io.Closer).Close()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(b *testing.PB) {
for b.Next() {
logger.New().Str("hello file writer").Msg()
}
})
}