public void testMaxValue() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.THREAD_MAX_ACTIVE; RollingNumber counter = new RollingNumber(time, 200, 10); counter.UpdateRollingMax(type, 10); // sleep to get to a new bucket time.Increment(counter.BucketSizeInMs); counter.UpdateRollingMax(type, 30); // sleep to get to a new bucket time.Increment(counter.BucketSizeInMs); counter.UpdateRollingMax(type, 40); // sleep to get to a new bucket time.Increment(counter.BucketSizeInMs); counter.UpdateRollingMax(type, 15); Assert.Equal(40, counter.GetRollingMaxValue(type)); }
public void testCumulativeCounterAfterRolling() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.SUCCESS; RollingNumber counter = new RollingNumber(time, 20, 2); Assert.Equal(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.BucketSizeInMs); } catch (Exception) { // ignore } counter.GetValueOfLatestBucket(type); Assert.Equal(2, counter.GetValues(type).Count()); } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling Assert.Equal(20, counter.GetCumulativeSum(type)); }
public void testRolling() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.THREAD_MAX_ACTIVE; RollingNumber counter = new RollingNumber(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.BucketSizeInMs); } catch (Exception) { // ignore } counter.GetValueOfLatestBucket(type); Assert.Equal(2, counter.GetValues(type).Count()); // System.out.println("Head: " + counter.buckets.state.get().head); // System.out.println("Tail: " + counter.buckets.state.get().tail); } }
public void testCumulativeCounterAfterRollingAndReset3() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.SUCCESS; RollingNumber counter = new RollingNumber(time, 20, 2); Assert.Equal(0, counter.GetCumulativeSum(type)); counter.Increment(type); counter.Increment(type); counter.Increment(type); // iterate over 20 buckets on a queue sized for 2 for (int i = 0; i < 20; i++) { try { time.Increment(counter.BucketSizeInMs); } catch (Exception) { // ignore } } // since we are rolling over the buckets it should reset naturally // no increments during the loop, just some before and after counter.Increment(type); counter.Increment(type); // cumulative count should be 5 regardless of buckets rolling Assert.Equal(5, counter.GetCumulativeSum(type)); }
private void testCounterType(RollingNumberEvent type) { MockedClock time = new MockedClock(); RollingNumber counter = new RollingNumber(time, 200, 10); // increment counter.Increment(type); // we should have 1 bucket var buckets = counter.GetBuckets().ToArray(); Assert.Equal(1, buckets.Length); // the count should be 1 Assert.Equal(1, buckets.First().GetAdder(type)); Assert.Equal(1, counter.GetRollingSum(type)); // sleep to get to a new bucket time.Increment(counter.BucketSizeInMs * 3); // increment again in latest bucket counter.Increment(type); // we should have 2 buckets buckets = counter.GetBuckets().ToArray(); Assert.Equal(2, buckets.Length); // the counts of the last bucket Assert.Equal(1, buckets.First().GetAdder(type)); // the total counts Assert.Equal(2, counter.GetRollingSum(type)); }
public void testEmptySum() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.SUCCESS; RollingNumber counter = new RollingNumber(time, 200, 10); Assert.Equal(0, counter.GetRollingSum(type)); }
public void testEmptyLatestValue() { MockedClock time = new MockedClock(); RollingNumberEvent type = RollingNumberEvent.THREAD_MAX_ACTIVE; RollingNumber counter = new RollingNumber(time, 200, 10); Assert.Equal(0, counter.GetValueOfLatestBucket(type)); }
internal long GetValueOfLatestBucket(RollingNumberEvent ev) { Bucket lastBucket = GetCurrentBucket(); if (lastBucket == null) return 0; // we have bucket data so we'll return the lastBucket if ((int)ev > (int)RollingNumberEvent.MAX_COUNTER) return lastBucket.GetMaxUpdater(ev); else return lastBucket.GetAdder(ev); }
public void UpdateMaxMax(RollingNumberEvent ev, long value) { long max; do { max = maxAdders[(int)ev]; if (value <= max) { return; } }while (Interlocked.CompareExchange(ref maxAdders[(int)ev], value, max) != max); }
internal IEnumerable <long> GetValues(RollingNumberEvent ev, long startWindowTime = 0) { foreach (var b in GetBuckets(startWindowTime)) { if ((int)ev < (int)RollingNumberEvent.MAX_COUNTER) { yield return(b.adders[(int)ev]); } else { yield return(b.maxAdders[(int)ev]); } } }
internal long GetValueOfLatestBucket(RollingNumberEvent ev) { Bucket lastBucket = GetCurrentBucket(); if (lastBucket == null) { return(0); } // we have bucket data so we'll return the lastBucket if ((int)ev > (int)RollingNumberEvent.MAX_COUNTER) { return(lastBucket.GetMaxUpdater(ev)); } else { return(lastBucket.GetAdder(ev)); } }
public long GetAdder(RollingNumberEvent ev) { return this.adders[(int)ev]; }
public void UpdateMaxMax(RollingNumberEvent ev, long value) { long max; do { max = maxAdders[(int)ev]; if (value <= max) return; } while (Interlocked.CompareExchange(ref maxAdders[(int)ev], value, max) != max); }
public long GetAdder(RollingNumberEvent ev) { return(this.adders[(int)ev]); }
/// <summary> /// Gets the cumulative value for the specified <see cref="RollingNumberEvent"/>. /// (Returns <see cref="LongAdder.Sum()"/> for Counter types and <see cref="LongMaxUpdater.Max()"/> for MaxUpdater types.) /// </summary> /// <param name="type">The specified event.</param> /// <returns>The cumulative value for the specified event.</returns> public long Get(RollingNumberEvent type) { if (type.IsCounter()) { return this.adderForCounterType[(int)type].Sum(); } else if (type.IsMaxUpdater()) { return this.updaterForCounterType[(int)type].Max(); } else { throw new ArgumentException(string.Format("Unknown type of event: {0}", type), "type"); } }
public long GetCumulativeSum(RollingNumberEvent ev) { return(GetCurrentBucket().GetAdder(ev) + cumulativeSum.GetAdder(ev)); }
/// <summary> /// Get the max value of values in all buckets for the given <see cref="RollingNumberEvent"/> type. /// The <see cref="RollingNumberEvent"/> must be a "MaxUpdater" type (HystrixRollingNumberEvent.IsMaxUpdater() == true). /// </summary> /// <param name="type">HystrixRollingNumberEvent defining which "MaxUpdater" to retrieve values from</param> /// <returns>Max value for given <see cref="RollingNumberEvent"/> type during rolling window</returns> public long GetRollingMaxValue(RollingNumberEvent type) { long[] values = this.GetValues(type); if (values.Length == 0) { return 0; } else { Array.Sort(values); return values[values.Length - 1]; } }
public void Increment(RollingNumberEvent ev) { Interlocked.Increment(ref adders[(int)ev]); }
public void UpdateRollingMax(RollingNumberEvent ev, long value) { GetCurrentBucket().UpdateMaxMax(ev, value); }
/// <summary> /// Get the value of the latest (current) bucket in the rolling counter for the given <see cref="RollingNumberEvent"/> type. /// The <see cref="RollingNumberEvent"/> must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true). /// </summary> /// <param name="type">HystrixRollingNumberEvent defining which counter to retrieve value from</param> /// <returns>value from latest bucket for given <see cref="RollingNumberEvent"/> counter type</returns> public long GetValueOfLatestBucket(RollingNumberEvent type) { Bucket lastBucket = this.GetCurrentBucket(); if (lastBucket == null) { return 0; } // we have bucket data so we'll return the lastBucket return lastBucket.Get(type); }
public long GetCumulativeCount(RollingNumberEvent ev) { return(this._counter.GetCumulativeSum(ev)); }
/// <summary> /// Get an array of values for all buckets in the rolling counter for the given <see cref="RollingNumberEvent"/> type. /// Index 0 is the oldest bucket. /// The <see cref="RollingNumberEvent"/> must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true). /// </summary> /// <param name="type">HystrixRollingNumberEvent defining which counter to retrieve values from</param> /// <returns>Array of values from each of the rolling buckets for given <see cref="RollingNumberEvent"/> counter type</returns> public long[] GetValues(RollingNumberEvent type) { if (this.GetCurrentBucket() == 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 = this.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; foreach (Bucket bucket in bucketArray) { if (type.IsCounter()) { values[i++] = bucket.GetAdder(type).Sum(); } else if (type.IsMaxUpdater()) { values[i++] = bucket.GetMaxUpdater(type).Max(); } } return values; }
/// <summary> /// Increment the counter in the current bucket by one for the given <see cref="RollingNumberEvent"/> type. /// </summary> /// <param name="type">Defining which counter to increment, must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).</param> public void Increment(RollingNumberEvent type) { this.GetCurrentBucket().GetAdder(type).Increment(); }
/// <summary> /// Update a value and retain the max value. /// </summary> /// <param name="type">Defining which counter to update, must be a "MaxUpdater" type (HystrixRollingNumberEvent.IsMaxUpdater() == true).</param> /// <param name="value">Value to be updated to the current bucket</param> public void UpdateRollingMax(RollingNumberEvent type, long value) { this.GetCurrentBucket().GetMaxUpdater(type).Update(value); }
public void Increment(RollingNumberEvent ev) { GetCurrentBucket().Increment(ev); }
public long GetCumulativeCount(RollingNumberEvent ev) { return this._counter.GetCumulativeSum(ev); }
/// <summary> /// Get the sum of all buckets in the rolling counter for the given <see cref="RollingNumberEvent"/> type. /// The <see cref="RollingNumberEvent"/> must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true). /// </summary> /// <param name="type">defining which counter to retrieve values from</param> /// <returns>Value from the given <see cref="RollingNumberEvent"/> counter type.</returns> public long GetRollingSum(RollingNumberEvent type) { if (this.GetCurrentBucket() == null) { return 0; } long sum = 0; foreach (Bucket b in this.buckets) { sum += b.GetAdder(type).Sum(); } return sum; }
/// <summary> /// Get the cumulative sum of all buckets ever since the application started without rolling for the given /// <see cref="RollingNumberEvent"/> type. See <see cref="GetRollingSum(RollingNumberEvent)"/> for the rolling sum. /// </summary> /// <param name="type">Must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).</param> /// <returns>Cumulative sum of all increments and adds for the given <see cref="RollingNumberEvent"/> counter type.</returns> public long GetCumulativeSum(RollingNumberEvent type) { // This isn't 100% atomic since multiple threads can be affecting latestBucket & cumulativeSum independently // but that's okay since the count is always a moving target and we're accepting a "point in time" best attempt // we are however putting 'GetValueOfLatestBucket' first since it can have side-affects on cumulativeSum whereas the inverse is not true return this.GetValueOfLatestBucket(type) + this.cumulativeSum.Get(type); }
public long GetRollingMaxValue(RollingNumberEvent ev, long startWindowTime = 0L) { GetCurrentBucket(); return(GetValues(ev, startWindowTime).Max()); }
internal IEnumerable<long> GetValues(RollingNumberEvent ev, long startWindowTime=0) { foreach(var b in GetBuckets(startWindowTime)) { if ((int)ev < (int)RollingNumberEvent.MAX_COUNTER) yield return b.adders[(int)ev]; else yield return b.maxAdders[(int)ev]; } }
public long GetMaxUpdater(RollingNumberEvent ev) { return(this.maxAdders[(int)ev]); }
public long GetMaxUpdater(RollingNumberEvent ev) { return this.maxAdders[(int)ev]; }
public long GetRollingCount(RollingNumberEvent ev) { return(this._counter.GetRollingSum(ev)); }
/// <summary> /// Gets the <see cref="LongMaxUpdater"/> instance for the specified event. /// </summary> /// <param name="type">The specified event.</param> /// <returns>The <see cref="LongMaxUpdater"/> instance for the specified event.</returns> public LongMaxUpdater GetMaxUpdater(RollingNumberEvent type) { if (!type.IsMaxUpdater()) { throw new ArgumentException(string.Format("Type is not a MaxUpdater: {0}", type), "type"); } return this.updaterForCounterType[(int)type]; }
public long GetCumulativeSum(RollingNumberEvent ev) { return GetCurrentBucket().GetAdder(ev) + cumulativeSum.GetAdder(ev); }
public long GetRollingMaxValue(RollingNumberEvent ev, long startWindowTime = 0L) { GetCurrentBucket(); return GetValues(ev, startWindowTime).Max(); }
public long GetRollingCount(RollingNumberEvent ev) { return this._counter.GetRollingSum(ev); }
/// <summary> /// Add to the counter in the current bucket for the given <see cref="RollingNumberEvent"/> type. /// </summary> /// <param name="type">Defining which counter to add to, must be a "Counter" type (HystrixRollingNumberEvent.IsCounter() == true).</param> /// <param name="value">Value to be added to the current bucket.</param> public void Add(RollingNumberEvent type, long value) { this.GetCurrentBucket().GetAdder(type).Add(value); }