-
Notifications
You must be signed in to change notification settings - Fork 994
/
saturation_test.go
147 lines (117 loc) · 3.86 KB
/
saturation_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package raft
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestSaturationMetric(t *testing.T) {
t.Run("without smoothing", func(t *testing.T) {
sat := newSaturationMetric([]string{"metric"}, 100*time.Millisecond)
now := sat.lastReport
sat.nowFn = func() time.Time { return now }
var reported float32
sat.reportFn = func(val float32) { reported = val }
sat.sleeping()
// First window: 50ms sleeping + 75ms working.
now = now.Add(50 * time.Millisecond)
sat.working()
now = now.Add(75 * time.Millisecond)
sat.sleeping()
// Should be 60% saturation.
require.Equal(t, float32(0.6), reported)
// Second window: 90ms sleeping + 10ms working.
now = now.Add(90 * time.Millisecond)
sat.working()
now = now.Add(10 * time.Millisecond)
sat.sleeping()
// Should be 10% saturation.
require.Equal(t, float32(0.1), reported)
// Third window: 100ms sleeping + 0ms working.
now = now.Add(100 * time.Millisecond)
sat.working()
// Should be 0% saturation.
require.Equal(t, float32(0), reported)
})
}
func TestSaturationMetric_IncorrectUsage(t *testing.T) {
t.Run("calling sleeping() consecutively", func(t *testing.T) {
sat := newSaturationMetric([]string{"metric"}, 50*time.Millisecond)
now := sat.lastReport
sat.nowFn = func() time.Time { return now }
var reported float32
sat.reportFn = func(v float32) { reported = v }
// Calling sleeping() consecutively should reset sleepBegan without recording
// a sample, such that we "lose" time rather than recording nonsense data.
//
// 0 | sleeping() |
// => Sleeping (10ms)
// +10ms | working() |
// => Working (10ms)
// +20ms | sleeping() |
// => [!] LOST [!] (10ms)
// +30ms | sleeping() |
// => Sleeping (10ms)
// +40ms | working() |
// => Working (10ms)
// +50ms | sleeping() |
//
// Total reportable time: 40ms. Saturation: 50%.
sat.sleeping()
now = now.Add(10 * time.Millisecond)
sat.working()
now = now.Add(10 * time.Millisecond)
sat.sleeping()
now = now.Add(10 * time.Millisecond)
sat.sleeping()
now = now.Add(10 * time.Millisecond)
sat.working()
now = now.Add(10 * time.Millisecond)
sat.sleeping()
require.Equal(t, float32(0.5), reported)
})
t.Run("calling working() consecutively", func(t *testing.T) {
sat := newSaturationMetric([]string{"metric"}, 30*time.Millisecond)
now := sat.lastReport
sat.nowFn = func() time.Time { return now }
var reported float32
sat.reportFn = func(v float32) { reported = v }
// Calling working() consecutively should reset workBegan without recording
// a sample, such that we "lose" time rather than recording nonsense data.
//
// 0 | sleeping() |
// => Sleeping (10ms)
// +10ms | working() |
// => [!] LOST [!] (10ms)
// +20ms | working() |
// => Working (10ms)
// +30ms | sleeping() |
//
// Total reportable time: 20ms. Saturation: 50%.
sat.sleeping()
now = now.Add(10 * time.Millisecond)
sat.working()
now = now.Add(10 * time.Millisecond)
sat.working()
now = now.Add(10 * time.Millisecond)
sat.sleeping()
require.Equal(t, float32(0.5), reported)
})
t.Run("calling working() first", func(t *testing.T) {
sat := newSaturationMetric([]string{"metric"}, 10*time.Millisecond)
now := sat.lastReport
sat.nowFn = func() time.Time { return now }
var reported float32
sat.reportFn = func(v float32) { reported = v }
// Time from start until working() is treated as lost.
sat.working()
require.Equal(t, float32(0), reported)
sat.sleeping()
now = now.Add(5 * time.Millisecond)
sat.working()
now = now.Add(5 * time.Millisecond)
sat.sleeping()
require.Equal(t, float32(0.5), reported)
})
}