| 1 |
// Copyright 2021 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
//go:build !go1.17 |
| 15 |
// +build !go1.17 |
| 16 |
|
| 17 |
package prometheus |
| 18 |
|
| 19 |
import ( |
| 20 |
"runtime" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
dto "github.com/prometheus/client_model/go" |
| 25 |
) |
| 26 |
|
| 27 |
func TestGoCollectorMemStats(t *testing.T) { |
| 28 |
var ( |
| 29 |
c = NewGoCollector().(*goCollector) |
| 30 |
got uint64 |
| 31 |
) |
| 32 |
|
| 33 |
checkCollect := func(want uint64) { |
| 34 |
metricCh := make(chan Metric) |
| 35 |
endCh := make(chan struct{}) |
| 36 |
|
| 37 |
go func() { |
| 38 |
c.Collect(metricCh) |
| 39 |
close(endCh) |
| 40 |
}() |
| 41 |
Collect: |
| 42 |
for { |
| 43 |
select { |
| 44 |
case metric := <-metricCh: |
| 45 |
if metric.Desc().fqName != "go_memstats_alloc_bytes" { |
| 46 |
continue Collect |
| 47 |
} |
| 48 |
pb := &dto.Metric{} |
| 49 |
metric.Write(pb) |
| 50 |
got = uint64(pb.GetGauge().GetValue()) |
| 51 |
case <-endCh: |
| 52 |
break Collect |
| 53 |
} |
| 54 |
} |
| 55 |
if want != got { |
| 56 |
t.Errorf("unexpected value of go_memstats_alloc_bytes, want %d, got %d", want, got) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// Speed up the timing to make the test faster. |
| 61 |
c.msMaxWait = 5 * time.Millisecond |
| 62 |
c.msMaxAge = 50 * time.Millisecond |
| 63 |
|
| 64 |
// Scenario 1: msRead responds slowly, no previous memstats available, |
| 65 |
// msRead is executed anyway. |
| 66 |
c.msRead = func(ms *runtime.MemStats) { |
| 67 |
time.Sleep(20 * time.Millisecond) |
| 68 |
ms.Alloc = 1 |
| 69 |
} |
| 70 |
checkCollect(1) |
| 71 |
// Now msLast is set. |
| 72 |
c.msMtx.Lock() |
| 73 |
if want, got := uint64(1), c.msLast.Alloc; want != got { |
| 74 |
t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) |
| 75 |
} |
| 76 |
c.msMtx.Unlock() |
| 77 |
|
| 78 |
// Scenario 2: msRead responds fast, previous memstats available, new |
| 79 |
// value collected. |
| 80 |
c.msRead = func(ms *runtime.MemStats) { |
| 81 |
ms.Alloc = 2 |
| 82 |
} |
| 83 |
checkCollect(2) |
| 84 |
// msLast is set, too. |
| 85 |
c.msMtx.Lock() |
| 86 |
if want, got := uint64(2), c.msLast.Alloc; want != got { |
| 87 |
t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) |
| 88 |
} |
| 89 |
c.msMtx.Unlock() |
| 90 |
|
| 91 |
// Scenario 3: msRead responds slowly, previous memstats available, old |
| 92 |
// value collected. |
| 93 |
c.msRead = func(ms *runtime.MemStats) { |
| 94 |
time.Sleep(20 * time.Millisecond) |
| 95 |
ms.Alloc = 3 |
| 96 |
} |
| 97 |
checkCollect(2) |
| 98 |
// After waiting, new value is still set in msLast. |
| 99 |
time.Sleep(80 * time.Millisecond) |
| 100 |
c.msMtx.Lock() |
| 101 |
if want, got := uint64(3), c.msLast.Alloc; want != got { |
| 102 |
t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) |
| 103 |
} |
| 104 |
c.msMtx.Unlock() |
| 105 |
|
| 106 |
// Scenario 4: msRead responds slowly, previous memstats is too old, new |
| 107 |
// value collected. |
| 108 |
c.msRead = func(ms *runtime.MemStats) { |
| 109 |
time.Sleep(20 * time.Millisecond) |
| 110 |
ms.Alloc = 4 |
| 111 |
} |
| 112 |
checkCollect(4) |
| 113 |
c.msMtx.Lock() |
| 114 |
if want, got := uint64(4), c.msLast.Alloc; want != got { |
| 115 |
t.Errorf("unexpected of msLast.Alloc, want %d, got %d", want, got) |
| 116 |
} |
| 117 |
c.msMtx.Unlock() |
| 118 |
} |