ThrottlePolicy { public int calcNewMaxInFlight(double maxPerformanceChange, int numOk, int previousNumOk, int previousMaxInFlight, int maxInFlightNow, boolean messagesQueued) { double difference = calculateRuleBasedDifference(maxPerformanceChange, numOk, previousNumOk, previousMaxInFlight, maxInFlightNow); boolean previousRunWasBetter = numOk < previousNumOk; boolean previousRunHadLessInFlight = previousMaxInFlight < maxInFlightNow; int delta; if (previousRunWasBetter == previousRunHadLessInFlight) { delta = (int) (-1.1 * difference * maxInFlightNow); } else { delta = (int) (difference * maxInFlightNow); } if (abs(delta) < 2) { delta = -3; } if (!messagesQueued && delta > 0) { delta = -2; } return maxInFlightNow + delta; }  int calcNewMaxInFlight(double maxPerformanceChange, int numOk, int previousNumOk, int previousMaxInFlight,
                                  int maxInFlightNow, boolean messagesQueued); static final double SMALL_DIFFERENCE_IN_SUCCESSES_RATIO; }
@Test public void samePerformanceShouldTuneDown() { assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(95)); }
@Test public void improvedPerformanceSameSizeShouldTuneDown() { numOk += 200; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(89)); }
@Test public void improvedPerformanceSmallerSizeTuneDownFurther() { numOk += 200; max = 70; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(63)); }
@Test public void improvedPerformanceLargerSizeIncrease() { numOk += 200; max = 130; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(143)); dynamicFactor = 100; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(156)); }
@Test public void improvedPerformanceLargerSizeButQueuedFalse() { numOk += 200; max = 130; queued = false; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(128)); }
@Test public void lowerPerformanceSameSizeShouldIncrease() { numOk -= 200; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(110)); }
@Test public void lowerPerformanceSmallerSizeShouldIncreaseSize() { numOk -= 200; max = 30; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(33)); dynamicFactor = 100; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(36)); }
@Test public void lowerPerformanceLargerSizeTuneDownFurther() { numOk -= 200; max = 130; assertThat(throttlePolicy.calcNewMaxInFlight(dynamicFactor, numOk, prevOk, prevMax, max, queued), is(116)); }