github.com/uber-go/tally/v4@v4.1.17/stats_test.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package tally
    22  
    23  import (
    24  	"math/rand"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  type statsTestReporter struct {
    32  	last            interface{}
    33  	valueSamples    map[float64]int
    34  	durationSamples map[time.Duration]int
    35  	buckets         Buckets
    36  }
    37  
    38  func newStatsTestReporter() *statsTestReporter {
    39  	return &statsTestReporter{
    40  		valueSamples:    make(map[float64]int),
    41  		durationSamples: make(map[time.Duration]int),
    42  	}
    43  }
    44  
    45  func (r *statsTestReporter) ReportCounter(name string, tags map[string]string, value int64) {
    46  	r.last = value
    47  }
    48  
    49  func (r *statsTestReporter) ReportGauge(name string, tags map[string]string, value float64) {
    50  	r.last = value
    51  }
    52  
    53  func (r *statsTestReporter) ReportTimer(name string, tags map[string]string, interval time.Duration) {
    54  	r.last = interval
    55  }
    56  
    57  func (r *statsTestReporter) ReportHistogramValueSamples(
    58  	name string,
    59  	tags map[string]string,
    60  	buckets Buckets,
    61  	bucketLowerBound,
    62  	bucketUpperBound float64,
    63  	samples int64,
    64  ) {
    65  	r.valueSamples[bucketUpperBound] = int(samples)
    66  	r.buckets = buckets
    67  }
    68  func (r *statsTestReporter) ReportHistogramDurationSamples(
    69  	name string,
    70  	tags map[string]string,
    71  	buckets Buckets,
    72  	bucketLowerBound,
    73  	bucketUpperBound time.Duration,
    74  	samples int64,
    75  ) {
    76  	r.durationSamples[bucketUpperBound] = int(samples)
    77  	r.buckets = buckets
    78  }
    79  
    80  func (r *statsTestReporter) Capabilities() Capabilities {
    81  	return capabilitiesReportingNoTagging
    82  }
    83  
    84  func (r *statsTestReporter) Flush() {}
    85  
    86  func TestCounter(t *testing.T) {
    87  	counter := newCounter(nil)
    88  	r := newStatsTestReporter()
    89  
    90  	counter.Inc(1)
    91  	counter.report("", nil, r)
    92  	assert.Equal(t, int64(1), r.last)
    93  
    94  	counter.Inc(1)
    95  	counter.report("", nil, r)
    96  	assert.Equal(t, int64(1), r.last)
    97  
    98  	counter.Inc(1)
    99  	counter.report("", nil, r)
   100  	assert.Equal(t, int64(1), r.last)
   101  }
   102  
   103  func TestGauge(t *testing.T) {
   104  	gauge := newGauge(nil)
   105  	r := newStatsTestReporter()
   106  
   107  	gauge.Update(42)
   108  	gauge.report("", nil, r)
   109  	assert.Equal(t, float64(42), r.last)
   110  
   111  	gauge.Update(1234)
   112  	gauge.Update(5678)
   113  	gauge.report("", nil, r)
   114  	assert.Equal(t, float64(5678), r.last)
   115  }
   116  
   117  func TestTimer(t *testing.T) {
   118  	r := newStatsTestReporter()
   119  	timer := newTimer("t1", nil, r, nil)
   120  
   121  	timer.Record(42 * time.Millisecond)
   122  	assert.Equal(t, 42*time.Millisecond, r.last)
   123  
   124  	timer.Record(128 * time.Millisecond)
   125  	assert.Equal(t, 128*time.Millisecond, r.last)
   126  }
   127  
   128  func TestHistogramValueSamples(t *testing.T) {
   129  	r := newStatsTestReporter()
   130  	buckets := MustMakeLinearValueBuckets(0, 10, 10)
   131  	storage := newBucketStorage(valueHistogramType, buckets)
   132  	h := newHistogram(valueHistogramType, "h1", nil, r, storage, nil)
   133  
   134  	var offset float64
   135  	for i := 0; i < 3; i++ {
   136  		h.RecordValue(offset + rand.Float64()*10)
   137  	}
   138  	offset = 50
   139  	for i := 0; i < 5; i++ {
   140  		h.RecordValue(offset + rand.Float64()*10)
   141  	}
   142  
   143  	h.report(h.name, h.tags, r)
   144  
   145  	assert.Equal(t, 3, r.valueSamples[10.0])
   146  	assert.Equal(t, 5, r.valueSamples[60.0])
   147  	assert.Equal(t, buckets, r.buckets)
   148  }
   149  
   150  func TestHistogramDurationSamples(t *testing.T) {
   151  	r := newStatsTestReporter()
   152  	buckets := MustMakeLinearDurationBuckets(0, 10*time.Millisecond, 10)
   153  	storage := newBucketStorage(durationHistogramType, buckets)
   154  	h := newHistogram(durationHistogramType, "h1", nil, r, storage, nil)
   155  
   156  	var offset time.Duration
   157  	for i := 0; i < 3; i++ {
   158  		h.RecordDuration(offset +
   159  			time.Duration(rand.Float64()*float64(10*time.Millisecond)))
   160  	}
   161  	offset = 50 * time.Millisecond
   162  	for i := 0; i < 5; i++ {
   163  		h.RecordDuration(offset +
   164  			time.Duration(rand.Float64()*float64(10*time.Millisecond)))
   165  	}
   166  
   167  	h.report(h.name, h.tags, r)
   168  
   169  	assert.Equal(t, 3, r.durationSamples[10*time.Millisecond])
   170  	assert.Equal(t, 5, r.durationSamples[60*time.Millisecond])
   171  	assert.Equal(t, buckets, r.buckets)
   172  }