Search in sources :

Example 6 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testTargetUpdate.

@Test
public void testTargetUpdate() {
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule r = new CentralizedRule(input, new RandImpl());
    SamplingTargetDocument update = new SamplingTargetDocument().withRuleName("r1").withFixedRate(0.5).withInterval(20);
    r.update(update, Instant.now());
    double fixedRate = Whitebox.getInternalState(r, "fixedRate", CentralizedRule.class);
    Assert.assertEquals(0.5, fixedRate, 0);
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) Test(org.junit.Test)

Example 7 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testExpiredReservoirNegativeBernoulliSample.

@Test
public void testExpiredReservoirNegativeBernoulliSample() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 0, 0.2);
    CentralizedRule rule = new CentralizedRule(input, rand);
    SamplingTargetDocument target = createTarget(0, 0.2, 1499999999);
    rule.update(target, clock.instant());
    Mockito.when(rand.next()).thenReturn(0.4);
    SamplingResponse response = rule.sample(clock.instant());
    Assert.assertFalse(response.isSampled());
    Assert.assertEquals("r1", response.getRuleName().get());
    Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
    Assert.assertEquals(0, s.getSampled());
    Assert.assertEquals(1, s.getRequests());
    Assert.assertEquals(0, s.getBorrowed());
}
Also used : SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Clock(java.time.Clock) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse) Test(org.junit.Test)

Example 8 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testPositiveSampleTake.

@Test
public void testPositiveSampleTake() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule rule = new CentralizedRule(input, new RandImpl());
    SamplingTargetDocument target = createTarget(2, 0.0, 1500000010);
    rule.update(target, clock.instant());
    SamplingResponse response = rule.sample(clock.instant());
    Assert.assertTrue(response.isSampled());
    Assert.assertEquals("r1", response.getRuleName().get());
    Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
    Assert.assertEquals(1, s.getSampled());
    Assert.assertEquals(1, s.getRequests());
    Assert.assertEquals(0, s.getBorrowed());
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Clock(java.time.Clock) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse) Test(org.junit.Test)

Example 9 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testSnapshot.

@Test
public void testSnapshot() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule rule = new CentralizedRule(input, new RandImpl());
    SamplingTargetDocument target = createTarget(2, 0.0, 1500000010);
    rule.update(target, clock.instant());
    rule.sample(clock.instant());
    Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
    // Assert statistics were updated
    Assert.assertEquals(1, s.getSampled());
    Assert.assertEquals(1, s.getRequests());
    Assert.assertEquals(0, s.getBorrowed());
    SamplingStatisticsDocument snapshot = rule.snapshot(Date.from(clock.instant()));
    // Assert snapshot contains expected statistics
    Assert.assertEquals("r1", snapshot.getRuleName());
    Assert.assertEquals(TimeUnit.SECONDS.toMillis(1500000000), snapshot.getTimestamp().toInstant().toEpochMilli());
    Assert.assertEquals(1, snapshot.getRequestCount().intValue());
    Assert.assertEquals(1, snapshot.getSampledCount().intValue());
    Assert.assertEquals(0, snapshot.getBorrowCount().intValue());
    // Assert current statistics are empty
    Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getRequestCount().intValue());
    Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getSampledCount().intValue());
    Assert.assertEquals(0, rule.snapshot(Date.from(clock.instant())).getBorrowCount().intValue());
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) SamplingStatisticsDocument(com.amazonaws.services.xray.model.SamplingStatisticsDocument) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Clock(java.time.Clock) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) Test(org.junit.Test)

Aggregations

SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)9 SamplingRule (com.amazonaws.services.xray.model.SamplingRule)8 Test (org.junit.Test)8 Clock (java.time.Clock)6 SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)5 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)4 SamplingStatisticsDocument (com.amazonaws.services.xray.model.SamplingStatisticsDocument)1 CentralizedReservoir (com.amazonaws.xray.strategy.sampling.reservoir.CentralizedReservoir)1 CentralizedRule (com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)1