2016-06-02 17:47:15 +00:00
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
func TestCompile(t *testing.T) {
|
|
|
|
f, err := Compile([]string{})
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, f)
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f, err = Compile([]string{"cpu"})
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, f.Match("cpu"))
|
|
|
|
assert.False(t, f.Match("cpu0"))
|
|
|
|
assert.False(t, f.Match("mem"))
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f, err = Compile([]string{"cpu*"})
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, f.Match("cpu"))
|
|
|
|
assert.True(t, f.Match("cpu0"))
|
|
|
|
assert.False(t, f.Match("mem"))
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f, err = Compile([]string{"cpu", "mem"})
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, f.Match("cpu"))
|
|
|
|
assert.False(t, f.Match("cpu0"))
|
|
|
|
assert.True(t, f.Match("mem"))
|
|
|
|
|
2016-09-05 15:16:37 +00:00
|
|
|
f, err = Compile([]string{"cpu", "mem", "net*"})
|
2016-06-02 17:47:15 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, f.Match("cpu"))
|
|
|
|
assert.False(t, f.Match("cpu0"))
|
|
|
|
assert.True(t, f.Match("mem"))
|
|
|
|
assert.True(t, f.Match("network"))
|
|
|
|
}
|
|
|
|
|
2018-12-26 21:15:13 +00:00
|
|
|
func TestIncludeExclude(t *testing.T) {
|
|
|
|
tags := []string{}
|
|
|
|
labels := []string{"best", "com_influxdata", "timeseries", "com_influxdata_telegraf", "ever"}
|
|
|
|
|
|
|
|
filter, err := NewIncludeExcludeFilter([]string{}, []string{"com_influx*"})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create include/exclude filter - %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range labels {
|
|
|
|
if filter.Match(labels[i]) {
|
|
|
|
tags = append(tags, labels[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"best", "timeseries", "ever"}, tags)
|
|
|
|
}
|
|
|
|
|
2016-06-02 17:47:15 +00:00
|
|
|
var benchbool bool
|
|
|
|
|
|
|
|
func BenchmarkFilterSingleNoGlobFalse(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"cpu"})
|
2016-06-02 17:47:15 +00:00
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("network")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFilterSingleNoGlobTrue(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"cpu"})
|
2016-06-02 17:47:15 +00:00
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("cpu")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFilter(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"cpu", "mem", "net*"})
|
2016-06-02 17:47:15 +00:00
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("network")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFilterNoGlob(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"cpu", "mem", "net"})
|
2016-06-02 17:47:15 +00:00
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("net")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFilter2(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
|
2016-06-02 17:47:15 +00:00
|
|
|
"aw", "az", "axxx", "ab", "cpu", "mem", "net*"})
|
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("network")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkFilter2NoGlob(b *testing.B) {
|
2016-09-05 15:16:37 +00:00
|
|
|
f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq",
|
2016-06-02 17:47:15 +00:00
|
|
|
"aw", "az", "axxx", "ab", "cpu", "mem", "net"})
|
|
|
|
var tmp bool
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
tmp = f.Match("net")
|
|
|
|
}
|
|
|
|
benchbool = tmp
|
|
|
|
}
|