Search in sources :

Example 6 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class HistogramTest method testNegative.

@Test
public void testNegative() {
    FakeClock clock = new FakeClock();
    HistogramInterface hist = new Histogram(name, DEFAULT_WINDOW, DEFAULT_SLICES, DEFAULT_MAX_MEMORY, null, DEFAULT_QUANTILES, clock);
    int[] data = new int[201];
    for (int i = 0; i < data.length; ++i) {
        data[i] = -100 + i;
    }
    for (int x : data) {
        hist.add(x);
    }
    clock.advance(Amount.of(31L, Time.SECONDS));
    Snapshot sample = hist.snapshot();
    assertEquals(-100L, sample.min());
    assertEquals(100L, sample.max());
    assertEquals((long) data.length, sample.count());
    assertEquals(0L, sample.sum());
    assertEquals(0.0, sample.avg(), 0.1);
    long[] expected = new long[DEFAULT_QUANTILES.length];
    for (int i = 0; i < DEFAULT_QUANTILES.length; i++) {
        int idx = (int) (DEFAULT_QUANTILES[i] * data.length);
        expected[i] = data[idx];
    }
    checkQuantiles(expected, sample, ERROR);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Test(org.junit.Test)

Example 7 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class HistogramTest method testHistogram.

@Test
public void testHistogram() {
    int n = 10000;
    FakeClock clock = new FakeClock();
    Precision precision = new Precision(0.001, n);
    int slices = 2;
    HistogramInterface hist = new Histogram(name, Amount.of(1L, Time.MINUTES), slices, null, precision, DEFAULT_QUANTILES, clock);
    double error = precision.getEpsilon() * precision.getN() * slices;
    for (int i = 1; i <= n; ++i) {
        hist.add(i);
    }
    clock.advance(Amount.of(31L, Time.SECONDS));
    Snapshot sample = hist.snapshot();
    assertEquals(1L, sample.min());
    assertEquals((long) n, sample.max());
    assertEquals((long) n, sample.count());
    assertEquals((long) (n * (n + 1) / 2), sample.sum());
    assertEquals(n * (n + 1) / (2.0 * n), sample.avg(), 0.1);
    long[] expected = new long[DEFAULT_QUANTILES.length];
    for (int i = 0; i < DEFAULT_QUANTILES.length; i++) {
        expected[i] = (long) (DEFAULT_QUANTILES[i] * n);
    }
    checkQuantiles(expected, sample, error);
}
Also used : Precision(com.twitter.common.stats.Precision) FakeClock(com.twitter.common.util.testing.FakeClock) Test(org.junit.Test)

Example 8 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class BackoffDeciderTest method setUp.

@Before
public void setUp() {
    control = createControl();
    random = control.createMock(Random.class);
    clock = new FakeClock();
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Before(org.junit.Before)

Example 9 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class MarkDeadStrategyTest method setUp.

@Before
public void setUp() {
    wrappedStrategy = createMock(new Clazz<LoadBalancingStrategy<String>>() {
    });
    onBackendsChosen = createMock(new Clazz<Closure<Collection<String>>>() {
    });
    mockHostChecker = createMock(new Clazz<Predicate<String>>() {
    });
    random = createMock(Random.class);
    clock = new FakeClock();
    Function<String, BackoffDecider> backoffFactory = new Function<String, BackoffDecider>() {

        @Override
        public BackoffDecider apply(String s) {
            return BackoffDecider.builder(s).withSeedSize(1).withClock(clock).withRandom(random).withTolerateFailureRate(0.5).withStrategy(new TruncatedBinaryBackoff(INITIAL_BACKOFF, MAX_BACKOFF)).withRecoveryType(BackoffDecider.RecoveryType.FULL_CAPACITY).withRequestWindow(MAX_BACKOFF).build();
        }
    };
    markDead = new MarkDeadStrategy<String>(wrappedStrategy, backoffFactory, mockHostChecker);
}
Also used : Function(com.google.common.base.Function) TruncatedBinaryBackoff(com.twitter.common.util.TruncatedBinaryBackoff) Random(com.twitter.common.util.Random) BackoffDecider(com.twitter.common.util.BackoffDecider) FakeClock(com.twitter.common.util.testing.FakeClock) Collection(java.util.Collection) Before(org.junit.Before)

Example 10 with FakeClock

use of com.twitter.common.util.testing.FakeClock in project commons by twitter.

the class WindowedStatsTest method testStatsCorrectness.

@Test
public void testStatsCorrectness() {
    FakeClock clock = new FakeClock();
    Statistics reference = new Statistics();
    WindowedStatistics ws = new WindowedStatistics(window, slices, clock);
    for (int i = 0; i < 1000; i++) {
        reference.accumulate(i);
        ws.accumulate(i);
    }
    clock.advance(Amount.of(1 + sliceDuration, Time.NANOSECONDS));
    ws.refresh();
    assertEquals(reference.max(), ws.max());
    assertEquals(reference.min(), ws.min());
    assertEquals(reference.populationSize(), ws.populationSize());
    assertEquals(reference.sum(), ws.sum());
    assertEquals(reference.range(), ws.range());
    assertEquals(reference.mean(), ws.mean(), 0.01);
    assertEquals(reference.variance(), ws.variance(), 0.01);
    assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01);
    for (int i = 0; i < 1000; i++) {
        long x = i + 500;
        reference.accumulate(x);
        ws.accumulate(x);
    }
    clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS));
    ws.refresh();
    assertEquals(reference.max(), ws.max());
    assertEquals(reference.min(), ws.min());
    assertEquals(reference.populationSize(), ws.populationSize());
    assertEquals(reference.sum(), ws.sum());
    assertEquals(reference.range(), ws.range());
    assertEquals(reference.mean(), ws.mean(), 0.01);
    assertEquals(reference.variance(), ws.variance(), 0.01);
    assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01);
    for (int i = 0; i < 1000; i++) {
        long x = i * i;
        reference.accumulate(x);
        ws.accumulate(x);
    }
    clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS));
    ws.refresh();
    assertEquals(reference.max(), ws.max());
    assertEquals(reference.min(), ws.min());
    assertEquals(reference.populationSize(), ws.populationSize());
    assertEquals(reference.sum(), ws.sum());
    assertEquals(reference.range(), ws.range());
    assertEquals(reference.mean(), ws.mean(), 0.01);
    assertEquals(reference.variance(), ws.variance(), 0.01);
    assertEquals(reference.standardDeviation(), ws.standardDeviation(), 0.01);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) WindowedStatistics(com.twitter.common.stats.WindowedStatistics) WindowedStatistics(com.twitter.common.stats.WindowedStatistics) Test(org.junit.Test)

Aggregations

FakeClock (com.twitter.common.util.testing.FakeClock)24 Test (org.junit.Test)14 Before (org.junit.Before)9 Time (com.twitter.common.quantity.Time)7 WindowedStatistics (com.twitter.common.stats.WindowedStatistics)6 Function (com.google.common.base.Function)2 BackoffDecider (com.twitter.common.util.BackoffDecider)2 Random (com.twitter.common.util.Random)2 TruncatedBinaryBackoff (com.twitter.common.util.TruncatedBinaryBackoff)2 Collection (java.util.Collection)2 AbstractModule (com.google.inject.AbstractModule)1 Injector (com.google.inject.Injector)1 Singleton (com.google.inject.Singleton)1 GuiceServletContextListener (com.google.inject.servlet.GuiceServletContextListener)1 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)1 DefaultClientConfig (com.sun.jersey.api.client.config.DefaultClientConfig)1 JerseyServletModule (com.sun.jersey.guice.JerseyServletModule)1 GuiceContainer (com.sun.jersey.guice.spi.container.servlet.GuiceContainer)1 ContainerRequest (com.sun.jersey.spi.container.ContainerRequest)1 ContainerResponse (com.sun.jersey.spi.container.ContainerResponse)1