github.com/jpmicrosoft/grab/v3@v3.0.2/pkg/bps/sma_test.go (about)

     1  package bps
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  type Sample struct {
     9  	N      int64
    10  	Expect float64
    11  }
    12  
    13  func getSimpleSamples(sampleCount, rate int) []Sample {
    14  	a := make([]Sample, sampleCount)
    15  	for i := 1; i < sampleCount; i++ {
    16  		a[i] = Sample{N: int64(i * rate), Expect: float64(rate)}
    17  	}
    18  	return a
    19  }
    20  
    21  type SampleSetTest struct {
    22  	Gauge    Gauge
    23  	Interval time.Duration
    24  	Samples  []Sample
    25  }
    26  
    27  func (c *SampleSetTest) Run(t *testing.T) {
    28  	ts := time.Unix(0, 0)
    29  	for i, sample := range c.Samples {
    30  		c.Gauge.Sample(ts, sample.N)
    31  		if actual := c.Gauge.BPS(); actual != sample.Expect {
    32  			t.Errorf("expected: Gauge.BPS() → %0.2f, got %0.2f in test %d", sample.Expect, actual, i+1)
    33  		}
    34  		ts = ts.Add(c.Interval)
    35  	}
    36  }
    37  
    38  func TestSMA_SimpleSteadyCase(t *testing.T) {
    39  	test := &SampleSetTest{
    40  		Interval: time.Second,
    41  		Samples:  getSimpleSamples(100000, 3),
    42  	}
    43  	t.Run("SmallSampleSize", func(t *testing.T) {
    44  		test.Gauge = NewSMA(2)
    45  		test.Run(t)
    46  	})
    47  	t.Run("RegularSize", func(t *testing.T) {
    48  		test.Gauge = NewSMA(6)
    49  		test.Run(t)
    50  	})
    51  	t.Run("LargeSampleSize", func(t *testing.T) {
    52  		test.Gauge = NewSMA(1000)
    53  		test.Run(t)
    54  	})
    55  }