/
cputime_test.go
191 lines (163 loc) 路 5.85 KB
/
cputime_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
// Copyright The OpenTelemetry 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.
package cputime
import (
"context"
"fmt"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)
func getMetric(metrics []metricdata.Metrics, name string, lbl attribute.KeyValue) float64 {
for _, m := range metrics {
fmt.Println(m.Name)
if m.Name != name {
continue
}
switch dt := m.Data.(type) {
case metricdata.Gauge[int64]:
if !lbl.Valid() {
return float64(dt.DataPoints[0].Value)
}
for _, p := range dt.DataPoints {
if val, ok := p.Attributes.Value(lbl.Key); ok && val.Emit() == lbl.Value.Emit() {
return float64(p.Value)
}
}
case metricdata.Gauge[float64]:
if !lbl.Valid() {
return dt.DataPoints[0].Value
}
for _, p := range dt.DataPoints {
if val, ok := p.Attributes.Value(lbl.Key); ok && val.Emit() == lbl.Value.Emit() {
return p.Value
}
}
case metricdata.Sum[int64]:
if !lbl.Valid() {
return float64(dt.DataPoints[0].Value)
}
for _, p := range dt.DataPoints {
if val, ok := p.Attributes.Value(lbl.Key); ok && val.Emit() == lbl.Value.Emit() {
return float64(p.Value)
}
}
case metricdata.Sum[float64]:
if !lbl.Valid() {
return dt.DataPoints[0].Value
}
for _, p := range dt.DataPoints {
if val, ok := p.Attributes.Value(lbl.Key); ok && val.Emit() == lbl.Value.Emit() {
return p.Value
}
}
default:
panic(fmt.Sprintf("invalid aggregation type: %v", dt))
}
}
panic(fmt.Sprintf("Could not locate a metric in test output, name: %s, keyValue: %v", name, lbl))
}
func TestProcessCPU(t *testing.T) {
ctx := context.Background()
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
err := Start(WithMeterProvider(provider))
require.NoError(t, err)
// This is a second copy of the same source of information.
// We ultimately have to trust the information source, the
// test here is to be sure the information is correctly
// translated into metrics.
c, err := newCputime(config{
MeterProvider: provider,
})
require.NoError(t, err)
start := time.Now()
for time.Since(start) < time.Second {
// This has a mix of user and system time, so serves
// the purpose of advancing both process and host,
// user and system CPU usage.
_, _, _, _ = c.getProcessTimes(ctx)
}
beforeUser, beforeSystem, _, _ := c.getProcessTimes(ctx)
data, err := reader.Collect(ctx)
require.NoError(t, err)
require.Equal(t, 1, len(data.ScopeMetrics))
processUser := getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeUser[0])
processSystem := getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeSystem[0])
afterUser, afterSystem, _, _ := c.getProcessTimes(ctx)
// Validate process times:
// User times are in range
require.LessOrEqual(t, beforeUser, processUser)
require.GreaterOrEqual(t, afterUser, processUser)
// System times are in range
require.LessOrEqual(t, beforeSystem, processSystem)
require.GreaterOrEqual(t, afterSystem, processSystem)
}
func TestProcessUptime(t *testing.T) {
ctx := context.Background()
y2k, err := time.Parse(time.RFC3339, "2000-01-01T00:00:00Z")
require.NoError(t, err)
expectUptime := time.Since(y2k).Seconds()
var save time.Time
processStartTime, save = y2k, processStartTime
defer func() {
processStartTime = save
}()
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
c, err := newCputime(config{MeterProvider: provider})
require.NoError(t, err)
require.NoError(t, c.register())
data, err := reader.Collect(ctx)
require.NoError(t, err)
require.Equal(t, 1, len(data.ScopeMetrics))
procUptime := getMetric(data.ScopeMetrics[0].Metrics, "process.uptime", attribute.KeyValue{})
require.LessOrEqual(t, expectUptime, procUptime)
}
func TestProcessGCCPUTime(t *testing.T) {
ctx := context.Background()
reader := metric.NewManualReader()
provider := metric.NewMeterProvider(metric.WithReader(reader))
c, err := newCputime(config{
MeterProvider: provider,
})
require.NoError(t, err)
require.NoError(t, c.register())
data, err := reader.Collect(ctx)
require.NoError(t, err)
require.Equal(t, 1, len(data.ScopeMetrics))
initialUtime := getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeUser[0])
initialStime := getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeSystem[0])
initialGCtime := getMetric(data.ScopeMetrics[0].Metrics, "process.runtime.go.gc.cpu.time", attribute.KeyValue{})
// Make garbage
for i := 0; i < 2; i++ {
var garbage []struct{}
for start := time.Now(); time.Since(start) < time.Second/16; {
garbage = append(garbage, struct{}{})
}
require.Less(t, 0, len(garbage))
runtime.GC()
data, err := reader.Collect(ctx)
require.NoError(t, err)
require.Equal(t, 1, len(data.ScopeMetrics))
utime := -initialUtime + getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeUser[0])
stime := -initialStime + getMetric(data.ScopeMetrics[0].Metrics, "process.cpu.time", AttributeCPUTimeSystem[0])
gctime := -initialGCtime + getMetric(data.ScopeMetrics[0].Metrics, "process.runtime.go.gc.cpu.time", attribute.KeyValue{})
require.LessOrEqual(t, gctime, utime+stime)
}
}