-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobserver_test.go
121 lines (93 loc) · 2.75 KB
/
observer_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
package logz_test
import (
"sort"
"strconv"
"sync"
"testing"
"github.com/bool64/logz"
"github.com/stretchr/testify/assert"
)
func TestObserver_ObserveMessage(t *testing.T) {
o := logz.NewObserver(logz.Config{})
o.ObserveMessage("test", 123)
o.ObserveMessage("test", 456)
o.ObserveMessage("another test", 789)
entries := o.GetEntries()
// Order of entries may be random as they are retrieved from a map.
sort.Slice(entries, func(i, j int) bool {
return entries[i].Message > entries[j].Message
})
assert.Equal(t, "test", entries[0].Message)
assert.Equal(t, uint64(2), entries[0].Count)
assert.NotEmpty(t, entries[0].First)
assert.NotEmpty(t, entries[0].Last)
assert.Equal(t, "another test", entries[1].Message)
assert.Equal(t, uint64(1), entries[1].Count)
assert.NotEmpty(t, entries[1].First)
assert.NotEmpty(t, entries[1].Last)
entry := o.Find("test")
assert.Equal(t, "test", entry.Message)
assert.Equal(t, uint64(2), entry.Count)
assert.NotEmpty(t, entry.First)
assert.NotEmpty(t, entry.Last)
assert.NotEmpty(t, entry.Samples)
}
func TestObserver_ObserveMessage_filter(t *testing.T) {
o := logz.NewObserver(logz.Config{FilterMessage: true})
o.ObserveMessage("test foo123", 123)
o.ObserveMessage("test bar456", 456)
o.ObserveMessage("another test", 789)
entries := o.GetEntries()
// Order of entries may be random as they are retrieved from a map.
sort.Slice(entries, func(i, j int) bool {
return entries[i].Message > entries[j].Message
})
assert.Equal(t, "test X", entries[0].Message)
assert.Equal(t, uint64(2), entries[0].Count)
assert.NotEmpty(t, entries[0].First)
assert.NotEmpty(t, entries[0].Last)
assert.Equal(t, "another test", entries[1].Message)
assert.Equal(t, uint64(1), entries[1].Count)
assert.NotEmpty(t, entries[1].First)
assert.NotEmpty(t, entries[1].Last)
entry := o.Find("test X")
assert.Equal(t, "test X", entry.Message)
assert.Equal(t, uint64(2), entry.Count)
assert.NotEmpty(t, entry.First)
assert.NotEmpty(t, entry.Last)
assert.NotEmpty(t, entry.Samples)
}
func BenchmarkObserver_ObserveMessage(b *testing.B) {
o := logz.NewObserver(logz.Config{})
wg := sync.WaitGroup{}
concurrency := 50
b.ReportAllocs()
for i := 0; i < concurrency; i++ {
msg := "message" + strconv.Itoa(i)
wg.Add(1)
go func() {
for i := 0; i < b.N/concurrency; i++ {
o.ObserveMessage(msg, i)
}
wg.Done()
}()
}
wg.Wait()
}
func BenchmarkObserver_ObserveMessage_filter(b *testing.B) {
o := logz.NewObserver(logz.Config{FilterMessage: true})
wg := sync.WaitGroup{}
concurrency := 50
b.ReportAllocs()
for i := 0; i < concurrency; i++ {
msg := "foo bar message" + strconv.Itoa(i)
wg.Add(1)
go func() {
for i := 0; i < b.N/concurrency; i++ {
o.ObserveMessage(msg, i)
}
wg.Done()
}()
}
wg.Wait()
}