Search in sources :

Example 1 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCollapserBatchSizeDistributionStreamTest method testEmptyStreamProducesEmptyDistributions.

@Test
public void testEmptyStreamProducesEmptyDistributions() {
    HystrixCollapserKey key = HystrixCollapserKey.Factory.asKey("Collapser-Batch-Size-A");
    stream = RollingCollapserBatchSizeDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().skip(10).take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println("OnNext @ " + System.currentTimeMillis());
            assertEquals(0, distribution.getTotalCount());
        }
    });
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(0, stream.getLatest().getTotalCount());
}
Also used : CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 2 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testSingleBucketGetsStored.

@Test
public void testSingleBucketGetsStored() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-B");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() == 1) {
                assertBetween(10, 50, (int) distribution.getMean());
            } else if (distribution.getTotalCount() == 2) {
                assertBetween(300, 400, (int) distribution.getMean());
            }
        }
    });
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    //latency = 600
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.TIMEOUT);
    cmd1.observe();
    cmd2.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertBetween(150, 400, stream.getLatestMean());
    assertBetween(10, 50, stream.getLatestPercentile(0.0));
    assertBetween(300, 800, stream.getLatestPercentile(100.0));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 3 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testResponseFromCacheDoesNotGetLatencyTracked.

@Test
public void testResponseFromCacheDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-G");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //should get 1 SUCCESS and 1 RESPONSE_FROM_CACHE
    List<Command> commands = Command.getCommandsWithResponseFromCache(groupKey, key);
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            assertTrue(distribution.getTotalCount() <= 1);
        }
    });
    for (Command cmd : commands) {
        cmd.observe();
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(1, stream.getLatest().getTotalCount());
    assertBetween(0, 30, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 4 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testSemaphoreRejectedCommandDoesNotGetLatencyTracked.

@Test
public void testSemaphoreRejectedCommandDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-F");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //10 commands with latency should occupy all semaphores.  execute those, then wait for bucket to roll
    //next command should be a semaphore rejection
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 200, HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() > 0) {
                assertBetween(200, 250, (int) distribution.getMean());
            }
        }
    });
    for (final Command cmd : commands) {
        //since these are blocking calls on the caller thread, we need a new caller thread for each command to actually get the desired concurrency
        new Thread(new HystrixContextRunnable(new Runnable() {

            @Override
            public void run() {
                cmd.observe();
            }
        })).start();
    }
    Command semaphoreRejected = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(40);
        semaphoreRejected.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(10, stream.getLatest().getTotalCount());
    assertBetween(200, 250, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(semaphoreRejected.isResponseSemaphoreRejected());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) HystrixContextRunnable(com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 5 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testMultipleBucketsBothGetStored.

@Test
public void testMultipleBucketsBothGetStored() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-H");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            if (distribution.getTotalCount() == 2) {
                assertBetween(55, 90, (int) distribution.getMean());
            }
            if (distribution.getTotalCount() == 5) {
                assertEquals(60, 90, (long) distribution.getMean());
            }
        }
    });
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 10);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.FAILURE, 100);
    cmd1.observe();
    cmd2.observe();
    try {
        Thread.sleep(500);
    } catch (InterruptedException ie) {
        fail("Interrupted ex");
    }
    Command cmd3 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd4 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 60);
    Command cmd5 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 70);
    cmd3.observe();
    cmd4.observe();
    cmd5.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertBetween(55, 90, stream.getLatestMean());
    assertBetween(10, 50, stream.getLatestPercentile(0.0));
    assertBetween(100, 150, stream.getLatestPercentile(100.0));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

CachedValuesHistogram (com.netflix.hystrix.metric.CachedValuesHistogram)12 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 Test (org.junit.Test)12 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)9 HystrixCollapserKey (com.netflix.hystrix.HystrixCollapserKey)3 ArrayList (java.util.ArrayList)3 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)1