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);
}
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);
}
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();
}
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);
}
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);
}
Aggregations