use of com.twitter.common.quantity.Time in project commons by twitter.
the class DynamicPoolTest method mySetUp.
@Before
public void mySetUp() throws Exception {
control = createControl();
@SuppressWarnings("unchecked") Function<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>> poolFactory = control.createMock(Function.class);
this.poolFactory = poolFactory;
LoadBalancerImpl<InetSocketAddress> lb = LoadBalancerImpl.create(new RandomStrategy<InetSocketAddress>());
poolRebuilds = new LinkedBlockingQueue<Pair<Set<ObjectPool<Connection<TTransport, InetSocketAddress>>>, Map<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>>>>();
serverSet = new ServerSetImpl(createZkClient(), ZooDefs.Ids.OPEN_ACL_UNSAFE, "/test-service");
Closure<Collection<InetSocketAddress>> onBackendsChosen = Closures.noop();
Amount<Long, Time> restoreInterval = Amount.of(1L, Time.MINUTES);
connectionPool = new DynamicPool<ServiceInstance, TTransport, InetSocketAddress>(serverSet, poolFactory, lb, onBackendsChosen, restoreInterval, Util.GET_ADDRESS, Util.IS_ALIVE) {
@Override
void poolRebuilt(Set<ObjectPool<Connection<TTransport, InetSocketAddress>>> deadPools, Map<InetSocketAddress, ObjectPool<Connection<TTransport, InetSocketAddress>>> livePools) {
super.poolRebuilt(deadPools, livePools);
poolRebuilds.offer(Pair.of(deadPools, livePools));
}
};
}
use of com.twitter.common.quantity.Time 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.quantity.Time 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.quantity.Time 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.quantity.Time 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));
}
Aggregations