-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_test.go
290 lines (260 loc) · 7.74 KB
/
process_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
package jqtop
import (
"reflect"
"strconv"
"testing"
"github.com/hpcloud/tail"
metrics "github.com/rcrowley/go-metrics"
"github.com/tidwall/gjson"
)
// clearCounters helps us to clear counters
// in tests
func clearCounters() {
// Clear counters
for _, r := range countersSlice.counters {
r.Each(func(name string, i interface{}) {
c := i.(*metrics.StandardCounter)
c.Clear()
})
}
}
func TestIsMatching(t *testing.T) {
var testCases = []struct {
json string
f *filter
expected bool
}{
{
`{"foo": "i am awesome"}`,
&filter{Function: "contains", Args: []string{"foo", "awesome"}},
true,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "contains", Args: []string{"foo", "awesome"}},
true,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "equals", Args: []string{"foo", "awesome"}},
false,
},
{
`{"foo": "i am awesome"}`,
&filter{Negate: false, Function: "contains", Args: []string{"foo", "awesome"}},
true,
},
}
r := isMatching(`{"foo": "i am awesome"}`, []filter{}, &Fields{})
if !r {
t.Errorf("No filters provided but line was filtered")
}
for i, tt := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r := isMatching(tt.json, []filter{*tt.f}, &Fields{})
if r != tt.expected {
t.Errorf("Unexpected return from isMatchFilter. Input: %+v", tt)
}
})
}
}
func TestIsMatchFilter(t *testing.T) {
var testCases = []struct {
json string
f *filter
expected bool
}{
{
`{"foo": "i am awesome"}`,
&filter{Function: "contains", Args: []string{"foo", "awesome"}},
true,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "contains", Args: []string{"not_found", "awesome"}},
false,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "contains", Args: []string{"foo", "awesomeness"}},
false,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "regex", Args: []string{"foo", "awe.*"}},
true,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "regex", Args: []string{"foo", "*.*"}},
false,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "regex", Args: []string{"foo", "AWE.*"}},
false,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "equals", Args: []string{"foo", "i am awesome"}},
true,
},
{
`{"foo": "i am awesome"}`,
&filter{Function: "equals_invalid_spelling", Args: []string{"foo", "i am awesome"}},
false,
},
}
for i, tt := range testCases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r := isMatchFilter(tt.json, *tt.f, &Fields{})
if r != tt.expected {
t.Errorf("Unexpected return from isMatchFilter. Input: %+v", tt)
}
})
}
}
func TestProcessLine(t *testing.T) {
Config.Interval = 1
clearCounters()
line := []string{
`{"cqtn": "23/Sep/2018:02:34:25 -0000", "cqhm": "GET", "cquuc": "http://coolhost.com:1234/admin", "cqhv": "HTTP/1.1", "pssc": "200"}`,
}
simpleF := []string{"cquuc", "pssc", "cqhm", "cqhv"}
derivedFields := make(map[string]*derivedField)
derivedFields["host_with_protocol"] = &derivedField{
NewField: "host_with_protocol",
Fname: "regex_capture",
Args: []string{"cquuc", "(.*?://.*?)/"},
}
allFields := Fields{
SimpleFields: simpleF,
DerivedFields: derivedFields,
FieldsInOrder: []string{"cquuc", "pssc", "cqhm", "cqhv", "host_with_protocol"},
}
// This is initialized elsewhere, needed for test only
countersSlice.counters = initFieldCounters(allFields)
processLine(line[0], &allFields)
if len(countersSlice.counters) != 5 {
t.Errorf("processLine didn't parse all fields\n. Current counters: %+v", countersSlice.counters)
}
}
func TestProcessLines(t *testing.T) {
// Set proper args and vars
Config.Interval = 1
Config.Fields = "cquuc; host_with_protocol = regex_capture(cquuc, \"(.*?://.*?)/\");"
Config.Filters = ""
clearCounters()
linesChan := make(chan *tail.Line)
// Send lines to channel
go func() {
linesChan <- tail.NewLine(`{"cqtn": "23/Sep/2018:02:34:25 -0000", "cqhm": "GET", "cquuc": "http://coolhost.com:1234/admin", "cqhv": "HTTP/1.1", "pssc": "200"}`)
linesChan <- tail.NewLine(`{"cqtn": "23/Sep/2018:02:34:25 -0000", "cqhm": "GET", "cquuc": "http://coolhost2.com:1234/admin", "cqhv": "HTTP/1.1", "pssc": "200"}`)
close(linesChan)
}()
// Reading from channel
ProcessLines(linesChan)
if len(countersSlice.counters) != 2 {
t.Errorf("Correct numbers of counters were not updated")
}
c := countersSlice.counters[0].GetOrRegister("http://coolhost.com:1234/admin", metrics.NewCounter).(*metrics.StandardCounter)
if c.Count() != int64(1) {
t.Errorf("Wrong counter values")
}
}
func BenchmarkProcessLine(b *testing.B) {
Config.Interval = 1
line := []string{
`{"cqtn": "23/Sep/2018:02:34:25 -0000", "cqhm": "GET", "cquuc": "http://coolhost.com:1234/admin", "cqhv": "HTTP/1.1", "pssc": "200"}`,
}
simpleF := []string{"cquuc", "pssc", "cqhm", "cqhv"}
derivedFields := make(map[string]*derivedField)
derivedFields["host_with_protocol"] = &derivedField{
NewField: "host_with_protocol",
Fname: "regex_capture",
Args: []string{"cquuc", "(.*?://.*?)/"},
}
allFields := Fields{
SimpleFields: simpleF,
DerivedFields: derivedFields,
}
for n := 0; n < b.N; n++ {
processLine(line[0], &allFields)
}
}
func BenchmarkGjson(b *testing.B) {
line := `{"cqtn": "23/Sep/2018:02:34:25 -0000", "cqhm": "GET", "cquuc": "http://coolhost.com:1234/admin", "cqhv": "HTTP/1.1", "pssc": "200"}`
b.Run("Individual fetch", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = gjson.Get(line, "pssc")
}
})
b.Run("Individual fetches of multiple values", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = gjson.Get(line, "cquuc")
_ = gjson.Get(line, "pssc")
_ = gjson.Get(line, "cqhm")
_ = gjson.Get(line, "cqhv")
}
})
b.Run("GetMany: Many at once", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = gjson.GetMany(line, "cquuc", "pssc", "cqhm", "cqhv")
}
})
/*
goos: linux
goarch: amd64
PASS
benchmark iter time/iter
--------- ---- ---------
BenchmarkGjson/Individual_fetch-12 2000000 898.00 ns/op
BenchmarkGjson/Individual_fetches_of_multiple_values-12 300000 3774.00 ns/op
BenchmarkGjson/GetMany:_Many_at_once-12 500000 4789.00 ns/op
ok _/home/arastogi/repos/jtop 6.416s
*/
}
func Test_getFieldIndex(t *testing.T) {
type args struct {
allFields Fields
fName string
}
tests := []struct {
name string
args args
wantIndex int
}{
{"valid field at index 0", args{Fields{FieldsInOrder: []string{"cquuc", "domain"}}, "cquuc"}, 0},
{"valid field at index 1", args{Fields{FieldsInOrder: []string{"cquuc", "domain"}}, "domain"}, 1},
{"invalid field", args{Fields{FieldsInOrder: []string{"cquuc", "domain"}}, "non_existent"}, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotIndex := getFieldIndex(tt.args.allFields, tt.args.fName); gotIndex != tt.wantIndex {
t.Errorf("getIndexField() = %v, want %v", gotIndex, tt.wantIndex)
}
})
}
}
func Test_initFieldCounters(t *testing.T) {
type args struct {
allFields Fields
}
tests := []struct {
name string
args args
wantRegistrySliceLen int
}{
{"valid", args{Fields{FieldsInOrder: []string{"cquuc", "domain"}}}, 2},
{"no fields", args{Fields{FieldsInOrder: []string{}}}, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRegistrySlice := initFieldCounters(tt.args.allFields); !reflect.DeepEqual(gotRegistrySlice, tt.wantRegistrySliceLen) {
if len(gotRegistrySlice) != tt.wantRegistrySliceLen {
t.Errorf("initCounters() = %v, want %v", len(gotRegistrySlice), tt.wantRegistrySliceLen)
}
}
})
}
}