@@ -17,7 +17,6 @@ package host_test
17
17
import (
18
18
"context"
19
19
"os"
20
- "runtime"
21
20
"testing"
22
21
"time"
23
22
@@ -142,45 +141,32 @@ func TestHostMemory(t *testing.T) {
142
141
assert .NoError (t , err )
143
142
144
143
ctx := context .Background ()
145
- hostBefore , err := mem .VirtualMemoryWithContext (ctx )
144
+ vMem , err := mem .VirtualMemoryWithContext (ctx )
146
145
require .NoError (t , err )
147
146
148
- slice := make ([]byte , 100 * 1024 * 1024 )
149
- defer runtime .KeepAlive (slice )
150
- for i := range slice {
151
- slice [i ] = byte (i )
152
- }
153
-
154
- // As we are going to read the /proc file system for this info, sleep a while:
155
- time .Sleep (time .Second )
156
-
157
147
impl .RunAsyncInstruments ()
158
148
159
- hostAfter , err := mem .VirtualMemoryWithContext (ctx )
160
- require .NoError (t , err )
161
-
162
149
hostUsed := getMetric (impl , "system.memory.usage" , host .LabelMemoryUsed [0 ])
150
+ assert .Greater (t , hostUsed , 0.0 )
151
+ assert .LessOrEqual (t , hostUsed , float64 (vMem .Total ))
152
+
163
153
hostAvailable := getMetric (impl , "system.memory.usage" , host .LabelMemoryAvailable [0 ])
154
+ assert .GreaterOrEqual (t , hostAvailable , 0.0 )
155
+ assert .Less (t , hostAvailable , float64 (vMem .Total ))
164
156
165
157
hostUsedUtil := getMetric (impl , "system.memory.utilization" , host .LabelMemoryUsed [0 ])
166
- hostAvailableUtil := getMetric (impl , "system.memory.utilization" , host .LabelMemoryAvailable [0 ])
167
-
168
- beforeTotal := hostBefore .Available + hostBefore .Used
169
- afterTotal := hostAfter .Available + hostAfter .Used
170
- measureTotal := hostUsed + hostAvailable
171
-
172
- // Tolerance is 5%
173
- const tolerance = 0.05
158
+ assert .Greater (t , hostUsedUtil , 0.0 )
159
+ assert .LessOrEqual (t , hostUsedUtil , 1.0 )
174
160
175
- // Check that the sum of used and available doesn't change:
176
- require .InEpsilon (t , float64 (beforeTotal ), measureTotal , tolerance )
177
- require .InEpsilon (t , float64 (afterTotal ), measureTotal , tolerance )
178
-
179
- // Check that the implied total is equal from both Used and Available metrics:
180
- require .InEpsilon (t , hostUsed / hostUsedUtil , hostAvailable / hostAvailableUtil , tolerance )
161
+ hostAvailableUtil := getMetric (impl , "system.memory.utilization" , host .LabelMemoryAvailable [0 ])
162
+ assert .GreaterOrEqual (t , hostAvailableUtil , 0.0 )
163
+ assert .Less (t , hostAvailableUtil , 1.0 )
181
164
182
- // Check that utilization sums to 1.0:
183
- require .InEpsilon (t , 1.0 , hostUsedUtil + hostAvailableUtil , tolerance )
165
+ if hostUsed > hostAvailable {
166
+ assert .Greater (t , hostUsedUtil , hostAvailableUtil )
167
+ } else {
168
+ assert .Less (t , hostUsedUtil , hostAvailableUtil )
169
+ }
184
170
}
185
171
186
172
func sendBytes (t * testing.T , count int ) error {
0 commit comments