public void TestMaxValue() { MockedTime time = new MockedTime(); try { HystrixRollingNumberEvent type = HystrixRollingNumberEvent.THREAD_MAX_ACTIVE; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); counter.UpdateRollingMax(type, 10); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds); counter.UpdateRollingMax(type, 30); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds); counter.UpdateRollingMax(type, 40); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds); counter.UpdateRollingMax(type, 15); Assert.Equal(40, counter.GetRollingMaxValue(type)); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestValueIsZeroAfterRollingWindowPassesAndNoTraffic() { 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); p.AddValue(4000); 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(1500, p.GetPercentile(50)); // let 1 minute pass time.Increment(60000); // no data in a minute should mean all buckets are empty (or reset) so we should not have any percentiles Assert.Equal(0, p.GetPercentile(50)); }
public void TestCounterRetrievalRefreshesBuckets() { 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); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds * 3); // we should have 1 bucket since nothing has triggered the update of buckets in the elapsed time Assert.Equal(1, counter._buckets.Size); // the total counts Assert.Equal(4, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS)); Assert.Equal(2, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE)); // we should have 4 buckets as the counter 'gets' should have triggered the buckets being created to fill in time Assert.Equal(4, counter._buckets.Size); // wait until window passes time.Increment(counter._timeInMilliseconds); // the total counts should all be 0 (and the buckets cleared by the get, not only Increment) Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.SUCCESS)); Assert.Equal(0, counter.GetRollingSum(HystrixRollingNumberEvent.FAILURE)); // 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)); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
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 TestSampleDataOverTime2() { output.WriteLine("\n\n***************************** testSampleDataOverTime2 \n"); var time = new MockedTime(); int previousTime = 0; var p = new HystrixRollingPercentile(time, TimeInMilliseconds, NumberOfBuckets, BucketDataLength, Enabled); for (int i = 0; i < SampleDataHolder2.Data.Length; i++) { int timeInMillisecondsSinceStart = SampleDataHolder2.Data[i][0]; int latency = SampleDataHolder2.Data[i][1]; time.Increment(timeInMillisecondsSinceStart - previousTime); previousTime = timeInMillisecondsSinceStart; p.AddValue(latency); } output.WriteLine("0.01: " + p.GetPercentile(0.01)); output.WriteLine("Median: " + p.GetPercentile(50)); output.WriteLine("90th: " + p.GetPercentile(90)); output.WriteLine("99th: " + p.GetPercentile(99)); output.WriteLine("99.5th: " + p.GetPercentile(99.5)); output.WriteLine("99.99: " + p.GetPercentile(99.99)); if (p.GetPercentile(50) > 90 || p.GetPercentile(50) < 50) { Assert.True(false, "We expect around 60-70 but got: " + p.GetPercentile(50)); } if (p.GetPercentile(99) < 400) { Assert.True(false, "We expect to see some high values over 400 but got: " + p.GetPercentile(99)); } }
public void TestEmptyBucketsFillIn() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // add 1 counter.Increment(HystrixRollingNumberEvent.SUCCESS); // we should have 1 bucket Assert.Equal(1, counter._buckets.Size); // wait past 3 bucket time periods (the 1st bucket then 2 empty ones) time.Increment(counter._bucketSizeInMillseconds * 3); // add another counter.Increment(HystrixRollingNumberEvent.SUCCESS); // we should have 4 (1 + 2 empty + 1 new one) buckets Assert.Equal(4, counter._buckets.Size); } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestCumulativeCounterAfterRolling() { 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); } // cumulative count should be 20 (for the number of loops above) regardless of buckets rolling Assert.Equal(20, counter.GetCumulativeSum(type)); }
public void TestCumulativeCounterAfterRollingAndReset3() { 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 } } // 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)); }
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) { // ignore } Assert.Equal(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); } }
public void TestThreadSafety() { var time = new MockedTime(); var p = new HystrixRollingPercentile(time, 100, 25, 1000, true); int num_threads = 1000; // .NET Core StackOverflow int num_iterations = 1000000; var latch = new CountdownEvent(num_threads); var aggregateMetrics = new AtomicInteger(); // same as a blackhole var r = new Random(); var cts = new CancellationTokenSource(); var metricsPoller = Task.Run(() => { while (!cts.Token.IsCancellationRequested) { aggregateMetrics.AddAndGet(p.Mean + p.GetPercentile(10) + p.GetPercentile(50) + p.GetPercentile(90)); } }); for (int i = 0; i < num_threads; i++) { int threadId = i; Task.Run(() => { for (int j = 1; j < (num_iterations / num_threads) + 1; j++) { int nextInt = r.Next(100); p.AddValue(nextInt); if (threadId == 0) { time.Increment(1); } } latch.SignalEx(); }); } try { latch.Wait(TimeSpan.FromSeconds(100)); cts.Cancel(); } catch (Exception) { Assert.True(false, "Timeout on all threads writing percentiles"); } aggregateMetrics.AddAndGet(p.Mean + p.GetPercentile(10) + p.GetPercentile(50) + p.GetPercentile(90)); output.WriteLine(p.Mean + " : " + p.GetPercentile(50) + " : " + p.GetPercentile(75) + " : " + p.GetPercentile(90) + " : " + p.GetPercentile(95) + " : " + p.GetPercentile(99)); }
public void TestUpdateMax2() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // Increment counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 10); counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 20); // we should have 1 bucket Assert.Equal(1, counter._buckets.Size); // the count should be 30 Assert.Equal(30, counter._buckets.Last.GetMaxUpdater(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE).Max); Assert.Equal(30, counter.GetRollingMaxValue(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE)); // sleep to get to a new bucket time.Increment(counter._bucketSizeInMillseconds * 3); counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 30); counter.UpdateRollingMax(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE, 50); // we should have 4 buckets Assert.Equal(4, counter._buckets.Size); // the count Assert.Equal(50, counter._buckets.Last.GetMaxUpdater(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE).Max); Assert.Equal(50, counter.GetValueOfLatestBucket(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE)); // values per bucket long[] values = counter.GetValues(HystrixRollingNumberEvent.THREAD_MAX_ACTIVE); Assert.Equal(30, values[0]); // oldest bucket Assert.Equal(0, values[1]); Assert.Equal(0, values[2]); Assert.Equal(50, values[3]); // latest bucket } catch (Exception e) { output.WriteLine(e.ToString()); Assert.True(false, "Exception: " + e.Message); } }
public void TestDoesNothingWhenDisabled() { MockedTime time = new MockedTime(); int previousTime = 0; HystrixRollingPercentile p = new HystrixRollingPercentile(time, TimeInMilliseconds, NumberOfBuckets, BucketDataLength, false); for (int i = 0; i < SampleDataHolder2.Data.Length; i++) { int timeInMillisecondsSinceStart = SampleDataHolder2.Data[i][0]; int latency = SampleDataHolder2.Data[i][1]; time.Increment(timeInMillisecondsSinceStart - previousTime); previousTime = timeInMillisecondsSinceStart; p.AddValue(latency); } Assert.Equal(-1, p.GetPercentile(50)); Assert.Equal(-1, p.GetPercentile(75)); Assert.Equal(-1, p.Mean); }
public void TestSampleDataOverTime1() { output.WriteLine("\n\n***************************** testSampleDataOverTime1 \n"); var time = new MockedTime(); var p = new HystrixRollingPercentile(time, TimeInMilliseconds, NumberOfBuckets, BucketDataLength, Enabled); int previousTime = 0; for (int i = 0; i < SampleDataHolder1.Data.Length; i++) { int timeInMillisecondsSinceStart = SampleDataHolder1.Data[i][0]; int latency = SampleDataHolder1.Data[i][1]; time.Increment(timeInMillisecondsSinceStart - previousTime); previousTime = timeInMillisecondsSinceStart; p.AddValue(latency); } output.WriteLine("0.01: " + p.GetPercentile(0.01)); output.WriteLine("Median: " + p.GetPercentile(50)); output.WriteLine("90th: " + p.GetPercentile(90)); output.WriteLine("99th: " + p.GetPercentile(99)); output.WriteLine("99.5th: " + p.GetPercentile(99.5)); output.WriteLine("99.99: " + p.GetPercentile(99.99)); output.WriteLine("Median: " + p.GetPercentile(50)); output.WriteLine("Median: " + p.GetPercentile(50)); output.WriteLine("Median: " + p.GetPercentile(50)); /* * In a loop as a use case was found where very different values were calculated in subsequent requests. */ for (int i = 0; i < 10; i++) { if (p.GetPercentile(50) > 5) { Assert.True(false, "We expect around 2 but got: " + p.GetPercentile(50)); } if (p.GetPercentile(99.5) < 20) { Assert.True(false, "We expect to see some high values over 20 but got: " + p.GetPercentile(99.5)); } } }
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); } }
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 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 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); }