use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedHistogramTest method testWinHistogramWithEdgeCases.
@Test
public void testWinHistogramWithEdgeCases() {
FakeClock clock = new FakeClock();
Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS);
int slices = 10;
long sliceDuration = window.as(Time.NANOSECONDS) / slices;
WindowedApproxHistogram h = new WindowedApproxHistogram(window, slices, DEFAULT_MAX_MEMORY, clock);
h.add(Long.MIN_VALUE);
clock.advance(Amount.of(2 * sliceDuration, Time.NANOSECONDS));
assertEquals(Long.MIN_VALUE, h.getQuantile(0.0));
assertEquals(Long.MIN_VALUE, h.getQuantile(0.5));
assertEquals(Long.MIN_VALUE, h.getQuantile(1.0));
h.add(Long.MAX_VALUE);
clock.advance(Amount.of(2 * sliceDuration, Time.NANOSECONDS));
assertEquals(Long.MIN_VALUE, h.getQuantile(0.0));
assertEquals(Long.MIN_VALUE, h.getQuantile(0.25));
assertEquals(Long.MAX_VALUE, h.getQuantile(0.75));
assertEquals(Long.MAX_VALUE, h.getQuantile(1.0));
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedHistogramTest method testWinHistogramWithGap.
@Test
public void testWinHistogramWithGap() {
FakeClock clock = new FakeClock();
Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS);
int slices = 10;
WindowedHistogram<?> wh = createFullHistogram(window, slices, clock);
for (int j = 0; j < 1000; j++) {
wh.add(100);
}
// [1][2][3][4][5][6][7][8][9][10][100]
// ^
// quantiles are computed based on [1] -> [10]
clock.advance(Amount.of((slices - 1) * 100L / slices, Time.MILLISECONDS));
for (int j = 0; j < 1000; j++) {
wh.add(200);
}
// [1][2][3][4][5][6][7][8][200][10][100]
// ^
// quantiles are computed based on [10][100][1][2][3][4][5][6][7][8]
// and removing old ones [10][100][.][.][.][.][.][.][.][.]
// all the histograms between 100 and 200 are old and shouldn't matter in the computation of
// quantiles.
assertEquals(10L, wh.getQuantile(0.25), 1.0);
assertEquals(100L, wh.getQuantile(0.75), 1.0);
clock.advance(Amount.of(100L / slices, Time.MILLISECONDS));
// [1][2][3][4][5][6][7][8][200][10][100]
// ^
// quantiles are computed based on [100][1][2][3][4][5][6][7][8][200]
// and removing old ones [100][.][.][.][.][.][.][.][.][200]
assertEquals(100L, wh.getQuantile(0.25), 1.0);
assertEquals(200L, wh.getQuantile(0.75), 1.0);
// advance a lot in time, everything should be "forgotten"
clock.advance(Amount.of(500L, Time.MILLISECONDS));
assertEquals(0L, wh.getQuantile(0.5), 1.0);
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedHistogramTest method testSimpleWinHistogram.
@Test
public void testSimpleWinHistogram() {
FakeClock clock = new FakeClock();
Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS);
int slices = 10;
WindowedHistogram<?> wh = createFullHistogram(window, slices, clock);
// check that the global distribution is the aggregation of all underlying histograms
for (int i = 1; i <= slices; i++) {
double q = (double) i / slices;
assertEquals(i, wh.getQuantile(q), 1.0);
}
// advance in time an forget about old values
long sliceDuration = window.as(Time.NANOSECONDS) / slices;
clock.advance(Amount.of(sliceDuration, Time.NANOSECONDS));
for (int j = 0; j < 1000; j++) {
wh.add(11);
}
assertEquals(2, wh.getQuantile(0.05), 1.0);
assertEquals(11, wh.getQuantile(0.99), 1.0);
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class WindowedHistogramTest method testWinHistogramAccuracy.
@Test
public void testWinHistogramAccuracy() {
FakeClock ticker = new FakeClock();
Amount<Long, Time> window = Amount.of(100L, Time.MILLISECONDS);
int slices = 10;
Amount<Long, Time> sliceDuration = Amount.of(window.as(Time.NANOSECONDS) / slices, Time.NANOSECONDS);
WindowedHistogram<?> wh = createFullHistogram(window, slices, ticker);
RealHistogram rh = fillHistogram(new RealHistogram(), sliceDuration, slices, new FakeClock());
assertEquals(wh.getQuantile(0.5), rh.getQuantile(0.5));
assertEquals(wh.getQuantile(0.75), rh.getQuantile(0.75));
assertEquals(wh.getQuantile(0.9), rh.getQuantile(0.9));
assertEquals(wh.getQuantile(0.99), rh.getQuantile(0.99));
}
use of com.twitter.common.util.testing.FakeClock in project commons by twitter.
the class MetricsQueryBench method setUp.
@Override
protected void setUp() {
metrics = Metrics.createDetached();
FakeClock clock = new FakeClock();
Amount<Long, Time> window = WindowedApproxHistogram.DEFAULT_WINDOW;
int slices = WindowedApproxHistogram.DEFAULT_SLICES;
Amount<Long, Time> delta = Amount.of(window.as(Time.MILLISECONDS) / N, Time.MILLISECONDS);
Amount<Long, Data> maxMem = WindowedApproxHistogram.DEFAULT_MAX_MEMORY;
for (int i = 0; i < 10; i++) {
metrics.createCounter("counter-" + i).increment();
HistogramInterface h = new Histogram("hist-" + i, window, slices, maxMem, null, Histogram.DEFAULT_QUANTILES, clock, metrics);
for (int j = 0; j < N; j++) {
h.add(j);
clock.advance(delta);
}
}
// Initialize Histograms and fill them with values (in every buckets for windowed ones)
hist = new Histogram("hist", window, slices, maxMem, null, Histogram.DEFAULT_QUANTILES, clock);
approxHist = new WindowedApproxHistogram(window, slices, maxMem, clock);
winStats = new WindowedStatistics(window, slices, clock);
for (int j = 0; j < N; j++) {
hist.add(j);
approxHist.add(j);
winStats.accumulate(j);
clock.advance(delta);
}
}
Aggregations