2020-12-03 10:20:28 +00:00
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2021-01-25 00:53:24 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-12-03 10:20:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestExpandChildCollectors(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expectedOutput []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
input: "testing1,testing2,testing3",
|
|
|
|
expectedOutput: []string{"testing1", "testing2", "testing3"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "duplicate",
|
|
|
|
input: "testing1,testing2,testing2,testing3",
|
|
|
|
expectedOutput: []string{"testing1", "testing2", "testing3"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
output := expandEnabledChildCollectors(c.input)
|
|
|
|
if !reflect.DeepEqual(output, c.expectedOutput) {
|
|
|
|
t.Errorf("Output mismatch, expected %+v, got %+v", c.expectedOutput, output)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 00:53:24 +00:00
|
|
|
|
|
|
|
func benchmarkCollector(b *testing.B, name string, collectFunc func() (Collector, error)) {
|
|
|
|
// Create perflib scrape context. Some perflib collectors required a correct context,
|
|
|
|
// or will fail during benchmark.
|
|
|
|
scrapeContext, err := PrepareScrapeContext([]string{name})
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
c, err := collectFunc()
|
|
|
|
if err != nil {
|
|
|
|
b.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics := make(chan prometheus.Metric)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-metrics
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2021-12-24 10:19:05 +00:00
|
|
|
c.Collect(scrapeContext, metrics) //nolint:errcheck
|
2021-01-25 00:53:24 +00:00
|
|
|
}
|
|
|
|
}
|