gitee.com/gricks/utils@v1.0.8/metrics_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  
     9  	. "github.com/smartystreets/goconvey/convey"
    10  )
    11  
    12  func loadHistogramData() []int {
    13  	b, err := ioutil.ReadFile("metrics_test.data")
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	rst := []int{}
    18  	a := strings.Fields(string(b))
    19  	for _, v := range a {
    20  		if v != "" {
    21  			i, err := strconv.Atoi(v)
    22  			if err != nil {
    23  				panic(err)
    24  			}
    25  			rst = append(rst, i)
    26  		}
    27  	}
    28  	return rst
    29  }
    30  
    31  func Test_Histogram(t *testing.T) {
    32  	Convey("Histogram", t, func() {
    33  		h := NewHistogram(160)
    34  		for _, v := range loadHistogramData() {
    35  			h.Add(float64(v))
    36  		}
    37  		So(h.Count(), ShouldEqual, 14999)
    38  		So(h.Quantile(0.25), ShouldAlmostEqual, 14, 0.2)
    39  		So(h.Quantile(0.50), ShouldAlmostEqual, 18, 0.2)
    40  		So(h.Quantile(0.75), ShouldAlmostEqual, 22, 0.2)
    41  	})
    42  }