-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog_test.go
92 lines (76 loc) · 1.83 KB
/
log_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
package asyncLog
import (
"testing"
"time"
)
func TestNewLogFile(t *testing.T) {
n := len(asyncLog.files)
lf1 := NewLogFile("/tmp/test1.log")
if len(asyncLog.files) != n+1 {
t.Fatalf("NewLogFile num != 1")
}
_ = NewLogFile("/tmp/test1.log")
if len(asyncLog.files) != n+1 {
t.Fatalf("NewLogFile num != 1 repeat")
}
lf2 := NewLogFile("/tmp/test2.log")
if len(asyncLog.files) != n+2 {
t.Fatalf("NewLogFile num != 2")
}
lf2.SetRotate(RotateDate)
_ = lf1.Write("lf1: hello world1")
_ = lf1.Write("lf1: hello world2")
_ = lf2.Write("lf2: hello world1")
_ = lf2.Write("lf2: hello world2")
_ = lf1.Write("lf1: hello world3")
_ = lf1.Write("lf1: hello world4")
time.Sleep(time.Second * 2)
_ = lf1.Write("lf1: ---hello world1")
_ = lf1.Write("lf1: ---hello world2")
_ = lf2.Write("lf2: ---hello world1")
_ = lf2.Write("lf2: ---hello world2")
_ = lf1.Write("lf1: ---hello world3")
_ = lf1.Write("lf1: ---hello world4")
time.Sleep(time.Second * 2)
}
func TestJson(t *testing.T) {
lf1 := NewLogFile("/tmp/test-json.log")
lf1.SetFlags(NoFlag)
lf1.SetUseCache(false)
var hello = struct {
Hello string
World int
}{
Hello: "test",
World: 12,
}
_ = lf1.WriteJson(hello)
_ = lf1.WriteJson(hello)
_ = lf1.WriteJson(hello)
time.Sleep(time.Second * 2)
}
func TestProbability(t *testing.T) {
lf := NewLogFile("/tmp/test-probability.log")
lf.SetProbability(0.5)
for i := 0; i < 20; i++ {
lf.Write("probability")
}
time.Sleep(time.Second)
}
func BenchmarkWrite(b *testing.B) {
lf := NewLogFile("/tmp/bench-test.log")
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = lf.Write("hello world")
}
})
}
func BenchmarkWriteNoCache(b *testing.B) {
lf := NewLogFile("/tmp/bench-nocache-test.log")
lf.SetUseCache(false)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = lf.Write("hello world")
}
})
}