aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/vespa/streamingvisitors/tracing/ProbabilisticSampleRateTest.java
blob: c732baa9204bfb5ebf5143ebc2e3b21bdf59f027 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.streamingvisitors.tracing;

import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ProbabilisticSampleRateTest {

    private static long ms2ns(long ms) {
        return TimeUnit.MILLISECONDS.toNanos(ms);
    }

    @Test
    void samples_are_rate_limited_per_second() {
        var clock = MockUtils.mockedClockReturning(ms2ns(10_000), ms2ns(10_500), ms2ns(10_500), ms2ns(10_501));
        var rng = MockUtils.mockedRandomReturning(0.1, 0.51, 0.49, 0.01);
        var sampler = new ProbabilisticSampleRate(clock, () -> rng, 1.0);
        // 1st invocation, 10 seconds (technically "infinity") since last sample. P = 1.0, sampled
        assertTrue(sampler.shouldSample());
        // 2nd invocation, 0.5 seconds since last sample. rng = 0.51 >= P = 0.5, not sampled
        assertFalse(sampler.shouldSample());
        // 3rd invocation, 0.5 seconds since last sample. rng = 0.49 < P = 0.5, sampled
        assertTrue(sampler.shouldSample());
        // 4th invocation, 0.001 seconds since last sample. rng = 0.01 >= P = 0.001, not sampled
        assertFalse(sampler.shouldSample());
    }

    @Test
    void zero_desired_sample_rate_returns_false() {
        var clock = MockUtils.mockedClockReturning(ms2ns(10_000));
        var rng = MockUtils.mockedRandomReturning(0.99999999); // [0, 1)
        var sampler = new ProbabilisticSampleRate(clock, () -> rng, 0.0);

        assertFalse(sampler.shouldSample());
    }

}