2019-05-07 13:21:41 +02:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// +build !noprocesses
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2019-12-31 17:19:37 +01:00
|
|
|
"github.com/go-kit/kit/log"
|
2019-05-07 13:21:41 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPerfCollector(t *testing.T) {
|
|
|
|
paranoidBytes, err := ioutil.ReadFile("/proc/sys/kernel/perf_event_paranoid")
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("Procfs not mounted, skipping perf tests")
|
|
|
|
}
|
|
|
|
paranoidStr := strings.Replace(string(paranoidBytes), "\n", "", -1)
|
|
|
|
paranoid, err := strconv.Atoi(paranoidStr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Expected perf_event_paranoid to be an int, got: %s", paranoidStr)
|
|
|
|
}
|
|
|
|
if paranoid >= 1 {
|
|
|
|
t.Skip("Skipping perf tests, set perf_event_paranoid to 0")
|
|
|
|
}
|
2019-12-31 17:19:37 +01:00
|
|
|
collector, err := NewPerfCollector(log.NewNopLogger())
|
2019-05-07 13:21:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup background goroutine to capture metrics.
|
|
|
|
metrics := make(chan prometheus.Metric)
|
|
|
|
defer close(metrics)
|
|
|
|
go func() {
|
|
|
|
for range metrics {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if err := collector.Update(metrics); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:36:33 +01:00
|
|
|
|
|
|
|
func TestPerfCPUFlagToCPUs(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
flag string
|
|
|
|
exCpus []int
|
|
|
|
errStr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid single cpu",
|
|
|
|
flag: "1",
|
|
|
|
exCpus: []int{1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid range cpus",
|
|
|
|
flag: "1-5",
|
|
|
|
exCpus: []int{1, 2, 3, 4, 5},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid double digit",
|
|
|
|
flag: "10",
|
|
|
|
exCpus: []int{10},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid double digit range",
|
|
|
|
flag: "10-12",
|
|
|
|
exCpus: []int{10, 11, 12},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid double digit stride",
|
|
|
|
flag: "10-20:5",
|
|
|
|
exCpus: []int{10, 15, 20},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
cpus, err := perfCPUFlagToCPUs(test.flag)
|
|
|
|
if test.errStr != "" {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected error to not be nil")
|
|
|
|
}
|
|
|
|
if test.errStr != err.Error() {
|
|
|
|
t.Fatalf(
|
|
|
|
"expected error %q, got %q",
|
|
|
|
test.errStr,
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(cpus) != len(test.exCpus) {
|
|
|
|
t.Fatalf(
|
|
|
|
"expected cpus %v, got %v",
|
|
|
|
test.exCpus,
|
|
|
|
cpus,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for i := range cpus {
|
|
|
|
if test.exCpus[i] != cpus[i] {
|
|
|
|
t.Fatalf(
|
|
|
|
"expected cpus %v, got %v",
|
|
|
|
test.exCpus,
|
|
|
|
cpus,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|