Debian Codegraph @

internal/memorylimiter/memorylimiter_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package memorylimiter
5
6 import (
7 "runtime"
8 "testing"
9 "time"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13 "go.uber.org/zap"
14
15 "go.opentelemetry.io/collector/internal/memorylimiter/iruntime"
16 )
17
18 // TestMemoryPressureResponse manipulates results from querying memory and
19 // check expected side effects.
20 func TestMemoryPressureResponse(t *testing.T) {
21 var currentMemAlloc uint64
22 cfg := &Config{
23 CheckInterval: 1 * time.Minute,
24 MemoryLimitMiB: 1024,
25 MemorySpikeLimitMiB: 0,
26 }
27 ml, err := NewMemoryLimiter(cfg, zap.NewNop())
28 require.NoError(t, err)
29 ml.readMemStatsFn = func(ms *runtime.MemStats) {
30 ms.Alloc = currentMemAlloc * mibBytes
31 }
32
33 // Below memAllocLimit.
34 currentMemAlloc = 800
35 ml.CheckMemLimits()
36 assert.False(t, ml.MustRefuse())
37
38 // Above memAllocLimit.
39 currentMemAlloc = 1800
40 ml.CheckMemLimits()
41 assert.True(t, ml.MustRefuse())
42
43 // Check spike limit
44 ml.usageChecker.memSpikeLimit = 512 * mibBytes
45
46 // Below memSpikeLimit.
47 currentMemAlloc = 500
48 ml.CheckMemLimits()
49 assert.False(t, ml.MustRefuse())
50
51 // Above memSpikeLimit.
52 currentMemAlloc = 550
53 ml.CheckMemLimits()
54 assert.True(t, ml.MustRefuse())
55 }
56
57 func TestGetDecision(t *testing.T) {
58 t.Run("fixed_limit", func(t *testing.T) {
59 d, err := getMemUsageChecker(&Config{MemoryLimitMiB: 100, MemorySpikeLimitMiB: 20}, zap.NewNop())
60 require.NoError(t, err)
61 assert.Equal(t, &memUsageChecker{
62 memAllocLimit: 100 * mibBytes,
63 memSpikeLimit: 20 * mibBytes,
64 }, d)
65 })
66
67 t.Cleanup(func() {
68 GetMemoryFn = iruntime.TotalMemory
69 })
70 GetMemoryFn = func() (uint64, error) {
71 return 100 * mibBytes, nil
72 }
73 t.Run("percentage_limit", func(t *testing.T) {
74 d, err := getMemUsageChecker(&Config{MemoryLimitPercentage: 50, MemorySpikePercentage: 10}, zap.NewNop())
75 require.NoError(t, err)
76 assert.Equal(t, &memUsageChecker{
77 memAllocLimit: 50 * mibBytes,
78 memSpikeLimit: 10 * mibBytes,
79 }, d)
80 })
81 }
82
83 func TestRefuseDecision(t *testing.T) {
84 decision1000Limit30Spike30 := newPercentageMemUsageChecker(1000, 60, 30)
85 decision1000Limit60Spike50 := newPercentageMemUsageChecker(1000, 60, 50)
86 decision1000Limit40Spike20 := newPercentageMemUsageChecker(1000, 40, 20)
87
88 tests := []struct {
89 name string
90 usageChecker memUsageChecker
91 ms *runtime.MemStats
92 shouldRefuse bool
93 }{
94 {
95 name: "should refuse over limit",
96 usageChecker: *decision1000Limit30Spike30,
97 ms: &runtime.MemStats{Alloc: 600},
98 shouldRefuse: true,
99 },
100 {
101 name: "should not refuse",
102 usageChecker: *decision1000Limit30Spike30,
103 ms: &runtime.MemStats{Alloc: 100},
104 shouldRefuse: false,
105 },
106 {
107 name: "should not refuse spike, fixed usageChecker",
108 usageChecker: memUsageChecker{
109 memAllocLimit: 600,
110 memSpikeLimit: 500,
111 },
112 ms: &runtime.MemStats{Alloc: 300},
113 shouldRefuse: true,
114 },
115 {
116 name: "should refuse, spike, percentage usageChecker",
117 usageChecker: *decision1000Limit60Spike50,
118 ms: &runtime.MemStats{Alloc: 300},
119 shouldRefuse: true,
120 },
121 {
122 name: "should refuse, spike, percentage usageChecker",
123 usageChecker: *decision1000Limit40Spike20,
124 ms: &runtime.MemStats{Alloc: 250},
125 shouldRefuse: true,
126 },
127 }
128 for _, test := range tests {
129 t.Run(test.name, func(t *testing.T) {
130 shouldRefuse := test.usageChecker.aboveSoftLimit(test.ms)
131 assert.Equal(t, test.shouldRefuse, shouldRefuse)
132 })
133 }
134 }
135
136 func TestCallGCWhenSoftLimit(t *testing.T) {
137 tests := []struct {
138 name string
139 mlCfg *Config
140 memAllocMiB [2]uint64
141 numGCs int
142 }{
143 {
144 name: "GC when first soft limit and not immediately",
145 mlCfg: &Config{
146 CheckInterval: 1 * time.Minute,
147 MinGCIntervalWhenSoftLimited: 10 * time.Second,
148 MemoryLimitMiB: 50,
149 MemorySpikeLimitMiB: 10,
150 },
151 memAllocMiB: [2]uint64{45, 45},
152 numGCs: 1,
153 },
154 {
155 name: "GC always when soft limit min interval is 0",
156 mlCfg: &Config{
157 CheckInterval: 1 * time.Minute,
158 MinGCIntervalWhenSoftLimited: 0,
159 MemoryLimitMiB: 50,
160 MemorySpikeLimitMiB: 10,
161 },
162 memAllocMiB: [2]uint64{45, 45},
163 numGCs: 2,
164 },
165 {
166 name: "GC when first hard limit and not immediately",
167 mlCfg: &Config{
168 CheckInterval: 1 * time.Minute,
169 MinGCIntervalWhenHardLimited: 10 * time.Second,
170 MemoryLimitMiB: 50,
171 MemorySpikeLimitMiB: 10,
172 },
173 memAllocMiB: [2]uint64{55, 55},
174 numGCs: 1,
175 },
176 {
177 name: "GC always when hard limit min interval is 0",
178 mlCfg: &Config{
179 CheckInterval: 1 * time.Minute,
180 MinGCIntervalWhenHardLimited: 0,
181 MemoryLimitMiB: 50,
182 MemorySpikeLimitMiB: 10,
183 },
184 memAllocMiB: [2]uint64{55, 55},
185 numGCs: 2,
186 },
187 {
188 name: "GC based on soft then based on hard limit",
189 mlCfg: &Config{
190 CheckInterval: 1 * time.Minute,
191 MinGCIntervalWhenSoftLimited: 10 * time.Second,
192 MinGCIntervalWhenHardLimited: 0,
193 MemoryLimitMiB: 50,
194 MemorySpikeLimitMiB: 10,
195 },
196 memAllocMiB: [2]uint64{45, 55},
197 numGCs: 2,
198 },
199 }
200 for _, tt := range tests {
201 t.Run(tt.name, func(t *testing.T) {
202 ml, err := NewMemoryLimiter(tt.mlCfg, zap.NewNop())
203 require.NoError(t, err)
204 memAllocMiB := uint64(0)
205 ml.readMemStatsFn = func(ms *runtime.MemStats) {
206 ms.Alloc = memAllocMiB * mibBytes
207 }
208 // Mark last GC in the past so that even first call can trigger GC
209 // Not updating the initialization code, since at the beginning of the collector no need to GC.
210 ml.lastGCDone = ml.lastGCDone.Add(-time.Minute)
211 numGCs := 0
212 ml.runGCFn = func() {
213 numGCs++
214 }
215
216 memAllocMiB = tt.memAllocMiB[0]
217 ml.CheckMemLimits()
218 assert.True(t, ml.MustRefuse())
219
220 // On windows, time has larger precision, and checking here again may return same time as "lastGCDone"
221 // which will not trigger a new GC for 0 duration, update last GC with -1 millis.
222 ml.lastGCDone = ml.lastGCDone.Add(-1 * time.Millisecond)
223 memAllocMiB = tt.memAllocMiB[1]
224 ml.CheckMemLimits()
225 assert.True(t, ml.MustRefuse())
226
227 assert.Equal(t, tt.numGCs, numGCs)
228 })
229 }
230 }