public void TestWriteThreadSafety() { MockedTime time = new MockedTime(); HystrixRollingPercentile p = new HystrixRollingPercentile(time, 100, 25, 1000, true); int num_threads = 10; int num_iterations = 1000; CountdownEvent latch = new CountdownEvent(num_threads); Random r = new Random(); AtomicInteger added = new AtomicInteger(0); for (int i = 0; i < num_threads; i++) { Task t = new Task( () => { for (int j = 1; j < (num_iterations / num_threads) + 1; j++) { int nextInt = r.Next(100); p.AddValue(nextInt); added.GetAndIncrement(); } latch.SignalEx(); }, CancellationToken.None, TaskCreationOptions.LongRunning); t.Start(); } try { latch.Wait(TimeSpan.FromSeconds(100)); Assert.Equal(added.Value, p._buckets.PeekLast._data.Length); } catch (Exception) { Assert.True(false, "Timeout on all threads writing percentiles"); } }
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 Assert.Equal(1, counter._buckets.Size); // the count should be 1 Assert.Equal(1, counter._buckets.Last.GetAdder(type).Sum()); Assert.Equal(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 Assert.Equal(4, counter._buckets.Size); // the counts of the last bucket Assert.Equal(1, counter._buckets.Last.GetAdder(type).Sum()); // the total counts Assert.Equal(2, counter.GetRollingSum(type)); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestCumulativeCounterAfterRollingAndReset2() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(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._bucketSizeInMillseconds); } catch (Exception) { // ignore } 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(); } } // 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)); }
public void TestShortCircuited() { var time = new MockedTime(); try { var counter = new HystrixRollingNumber(time, 200, 10); // Increment counter.Increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 1 bucket Assert.Equal(1, counter._buckets.Size); // the count should be 1 Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum()); Assert.Equal(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 Assert.Equal(4, counter._buckets.Size); // the counts of the last bucket Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum()); // the total counts Assert.Equal(2, counter.GetRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestCumulativeCounterAfterRollingAndReset() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.SUCCESS; HystrixRollingNumber counter = new HystrixRollingNumber(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._bucketSizeInMillseconds); } catch (Exception) { // ignore } Assert.Equal(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 Assert.Equal(20, counter.GetCumulativeSum(type)); }
public void TestCreatesBuckets() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // confirm the initial settings Assert.Equal(200, counter._timeInMilliseconds); Assert.Equal(10, counter._numberOfBuckets); Assert.Equal(20, counter._bucketSizeInMillseconds); // we start out with 0 buckets in the queue Assert.Equal(0, counter._buckets.Size); // add a success in each interval which should result in all 10 buckets being created with 1 success in each for (int i = 0; i < counter._numberOfBuckets; i++) { counter.Increment(HystrixRollingNumberEvent.SUCCESS); time.Increment(counter._bucketSizeInMillseconds); } // confirm we have all 10 buckets Assert.Equal(10, counter._buckets.Size); // add 1 more and we should still only have 10 buckets since that's the max counter.Increment(HystrixRollingNumberEvent.SUCCESS); Assert.Equal(10, counter._buckets.Size); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestIncrementInMultipleBuckets() { 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); counter.Increment(HystrixRollingNumberEvent.TIMEOUT); counter.Increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds * 3); // Increment counter.Increment(HystrixRollingNumberEvent.SUCCESS); counter.Increment(HystrixRollingNumberEvent.SUCCESS); counter.Increment(HystrixRollingNumberEvent.FAILURE); counter.Increment(HystrixRollingNumberEvent.FAILURE); counter.Increment(HystrixRollingNumberEvent.FAILURE); counter.Increment(HystrixRollingNumberEvent.TIMEOUT); counter.Increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 4 buckets Assert.Equal(4, counter._buckets.Size); // the counts of the last bucket Assert.Equal(2, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SUCCESS).Sum()); Assert.Equal(3, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.FAILURE).Sum()); Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.TIMEOUT).Sum()); Assert.Equal(1, counter._buckets.Last.GetAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).Sum()); // the total counts Assert.Equal(6, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS)); Assert.Equal(5, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE)); Assert.Equal(3, counter.GetRollingSum(HystrixRollingNumberEvent.TIMEOUT)); Assert.Equal(2, counter.GetRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); // wait until window passes time.Increment(counter._timeInMilliseconds); // Increment counter.Increment(HystrixRollingNumberEvent.SUCCESS); // the total counts should now include only the last bucket after a reset since the window passed Assert.Equal(1, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS)); Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE)); Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.TIMEOUT)); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestRolling() { var time = new MockedTime(); var p = new HystrixRollingPercentile(time, TimeInMilliseconds, NumberOfBuckets, BucketDataLength, Enabled); p.AddValue(1000); p.AddValue(1000); p.AddValue(1000); p.AddValue(2000); Assert.Equal(1, p._buckets.Size); // no bucket turnover yet so percentile not yet generated Assert.Equal(0, p.GetPercentile(50)); time.Increment(6000); // still only 1 bucket until we touch it again Assert.Equal(1, p._buckets.Size); // a bucket has been created so we have a new percentile Assert.Equal(1000, p.GetPercentile(50)); // now 2 buckets since getting a percentile causes bucket retrieval Assert.Equal(2, p._buckets.Size); p.AddValue(1000); p.AddValue(500); // should still be 2 buckets Assert.Equal(2, p._buckets.Size); p.AddValue(200); p.AddValue(200); p.AddValue(1600); p.AddValue(200); p.AddValue(1600); p.AddValue(1600); // we haven't progressed to a new bucket so the percentile should be the same and ignore the most recent bucket Assert.Equal(1000, p.GetPercentile(50)); // Increment to another bucket so we include all of the above in the PercentileSnapshot time.Increment(6000); // the rolling version should have the same data as creating a snapshot like this var ps = new PercentileSnapshot(1000, 1000, 1000, 2000, 1000, 500, 200, 200, 1600, 200, 1600, 1600); Assert.Equal(ps.GetPercentile(0.15), p.GetPercentile(0.15)); Assert.Equal(ps.GetPercentile(0.50), p.GetPercentile(0.50)); Assert.Equal(ps.GetPercentile(0.90), p.GetPercentile(0.90)); Assert.Equal(ps.GetPercentile(0.995), p.GetPercentile(0.995)); output.WriteLine("100th: " + ps.GetPercentile(100) + " " + p.GetPercentile(100)); output.WriteLine("99.5th: " + ps.GetPercentile(99.5) + " " + p.GetPercentile(99.5)); output.WriteLine("99th: " + ps.GetPercentile(99) + " " + p.GetPercentile(99)); output.WriteLine("90th: " + ps.GetPercentile(90) + " " + p.GetPercentile(90)); output.WriteLine("50th: " + ps.GetPercentile(50) + " " + p.GetPercentile(50)); output.WriteLine("10th: " + ps.GetPercentile(10) + " " + p.GetPercentile(10)); // mean = 1000+1000+1000+2000+1000+500+200+200+1600+200+1600+1600/12 Assert.Equal(991, ps.Mean); }