private void testCounterType(HystrixRollingNumberEvent type) { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(type); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(type).sum()); assertEquals(1, counter.getRollingSum(type)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // increment again in latest bucket counter.increment(type); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(type).sum()); // the total counts assertEquals(2, counter.getRollingSum(type)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
@Test public void testShortCircuited() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).sum()); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // incremenet again in latest bucket counter.increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).sum()); // the total counts assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
@Test public void testTimeout() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // incremenet again in latest bucket counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); // the total counts assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
/** * Get an array of values for all buckets in the rolling counter for the given {@link HystrixRollingNumberEvent} type. * <p> * Index 0 is the oldest bucket. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to retrieve values from * @return array of values from each of the rolling buckets for given {@link HystrixRollingNumberEvent} counter type */ public long[] getValues(HystrixRollingNumberEvent type) { Bucket lastBucket = getCurrentBucket(); if (lastBucket == null) return new long[0]; // get buckets as an array (which is a copy of the current state at this point in time) Bucket[] bucketArray = buckets.getArray(); // we have bucket data so we'll return an array of values for all buckets long values[] = new long[bucketArray.length]; int i = 0; for (Bucket bucket : bucketArray) { if (type.isCounter()) { values[i++] = bucket.getAdder(type).sum(); } else if (type.isMaxUpdater()) { values[i++] = bucket.getMaxUpdater(type).max(); } } return values; }
/** * Get the sum of all buckets in the rolling counter for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to retrieve values from * @return * value from the given {@link HystrixRollingNumberEvent} counter type */ public long getRollingSum(HystrixRollingNumberEvent type) { Bucket lastBucket = getCurrentBucket(); if (lastBucket == null) return 0; long sum = 0; for (Bucket b : buckets) { sum += b.getAdder(type).sum(); } return sum; }
/** * Add to the counter in the current bucket for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to add to * @param value * long value to be added to the current bucket */ public void add(HystrixRollingNumberEvent type, long value) { getCurrentBucket().getAdder(type).add(value); }
/** * Increment the counter in the current bucket by one for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to increment */ public void increment(HystrixRollingNumberEvent type) { getCurrentBucket().getAdder(type).increment(); }
public void addBucket(Bucket lastBucket) { for (HystrixRollingNumberEvent type : HystrixRollingNumberEvent.values()) { if (type.isCounter()) { getAdder(type).add(lastBucket.getAdder(type).sum()); } if (type.isMaxUpdater()) { getMaxUpdater(type).update(lastBucket.getMaxUpdater(type).max()); } } }
assertEquals(2, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SUCCESS).sum()); assertEquals(3, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.FAILURE).sum()); assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).sum());
/** * Get an array of values for all buckets in the rolling counter for the given {@link HystrixRollingNumberEvent} type. * <p> * Index 0 is the oldest bucket. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to retrieve values from * @return array of values from each of the rolling buckets for given {@link HystrixRollingNumberEvent} counter type */ public long[] getValues(HystrixRollingNumberEvent type) { Bucket lastBucket = getCurrentBucket(); if (lastBucket == null) return new long[0]; // get buckets as an array (which is a copy of the current state at this point in time) Bucket[] bucketArray = buckets.getArray(); // we have bucket data so we'll return an array of values for all buckets long values[] = new long[bucketArray.length]; int i = 0; for (Bucket bucket : bucketArray) { if (type.isCounter()) { values[i++] = bucket.getAdder(type).sum(); } else if (type.isMaxUpdater()) { values[i++] = bucket.getMaxUpdater(type).max(); } } return values; }
@Test public void testIncrementInSingleBucket() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.SUCCESS); counter.increment(HystrixRollingNumberEvent.FAILURE); counter.increment(HystrixRollingNumberEvent.FAILURE); counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 4 assertEquals(4, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SUCCESS).sum()); assertEquals(2, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.FAILURE).sum()); assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
/** * Get the sum of all buckets in the rolling counter for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to retrieve values from * @return * value from the given {@link HystrixRollingNumberEvent} counter type */ public long getRollingSum(HystrixRollingNumberEvent type) { Bucket lastBucket = getCurrentBucket(); if (lastBucket == null) return 0; long sum = 0; for (Bucket b : buckets) { sum += b.getAdder(type).sum(); } return sum; }
/** * Add to the counter in the current bucket for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to add to * @param value * long value to be added to the current bucket */ public void add(HystrixRollingNumberEvent type, long value) { getCurrentBucket().getAdder(type).add(value); }
public void addBucket(Bucket lastBucket) { for (HystrixRollingNumberEvent type : HystrixRollingNumberEvent.values()) { if (type.isCounter()) { getAdder(type).add(lastBucket.getAdder(type).sum()); } if (type.isMaxUpdater()) { getMaxUpdater(type).update(lastBucket.getMaxUpdater(type).max()); } } }
/** * Increment the counter in the current bucket by one for the given {@link HystrixRollingNumberEvent} type. * <p> * The {@link HystrixRollingNumberEvent} must be a "counter" type <code>HystrixRollingNumberEvent.isCounter() == true</code>. * * @param type * HystrixRollingNumberEvent defining which counter to increment */ public void increment(HystrixRollingNumberEvent type) { getCurrentBucket().getAdder(type).increment(); }