2021-07-04 01:47:04 -07:00
|
|
|
// Copyright 2021 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.
|
|
|
|
|
2021-10-03 04:35:24 -07:00
|
|
|
//go:build !nocpu
|
2021-07-04 01:47:04 -07:00
|
|
|
// +build !nocpu
|
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2024-09-11 01:51:28 -07:00
|
|
|
"io"
|
|
|
|
"log/slog"
|
2021-07-04 01:47:04 -07:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prometheus/procfs"
|
|
|
|
)
|
|
|
|
|
2022-03-14 21:14:27 -07:00
|
|
|
func copyStats(d, s map[int64]procfs.CPUStat) {
|
|
|
|
for k := range s {
|
|
|
|
v := s[k]
|
|
|
|
d[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestCPUCollector(s map[int64]procfs.CPUStat) *cpuCollector {
|
|
|
|
dup := make(map[int64]procfs.CPUStat, len(s))
|
|
|
|
copyStats(dup, s)
|
2021-07-04 01:47:04 -07:00
|
|
|
return &cpuCollector{
|
2024-09-11 01:51:28 -07:00
|
|
|
logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
|
2021-07-04 01:47:04 -07:00
|
|
|
cpuStats: dup,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCPU(t *testing.T) {
|
2022-03-14 21:14:27 -07:00
|
|
|
firstCPUStat := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 100.0,
|
|
|
|
Nice: 100.0,
|
|
|
|
System: 100.0,
|
|
|
|
Idle: 100.0,
|
|
|
|
Iowait: 100.0,
|
|
|
|
IRQ: 100.0,
|
|
|
|
SoftIRQ: 100.0,
|
|
|
|
Steal: 100.0,
|
|
|
|
Guest: 100.0,
|
|
|
|
GuestNice: 100.0,
|
|
|
|
}}
|
2021-07-04 01:47:04 -07:00
|
|
|
|
|
|
|
c := makeTestCPUCollector(firstCPUStat)
|
2022-03-14 21:14:27 -07:00
|
|
|
want := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 101.0,
|
|
|
|
Nice: 101.0,
|
|
|
|
System: 101.0,
|
|
|
|
Idle: 101.0,
|
|
|
|
Iowait: 101.0,
|
|
|
|
IRQ: 101.0,
|
|
|
|
SoftIRQ: 101.0,
|
|
|
|
Steal: 101.0,
|
|
|
|
Guest: 101.0,
|
|
|
|
GuestNice: 101.0,
|
|
|
|
}}
|
2021-07-04 01:47:04 -07:00
|
|
|
c.updateCPUStats(want)
|
|
|
|
got := c.cpuStats
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Fatalf("should have %v CPU Stat: got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = makeTestCPUCollector(firstCPUStat)
|
2022-03-14 21:14:27 -07:00
|
|
|
jumpBack := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 99.9,
|
|
|
|
Nice: 99.9,
|
|
|
|
System: 99.9,
|
|
|
|
Idle: 99.9,
|
|
|
|
Iowait: 99.9,
|
|
|
|
IRQ: 99.9,
|
|
|
|
SoftIRQ: 99.9,
|
|
|
|
Steal: 99.9,
|
|
|
|
Guest: 99.9,
|
|
|
|
GuestNice: 99.9,
|
|
|
|
}}
|
2021-07-04 01:47:04 -07:00
|
|
|
c.updateCPUStats(jumpBack)
|
|
|
|
got = c.cpuStats
|
|
|
|
if reflect.DeepEqual(jumpBack, got) {
|
|
|
|
t.Fatalf("should have %v CPU Stat: got %v", firstCPUStat, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
c = makeTestCPUCollector(firstCPUStat)
|
2022-03-14 21:14:27 -07:00
|
|
|
resetIdle := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 102.0,
|
|
|
|
Nice: 102.0,
|
|
|
|
System: 102.0,
|
|
|
|
Idle: 1.0,
|
|
|
|
Iowait: 102.0,
|
|
|
|
IRQ: 102.0,
|
|
|
|
SoftIRQ: 102.0,
|
|
|
|
Steal: 102.0,
|
|
|
|
Guest: 102.0,
|
|
|
|
GuestNice: 102.0,
|
|
|
|
}}
|
2021-07-04 01:47:04 -07:00
|
|
|
c.updateCPUStats(resetIdle)
|
|
|
|
got = c.cpuStats
|
|
|
|
if !reflect.DeepEqual(resetIdle, got) {
|
|
|
|
t.Fatalf("should have %v CPU Stat: got %v", resetIdle, got)
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 09:47:40 -08:00
|
|
|
|
|
|
|
func TestCPUOffline(t *testing.T) {
|
|
|
|
// CPU 1 goes offline.
|
|
|
|
firstCPUStat := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 100.0,
|
|
|
|
Nice: 100.0,
|
|
|
|
System: 100.0,
|
|
|
|
Idle: 100.0,
|
|
|
|
Iowait: 100.0,
|
|
|
|
IRQ: 100.0,
|
|
|
|
SoftIRQ: 100.0,
|
|
|
|
Steal: 100.0,
|
|
|
|
Guest: 100.0,
|
|
|
|
GuestNice: 100.0,
|
|
|
|
},
|
|
|
|
1: {
|
|
|
|
User: 101.0,
|
|
|
|
Nice: 101.0,
|
|
|
|
System: 101.0,
|
|
|
|
Idle: 101.0,
|
|
|
|
Iowait: 101.0,
|
|
|
|
IRQ: 101.0,
|
|
|
|
SoftIRQ: 101.0,
|
|
|
|
Steal: 101.0,
|
|
|
|
Guest: 101.0,
|
|
|
|
GuestNice: 101.0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
c := makeTestCPUCollector(firstCPUStat)
|
|
|
|
want := map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 100.0,
|
|
|
|
Nice: 100.0,
|
|
|
|
System: 100.0,
|
|
|
|
Idle: 100.0,
|
|
|
|
Iowait: 100.0,
|
|
|
|
IRQ: 100.0,
|
|
|
|
SoftIRQ: 100.0,
|
|
|
|
Steal: 100.0,
|
|
|
|
Guest: 100.0,
|
|
|
|
GuestNice: 100.0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.updateCPUStats(want)
|
|
|
|
got := c.cpuStats
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Fatalf("should have %v CPU Stat: got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CPU 1 comes back online.
|
|
|
|
want = map[int64]procfs.CPUStat{
|
|
|
|
0: {
|
|
|
|
User: 100.0,
|
|
|
|
Nice: 100.0,
|
|
|
|
System: 100.0,
|
|
|
|
Idle: 100.0,
|
|
|
|
Iowait: 100.0,
|
|
|
|
IRQ: 100.0,
|
|
|
|
SoftIRQ: 100.0,
|
|
|
|
Steal: 100.0,
|
|
|
|
Guest: 100.0,
|
|
|
|
GuestNice: 100.0,
|
|
|
|
},
|
|
|
|
1: {
|
|
|
|
User: 101.0,
|
|
|
|
Nice: 101.0,
|
|
|
|
System: 101.0,
|
|
|
|
Idle: 101.0,
|
|
|
|
Iowait: 101.0,
|
|
|
|
IRQ: 101.0,
|
|
|
|
SoftIRQ: 101.0,
|
|
|
|
Steal: 101.0,
|
|
|
|
Guest: 101.0,
|
|
|
|
GuestNice: 101.0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c.updateCPUStats(want)
|
|
|
|
got = c.cpuStats
|
|
|
|
if !reflect.DeepEqual(want, got) {
|
|
|
|
t.Fatalf("should have %v CPU Stat: got %v", want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|