github.com/msales/pkg/v3@v3.24.0/stats/l2met_test.go (about)

     1  package stats_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/msales/pkg/v3/log"
    11  	"github.com/msales/pkg/v3/stats"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestL2met_Inc(t *testing.T) {
    16  	l := &testLogger{}
    17  	s := stats.NewL2met(l, "test")
    18  
    19  	s.Inc("test", 2, 1.0, "foo", "bar")
    20  
    21  	assert.Equal(t, "msg= count#test.test=2 foo=bar", l.Render())
    22  }
    23  
    24  func TestL2met_IncWithRate(t *testing.T) {
    25  	l := &testLogger{}
    26  	s := stats.NewL2met(l, "test", stats.UseRates(), stats.UseSampler(testSampler))
    27  
    28  	s.Inc("test", 2, 0.1, "foo", "bar")
    29  
    30  	assert.Equal(t, "msg= count#test.test@0.1=2 foo=bar", l.Render())
    31  }
    32  
    33  func TestL2met_Dec(t *testing.T) {
    34  	l := &testLogger{}
    35  	s := stats.NewL2met(l, "test")
    36  
    37  	s.Dec("test", 2, 1.0, "foo", "bar")
    38  
    39  	assert.Equal(t, "msg= count#test.test=-2 foo=bar", l.Render())
    40  }
    41  
    42  func TestL2met_DecWithRate(t *testing.T) {
    43  	l := &testLogger{}
    44  	s := stats.NewL2met(l, "test", stats.UseRates(), stats.UseSampler(testSampler))
    45  
    46  	s.Dec("test", 2, 0.1, "foo", "bar")
    47  
    48  	assert.Equal(t, "msg= count#test.test@0.1=-2 foo=bar", l.Render())
    49  }
    50  
    51  func TestL2met_Gauge(t *testing.T) {
    52  	l := &testLogger{}
    53  	s := stats.NewL2met(l, "test")
    54  
    55  	s.Gauge("test", 2.1, 1.0, "foo", "bar")
    56  
    57  	assert.Equal(t, "msg= sample#test.test=2.1 foo=bar", l.Render())
    58  }
    59  
    60  func TestL2met_GaugeWithRate(t *testing.T) {
    61  	l := &testLogger{}
    62  	s := stats.NewL2met(l, "test", stats.UseRates(), stats.UseSampler(testSampler))
    63  
    64  	s.Gauge("test", 2.1, 0.1, "foo", "bar")
    65  
    66  	assert.Equal(t, "msg= sample#test.test@0.1=2.1 foo=bar", l.Render())
    67  }
    68  
    69  func TestL2met_Timing(t *testing.T) {
    70  	l := &testLogger{}
    71  	s := stats.NewL2met(l, "test")
    72  
    73  	s.Timing("test", 2*time.Second, 1.0, "foo", "bar")
    74  
    75  	assert.Equal(t, "msg= measure#test.test=2000ms foo=bar", l.Render())
    76  }
    77  
    78  func TestL2met_TimingWithRate(t *testing.T) {
    79  	l := &testLogger{}
    80  	s := stats.NewL2met(l, "test", stats.UseRates(), stats.UseSampler(testSampler))
    81  
    82  	s.Timing("test", 2*time.Second, 0.1, "foo", "bar")
    83  
    84  	assert.Equal(t, "msg= measure#test.test@0.1=2000ms foo=bar", l.Render())
    85  }
    86  
    87  func TestL2met_Samples(t *testing.T) {
    88  	l := &testLogger{}
    89  	s := stats.NewL2met(l, "test", stats.UseRates(), stats.UseSampler(testNeverSampler))
    90  
    91  	s.Timing("test", 2*time.Second, 0.1, "foo", "bar")
    92  
    93  	assert.Equal(t, "msg=", l.Render())
    94  }
    95  
    96  func TestL2met_TimingFractions(t *testing.T) {
    97  	l := &testLogger{}
    98  	s := stats.NewL2met(l, "test")
    99  
   100  	s.Timing("test", 1034500, 1.0, "foo", "bar")
   101  
   102  	assert.Equal(t, "msg= measure#test.test=1.0345ms foo=bar", l.Render())
   103  }
   104  
   105  func TestL2met_TimingPartialFractions(t *testing.T) {
   106  	l := &testLogger{}
   107  	s := stats.NewL2met(l, "test")
   108  
   109  	s.Timing("test", 1230*time.Microsecond, 1.0, "foo", "bar")
   110  
   111  	assert.Equal(t, "msg= measure#test.test=1.23ms foo=bar", l.Render())
   112  }
   113  
   114  func TestL2met_Close(t *testing.T) {
   115  	l := &testLogger{}
   116  	s := stats.NewL2met(l, "test")
   117  
   118  	err := s.Close()
   119  
   120  	assert.NoError(t, err)
   121  }
   122  
   123  func BenchmarkL2met_Inc(b *testing.B) {
   124  	s := stats.NewL2met(log.Null, "test")
   125  
   126  	b.ResetTimer()
   127  	b.ReportAllocs()
   128  	for i := 0; i < b.N; i++ {
   129  		s.Inc("test", 2, 1.0, "foo", "bar")
   130  	}
   131  }
   132  
   133  func BenchmarkL2met_Timing(b *testing.B) {
   134  	s := stats.NewL2met(log.Null, "test")
   135  
   136  	b.ResetTimer()
   137  	b.ReportAllocs()
   138  	for i := 0; i < b.N; i++ {
   139  		s.Timing("test", 231, 1.0, "foo", "bar")
   140  	}
   141  }
   142  
   143  func BenchmarkL2met_IncWithRate(b *testing.B) {
   144  	l := &testLogger{}
   145  	s := stats.NewL2met(l, "test", stats.UseRates())
   146  
   147  	b.ResetTimer()
   148  	b.ReportAllocs()
   149  	for i := 0; i < b.N; i++ {
   150  		s.Inc("test", 2, 0.1, "foo", "bar")
   151  	}
   152  }
   153  
   154  func testSampler(f float32) bool {
   155  	return true
   156  }
   157  
   158  func testNeverSampler(f float32) bool {
   159  	return false
   160  }
   161  
   162  type testLogger struct {
   163  	msg string
   164  	ctx []interface{}
   165  }
   166  
   167  func (l *testLogger) Debug(msg string, ctx ...interface{}) {
   168  	l.msg = msg
   169  	l.ctx = ctx
   170  }
   171  
   172  func (l *testLogger) Info(msg string, ctx ...interface{}) {
   173  	l.msg = msg
   174  	l.ctx = ctx
   175  }
   176  
   177  func (l *testLogger) Error(msg string, ctx ...interface{}) {
   178  	l.msg = msg
   179  	l.ctx = ctx
   180  }
   181  
   182  func (l *testLogger) Render() string {
   183  	var buf bytes.Buffer
   184  	for i := 0; i < len(l.ctx); i += 2 {
   185  		buf.WriteString(fmt.Sprintf("%v=%v ", l.ctx[i], l.ctx[i+1]))
   186  	}
   187  
   188  	return strings.Trim(fmt.Sprintf("msg=%s %s", l.msg, buf.String()), " ")
   189  }