-
Notifications
You must be signed in to change notification settings - Fork 2
/
logwriter_test.go
385 lines (297 loc) · 6.35 KB
/
logwriter_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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package logwriter_test
import (
"bufio"
"bytes"
"github.com/regorov/logwriter"
"log"
"os"
"sync"
"testing"
"time"
)
// Replace value with smth close to your typical log item
var typicalLogItem = append(bytes.Repeat([]byte("R"), 256), '\n')
type dummy struct {
}
func (d *dummy) Write(p []byte) (int, error) {
n := len(p)
return n, nil
}
type dummyMutex struct {
mu sync.Mutex
}
var buf = make([]byte, 1024)
func (d *dummyMutex) Write(p []byte) (n int, err error) {
d.mu.Lock()
copy(buf[128:], p)
d.mu.Unlock()
return n, nil
}
// Dummy write
func BenchmarkDummyWriter(b *testing.B) {
d := &dummy{}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
d.Write(typicalLogItem)
}
})
return
}
// Dummy write with Mutex
func BenchmarkDummyWriterWithMutex(b *testing.B) {
d := &dummyMutex{mu: sync.Mutex{}}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
d.Write(typicalLogItem)
}
})
return
}
// Direct non-buffered file write
func BenchmarkFileWriteDirect(b *testing.B) {
f, err := os.OpenFile("test.log", os.O_CREATE|os.O_RDWR, 0)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := f.Write(typicalLogItem)
if err != nil {
b.Fatal(err, n)
}
}
f.Close()
return
}
// Buffered file write using bufio
func BenchmarkFileWriteBufferedByBufio(b *testing.B) {
f, err := os.OpenFile("test.log", os.O_CREATE|os.O_RDWR, 0)
if err != nil {
b.Fatal(err)
}
buf := bufio.NewWriterSize(f, 1024*1024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := buf.Write(typicalLogItem)
if err != nil {
b.Fatal(err, n)
}
if buf.Available() < 1024*1024-1024 {
buf.Flush()
}
}
buf.Flush()
f.Close()
return
}
func BenchmarkFileWriteBufferedBySlice(b *testing.B) {
f, err := os.OpenFile("test.log", os.O_CREATE|os.O_RDWR, 0)
if err != nil {
b.Fatal(err)
}
buf := make([]byte, 1024*1024)
k := 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
k += copy(buf[k:], typicalLogItem)
if k > 1024*1024-1024 {
n, err := f.Write(buf[:k])
if err != nil {
b.Fatal(err, n)
}
k = 0
}
}
n, err := f.Write(buf[:k])
if err != nil {
b.Fatal(err, n)
}
f.Close()
return
}
func BenchmarkLogWriteBuffered(b *testing.B) {
// because go test -bench runs Benchmark function a few times
if err := os.Remove("test.log"); err != nil {
if !os.IsNotExist(err) {
b.Fatal(err)
}
}
lw, err := logwriter.NewLogWriter("test",
&logwriter.Config{BufferSize: 1 * 1024 * 1024,
//BufferFlushInterval: 200 * time.Millisecond,
HotMaxSize: 4 * 1024 * 1024,
CompressColdFile: true,
ColdPath: "", Mode: logwriter.ProductionMode}, true, nil)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := lw.Write(typicalLogItem)
if err != nil {
b.Fatal(err, n)
}
}
if err := lw.Close(); err != nil {
b.Fatal(err)
}
return
}
/*
func TestLogWriter_Write(t *testing.T) {
lw, err := logwriter.NewLogWriter("writer",
&logwriter.Config{BufferSize: 1024 * 1024,
BufferFlushInterval: 1 * time.Microsecond,
HotMaxSize: 4 * 1024 * 1024,
ColdPath: "coldlog/", Mode: logwriter.ProductionMode},
false, nil)
if err != nil {
t.Fatal(err)
}
lw.Write([]byte("test1\n"))
time.Sleep(3 * time.Second)
lw.Write([]byte("test2\n"))
if err := lw.FreezeHotFile(); err != nil {
t.Fatal(err)
}
if err := lw.Close(); err != nil {
t.Fatal(err)
}
return
}
*/
// Channel making speed
/*
func BenchmarkMakeChan(b *testing.B) {
//a := make([]chan struct{}, 0)
type resp struct {
n int
err error
}
type req struct {
p []byte
out chan resp
}
w := make(chan req)
go func() {
for {
val := <-w
val.out <- resp{len(val.p), nil}
}
}()
b.ResetTimer()
buf := []byte(`logwriter2015/08/17 00:33:00 Test call 48117\n`)
for i := 0; i < b.N; i++ {
r := req{p: buf, out: make(chan resp)}
w <- r
<-r.out
}
return
}
*/
// Write into the file by standard log package using log.Output()
/*func BenchmarkStandardLogToFileUsingOutput(b *testing.B) {
f, err := os.OpenFile("standardlog-output.log", os.O_CREATE|os.O_RDWR, 0)
if err != nil {
b.Fatal(err)
}
// no prefix and date format to reduce extra work
//l := log.New(f, "", 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// l.Output(2, TypicalLogItem)
}
f.Close()
return
}*/
// Write into the file by standard log package
/*
func BenchmarkStandardLogToFileUsingPrint(b *testing.B) {
f, err := os.OpenFile("standardlog-print.log", os.O_CREATE|os.O_RDWR, 0)
if err != nil {
b.Fatal(err)
}
// no prefix and date format to reduce extra work
l := log.New(f, "", 0)
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Print(typicalLogItem)
}
f.Close()
return
}
*/
//
/*
func BenchmarkLogWrite(b *testing.B) {
lw, err := logwriter.NewLogWriter("logwriter",
&logwriter.Config{BufferSize: 2000000,
BufferFlushInterval: 200 * time.Millisecond,
ColdPath: "", Mode: logwriter.ProductionMode},
true,
nil)
if err != nil {
b.Fatal(err)
}
l := log.New(lw, "logwriter ", log.Ltime|log.Lmicroseconds)
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Print("Test call ", i)
}
if err := lw.Close(); err != nil {
b.Fatal(err)
}
return
}
*/
//
/*
func BenchmarkLogWriteParallel(b *testing.B) {
lw, err := logwriter.NewLogWriter("logwriter-par",
&logwriter.Config{BufferSize: 10 * 1024 * 1024,
HotPath: "", ColdPath: "",
Mode: logwriter.ProductionMode},
true,
nil)
if err != nil {
b.Fatal(err)
}
l := log.New(lw, "logwriter ", log.Ldate|log.Ltime|log.Lmicroseconds)
b.ResetTimer()
k := 0
b.RunParallel(func(pb *testing.PB) {
// Each goroutine has its own bytes.Buffer.
k++
i := 0
for pb.Next() {
i++
l.Println(" Test call ", k, "-", i)
}
})
if err := lw.Close(); err != nil {
b.Fatal(err)
}
return
}
*/
func ExampleNewLogWriter() {
cfg := &logwriter.Config{
BufferSize: 2 * logwriter.MB, // write buffering enabled
HotPath: "/var/log/example",
ColdPath: "/var/log/example/arch",
Mode: logwriter.ProductionMode} // write into a file only
lw, err := logwriter.NewLogWriter("mywebserver",
cfg,
true, // freeze log file if it exists
nil)
if err != nil {
panic(err)
}
l := log.New(lw, "mywebserver ", log.Ldate|log.Ltime|log.Lmicroseconds)
l.Println("Mywebserer started at ", time.Now())
if err := lw.Close(); err != nil {
panic(err)
}
}