go-watchdog/watermarks_test.go
Raúl Kripalani 31d951f370 implement automatic heapdumps when usage is above threshold.
A heapdump will be captured when the usage trespasses the threshold.
Staying above the threshold won't trigger another heapdump.
If the usage goes down, then back up, that is considered another
"episode" to be captured in a heapdump.

This feature is driven by three parameters:

* HeapdumpDir: the directory where the watchdog will write the heapdump.
  It will be created if it doesn't exist upon initialization. An error when
  creating the dir will not prevent heapdog initialization; it will just
  disable the heapdump capture feature.

  If zero-valued, the feature is disabled. Heapdumps will be written to path:
  <HeapdumpDir>/<RFC3339Nano formatted timestamp>.heap.

* HeapdumpMaxCaptures: sets the maximum amount of heapdumps a process will
  generate. This limits the amount of episodes that will be captured, in case
  the utilization climbs repeatedly over the threshold. By default, it is 10.

* HeapdumpThreshold: sets the utilization threshold that will trigger a
  heap dump to be taken automatically. A zero value disables this feature.
  By default, it is disabled.
2021-01-19 20:02:16 +00:00

55 lines
1.6 KiB
Go

package watchdog
import (
"testing"
"github.com/raulk/clock"
"github.com/stretchr/testify/require"
)
var (
watermarks = []float64{0.50, 0.75, 0.80}
thresholds = func() []uint64 {
var ret []uint64
for _, w := range watermarks {
ret = append(ret, uint64(float64(limit64MiB)*w))
}
return ret
}()
)
func TestProgressiveWatermarks(t *testing.T) {
clk := clock.NewMock()
Clock = clk
p, err := NewWatermarkPolicy(watermarks...)(limit64MiB)
require.NoError(t, err)
// at zero
next := p.Evaluate(UtilizationSystem, uint64(0))
require.EqualValues(t, thresholds[0], next)
// before the watermark.
next = p.Evaluate(UtilizationSystem, uint64(float64(limit64MiB)*watermarks[0])-1)
require.EqualValues(t, thresholds[0], next)
// exactly at the watermark; gives us the next watermark, as the watchdodg would've
// taken care of triggering the first watermark.
next = p.Evaluate(UtilizationSystem, uint64(float64(limit64MiB)*watermarks[0]))
require.EqualValues(t, thresholds[1], next)
// after the watermark gives us the next watermark.
next = p.Evaluate(UtilizationSystem, uint64(float64(limit64MiB)*watermarks[0])+1)
require.EqualValues(t, thresholds[1], next)
// last watermark; disable the policy.
next = p.Evaluate(UtilizationSystem, uint64(float64(limit64MiB)*watermarks[2]))
require.EqualValues(t, PolicyTempDisabled, next)
next = p.Evaluate(UtilizationSystem, uint64(float64(limit64MiB)*watermarks[2]+1))
require.EqualValues(t, PolicyTempDisabled, next)
next = p.Evaluate(UtilizationSystem, limit64MiB)
require.EqualValues(t, PolicyTempDisabled, next)
}