github.com/songzhibin97/gkit@v1.2.13/internal/stat/rolling_counter_test.go (about)

     1  package stat
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestRollingCounterAdd(t *testing.T) {
    11  	size := 3
    12  	bucketDuration := time.Second
    13  	r := NewRollingCounter(size, bucketDuration)
    14  	listBuckets := func() [][]float64 {
    15  		buckets := make([][]float64, 0)
    16  		r.Reduce(func(i Iterator) float64 {
    17  			for i.Next() {
    18  				bucket := i.Bucket()
    19  				buckets = append(buckets, bucket.Points)
    20  			}
    21  			return 0.0
    22  		})
    23  		return buckets
    24  	}
    25  	assert.Equal(t, [][]float64{{}, {}, {}}, listBuckets())
    26  	r.Add(1)
    27  	assert.Equal(t, [][]float64{{}, {}, {1}}, listBuckets())
    28  	time.Sleep(time.Second)
    29  	r.Add(2)
    30  	r.Add(3)
    31  	assert.Equal(t, [][]float64{{}, {1}, {5}}, listBuckets())
    32  	time.Sleep(time.Second)
    33  	r.Add(4)
    34  	r.Add(5)
    35  	r.Add(6)
    36  	assert.Equal(t, [][]float64{{1}, {5}, {15}}, listBuckets())
    37  	time.Sleep(time.Second)
    38  	r.Add(7)
    39  	assert.Equal(t, [][]float64{{5}, {15}, {7}}, listBuckets())
    40  }
    41  
    42  func TestRollingCounterReduce(t *testing.T) {
    43  	size := 3
    44  	bucketDuration := time.Second
    45  	r := NewRollingCounter(size, bucketDuration)
    46  	for x := 0; x < size; x = x + 1 {
    47  		for i := 0; i <= x; i++ {
    48  			r.Add(1)
    49  		}
    50  		if x < size-1 {
    51  			time.Sleep(bucketDuration)
    52  		}
    53  	}
    54  	result := r.Reduce(func(iterator Iterator) float64 {
    55  		var result float64
    56  		for iterator.Next() {
    57  			bucket := iterator.Bucket()
    58  			result += bucket.Points[0]
    59  		}
    60  		return result
    61  	})
    62  	if result != 6.0 {
    63  		t.Fatalf("Validate sum of points. result: %f", result)
    64  	}
    65  }
    66  
    67  func TestRollingCounterDataRace(t *testing.T) {
    68  	size := 3
    69  	bucketDuration := time.Millisecond * 10
    70  	r := NewRollingCounter(size, bucketDuration)
    71  	stop := make(chan bool)
    72  	go func() {
    73  		for {
    74  			select {
    75  			case <-stop:
    76  				return
    77  			default:
    78  				r.Add(1)
    79  				time.Sleep(time.Millisecond * 5)
    80  			}
    81  		}
    82  	}()
    83  	go func() {
    84  		for {
    85  			select {
    86  			case <-stop:
    87  				return
    88  			default:
    89  				_ = r.Reduce(func(i Iterator) float64 {
    90  					for i.Next() {
    91  						bucket := i.Bucket()
    92  						for range bucket.Points {
    93  							continue
    94  						}
    95  					}
    96  					return 0
    97  				})
    98  			}
    99  		}
   100  	}()
   101  	time.Sleep(time.Second * 3)
   102  	close(stop)
   103  }
   104  
   105  func BenchmarkRollingCounterIncr(b *testing.B) {
   106  	size := 3
   107  	bucketDuration := time.Millisecond * 100
   108  	r := NewRollingCounter(size, bucketDuration)
   109  	b.ResetTimer()
   110  	for i := 0; i <= b.N; i++ {
   111  		r.Add(1)
   112  	}
   113  }
   114  
   115  func BenchmarkRollingCounterReduce(b *testing.B) {
   116  	size := 3
   117  	bucketDuration := time.Second
   118  	r := NewRollingCounter(size, bucketDuration)
   119  	for i := 0; i <= 10; i++ {
   120  		r.Add(1)
   121  		time.Sleep(time.Millisecond * 500)
   122  	}
   123  	b.ResetTimer()
   124  	for i := 0; i <= b.N; i++ {
   125  		_ = r.Reduce(func(i Iterator) float64 {
   126  			var result float64
   127  			for i.Next() {
   128  				bucket := i.Bucket()
   129  				if len(bucket.Points) != 0 {
   130  					result += bucket.Points[0]
   131  				}
   132  			}
   133  			return result
   134  		})
   135  	}
   136  }