/** * Get the max value of values in all buckets for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "max updater" type <code>HystrixRollingNumberEvent.isMaxUpdater() == true</code>. * * @param type * HystrixRollingNumberEvent defining which "max updater" to retrieve values from * @return max value for given {@link HystrixRollingNumberEvent} type during rolling window */ public long getRollingMaxValue(HystrixRollingNumberEvent type) { long values[] = getValues(type); if (values.length == 0) { return 0; } else { Arrays.sort(values); return values[values.length - 1]; } }
@Test public void testRolling() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.getCurrentBucket(); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); // System.out.println("Head: " + counter.buckets.state.get().head); // System.out.println("Tail: " + counter.buckets.state.get().tail); } }
@Test public void testCumulativeCounterAfterRolling() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.increment(type); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling assertEquals(20, counter.getCumulativeSum(type)); }
@Test public void testCumulativeCounterAfterRollingAndReset() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(time, 20, 2); assertEquals(0, counter.getCumulativeSum(type)); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { // first bucket counter.increment(type); try { time.increment(counter.bucketSizeInMillseconds); } catch (Exception e) { // ignore } assertEquals(2, counter.getValues(type).length); counter.getValueOfLatestBucket(type); if (i == 5 || i == 15) { // simulate a reset occurring every once in a while // so we ensure the absolute sum is handling it okay counter.reset(); } } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling assertEquals(20, counter.getCumulativeSum(type)); }
long values[] = counter.getValues(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE); assertEquals(10, values[0]); // oldest bucket assertEquals(0, values[1]);
long values[] = counter.getValues(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE); assertEquals(30, values[0]); // oldest bucket assertEquals(0, values[1]);
/** * Get the max value of values in all buckets for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "max updater" type <code>HystrixRollingNumberEvent.isMaxUpdater() == true</code>. * * @param type * HystrixRollingNumberEvent defining which "max updater" to retrieve values from * @return max value for given {@link HystrixRollingNumberEvent} type during rolling window */ public long getRollingMaxValue(HystrixRollingNumberEvent type) { long values[] = getValues(type); if (values.length == 0) { return 0; } else { Arrays.sort(values); return values[values.length - 1]; } }