...

Source file src/github.com/rs/zerolog/sampler_test.go

Documentation: github.com/rs/zerolog

     1  // +build !binary_log
     2  
     3  package zerolog
     4  
     5  import (
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  var samplers = []struct {
    11  	name    string
    12  	sampler func() Sampler
    13  	total   int
    14  	wantMin int
    15  	wantMax int
    16  }{
    17  	{
    18  		"BasicSampler_1",
    19  		func() Sampler {
    20  			return &BasicSampler{N: 1}
    21  		},
    22  		100, 100, 100,
    23  	},
    24  	{
    25  		"BasicSampler_5",
    26  		func() Sampler {
    27  			return &BasicSampler{N: 5}
    28  		},
    29  		100, 20, 20,
    30  	},
    31  	{
    32  		"RandomSampler",
    33  		func() Sampler {
    34  			return RandomSampler(5)
    35  		},
    36  		100, 10, 30,
    37  	},
    38  	{
    39  		"BurstSampler",
    40  		func() Sampler {
    41  			return &BurstSampler{Burst: 20, Period: time.Second}
    42  		},
    43  		100, 20, 20,
    44  	},
    45  	{
    46  		"BurstSamplerNext",
    47  		func() Sampler {
    48  			return &BurstSampler{Burst: 20, Period: time.Second, NextSampler: &BasicSampler{N: 5}}
    49  		},
    50  		120, 40, 40,
    51  	},
    52  }
    53  
    54  func TestSamplers(t *testing.T) {
    55  	for i := range samplers {
    56  		s := samplers[i]
    57  		t.Run(s.name, func(t *testing.T) {
    58  			sampler := s.sampler()
    59  			got := 0
    60  			for t := s.total; t > 0; t-- {
    61  				if sampler.Sample(0) {
    62  					got++
    63  				}
    64  			}
    65  			if got < s.wantMin || got > s.wantMax {
    66  				t.Errorf("%s.Sample(0) == true %d on %d, want [%d, %d]", s.name, got, s.total, s.wantMin, s.wantMax)
    67  			}
    68  		})
    69  	}
    70  }
    71  
    72  func BenchmarkSamplers(b *testing.B) {
    73  	for i := range samplers {
    74  		s := samplers[i]
    75  		b.Run(s.name, func(b *testing.B) {
    76  			sampler := s.sampler()
    77  			b.RunParallel(func(pb *testing.PB) {
    78  				for pb.Next() {
    79  					sampler.Sample(0)
    80  				}
    81  			})
    82  		})
    83  	}
    84  }
    85  

View as plain text