Search in sources :

Example 1 with Time

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));
        }
    };
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServiceInstance(com.twitter.thrift.ServiceInstance) Time(com.twitter.common.quantity.Time) ServerSetImpl(com.twitter.common.zookeeper.ServerSetImpl) Collection(java.util.Collection) TTransport(org.apache.thrift.transport.TTransport) Pair(com.twitter.common.collections.Pair) Before(org.junit.Before)

Example 2 with Time

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));
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Time(com.twitter.common.quantity.Time) Test(org.junit.Test)

Example 3 with Time

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);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Time(com.twitter.common.quantity.Time) Test(org.junit.Test)

Example 4 with Time

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);
}
Also used : FakeClock(com.twitter.common.util.testing.FakeClock) Time(com.twitter.common.quantity.Time) Test(org.junit.Test)

Example 5 with Time

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));
}
Also used : RealHistogram(com.twitter.common.stats.testing.RealHistogram) FakeClock(com.twitter.common.util.testing.FakeClock) Time(com.twitter.common.quantity.Time) Test(org.junit.Test)

Aggregations

Time (com.twitter.common.quantity.Time)9 FakeClock (com.twitter.common.util.testing.FakeClock)7 Test (org.junit.Test)7 Pair (com.twitter.common.collections.Pair)1 Histogram (com.twitter.common.metrics.Histogram)1 HistogramInterface (com.twitter.common.metrics.HistogramInterface)1 Amount (com.twitter.common.quantity.Amount)1 Data (com.twitter.common.quantity.Data)1 WindowedApproxHistogram (com.twitter.common.stats.WindowedApproxHistogram)1 WindowedStatistics (com.twitter.common.stats.WindowedStatistics)1 RealHistogram (com.twitter.common.stats.testing.RealHistogram)1 ServerSetImpl (com.twitter.common.zookeeper.ServerSetImpl)1 ServiceInstance (com.twitter.thrift.ServiceInstance)1 InetSocketAddress (java.net.InetSocketAddress)1 Collection (java.util.Collection)1 Executor (java.util.concurrent.Executor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 TTransport (org.apache.thrift.transport.TTransport)1 Capture (org.easymock.Capture)1 Before (org.junit.Before)1