-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate_test.go
More file actions
187 lines (179 loc) · 4.68 KB
/
Copy pathrate_test.go
File metadata and controls
187 lines (179 loc) · 4.68 KB
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
package main
import (
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
func fapprox(a, b float64) bool {
d := a - b
if d < 0 {
d = -d
}
return d < 1e-9
}
func TestComputeEnergyWatts(t *testing.T) {
t0 := time.Unix(1_000_000, 0)
const maxRange = 262143328850 // realistic max_energy_range_uj
tests := []struct {
name string
prev energySample
cur energySample
max uint64
wantNil bool
wantWatt float64
}{
{
name: "steady 15W over 1s",
prev: energySample{uj: 1_000_000, ts: t0},
cur: energySample{uj: 1_000_000 + 15_000_000, ts: t0.Add(time.Second)},
max: maxRange,
wantWatt: 15,
},
{
name: "interval too short -> nil",
prev: energySample{uj: 1_000_000, ts: t0},
cur: energySample{uj: 2_000_000, ts: t0.Add(50 * time.Millisecond)},
max: maxRange,
wantNil: true,
},
{
name: "zero prev (unseeded) -> nil",
prev: energySample{},
cur: energySample{uj: 2_000_000, ts: t0.Add(time.Second)},
max: maxRange,
wantNil: true,
},
{
name: "rollover with valid max",
prev: energySample{uj: maxRange - 1_000_000, ts: t0},
cur: energySample{uj: 2_000_000, ts: t0.Add(time.Second)},
max: maxRange,
wantWatt: 3, // (max - prev + cur) = 1e6 + 2e6 = 3e6 uJ over 1s
},
{
name: "backwards counter, max unknown (0) -> nil",
prev: energySample{uj: 2_000_000, ts: t0},
cur: energySample{uj: 500_000, ts: t0.Add(time.Second)},
max: 0,
wantNil: true,
},
{
name: "backwards counter, implausible max<=prev -> nil",
prev: energySample{uj: 2_000_000, ts: t0},
cur: energySample{uj: 500_000, ts: t0.Add(time.Second)},
max: 1_000_000,
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := computeEnergyWatts(tt.prev, tt.cur, tt.max)
if tt.wantNil {
if got != nil {
t.Fatalf("expected nil, got %v", *got)
}
return
}
if got == nil {
t.Fatalf("expected %v W, got nil", tt.wantWatt)
}
if !fapprox(*got, tt.wantWatt) {
t.Errorf("got %v W, want %v W", *got, tt.wantWatt)
}
})
}
}
func TestComputeNetRates(t *testing.T) {
t0 := time.Unix(1_000_000, 0)
tests := []struct {
name string
prev, cur netSample
wantNil bool
wantRx, wantTx float64
}{
{
name: "2000 rx / 1000 tx over 2s",
prev: netSample{rx: 1000, tx: 500, ts: t0},
cur: netSample{rx: 5000, tx: 2500, ts: t0.Add(2 * time.Second)},
wantRx: 2000,
wantTx: 1000,
},
{
name: "interval too short -> nil",
prev: netSample{rx: 1000, tx: 500, ts: t0},
cur: netSample{rx: 5000, tx: 2500, ts: t0.Add(50 * time.Millisecond)},
wantNil: true,
},
{
name: "rx counter reset (backwards) -> nil",
prev: netSample{rx: 5000, tx: 500, ts: t0},
cur: netSample{rx: 1000, tx: 2500, ts: t0.Add(time.Second)},
wantNil: true,
},
{
name: "zero prev (unseeded) -> nil",
prev: netSample{},
cur: netSample{rx: 5000, tx: 2500, ts: t0.Add(time.Second)},
wantNil: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := computeNetRates(tt.prev, tt.cur)
if tt.wantNil {
if got.rxBytesPerSec != nil || got.txBytesPerSec != nil {
t.Fatalf("expected null rates, got rx=%v tx=%v", got.rxBytesPerSec, got.txBytesPerSec)
}
return
}
if got.rxBytesPerSec == nil || got.txBytesPerSec == nil {
t.Fatalf("expected rates, got nil")
}
if !fapprox(*got.rxBytesPerSec, tt.wantRx) || !fapprox(*got.txBytesPerSec, tt.wantTx) {
t.Errorf("got rx=%v tx=%v, want rx=%v tx=%v",
*got.rxBytesPerSec, *got.txBytesPerSec, tt.wantRx, tt.wantTx)
}
})
}
}
// TestMetricsHandlerConcurrentSnapshot exercises the cache under concurrent
// writes (sampler) and reads (handlers). Run with -race to catch any
// unsynchronized access to `latest`.
func TestMetricsHandlerConcurrentSnapshot(t *testing.T) {
storeMetrics(Metrics{}) // ensure a valid snapshot exists so readers get 200
stop := make(chan struct{})
var writer sync.WaitGroup
writer.Add(1)
go func() {
defer writer.Done()
for {
select {
case <-stop:
return
default:
var m Metrics
v := 1.5
m.CPU.UsagePercent = &v // fresh pointer each store, as in production
storeMetrics(m)
}
}
}()
var readers sync.WaitGroup
for i := 0; i < 50; i++ {
readers.Add(1)
go func() {
defer readers.Done()
req := httptest.NewRequest(http.MethodGet, "/api/metrics", nil)
rec := httptest.NewRecorder()
metricsHandler(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("status = %d, want 200", rec.Code)
}
}()
}
readers.Wait()
close(stop)
writer.Wait()
}