zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

Make the BurstSampler more accurate, especially when resetting the count.

Open newacorn opened this issue 5 months ago • 0 comments

After refactoring, the following test can pass.

func TestBurstSampler(t *testing.T) {
	s := &BurstSampler{Burst: 20, Period: time.Millisecond * 200}
	wg := sync.WaitGroup{}
	wg.Add(2000)
	past := atomic.Int64{}
	start := time.Now()
	for i := 0; i < 2000; i++ {
		go func() {
			defer wg.Done()
			for {
				if s.Sample(0) {
					past.Add(1)
				}
				if time.Now().Sub(start) > time.Millisecond*210 {
					break
				}
			}
		}()
	}
	wg.Wait()
	if past.Load() != 40 {
		t.Errorf("got %d, want 40", past.Load())
	}
}

newacorn avatar Sep 10 '24 22:09 newacorn