@Test
    void singlePolicyWithSmallWindows() {
        long operations = 1_000_000;
        int numberOfWorkers = 1;
        int maximumTasksPerWorker = 16;
        int workerParallelism = 12;

        { // This setup is lucky with the artificial local maxima for latency, and gives good results. See below for counter-examples.
            int workPerSuccess = 8;

            ManualTimer timer = new ManualTimer();
            DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
                    .setWindowSizeIncrement(0.1)
                    .setResizeRate(100);
            Summary summary = run(operations, workPerSuccess, numberOfWorkers, maximumTasksPerWorker, workerParallelism, timer, policy);

            double minMaxPending = numberOfWorkers * workerParallelism;
            double maxMaxPending = numberOfWorkers * maximumTasksPerWorker;
            System.err.println(operations / (double) timer.milliTime());
            assertInRange(minMaxPending, summary.averagePending, maxMaxPending);
            assertInRange(minMaxPending, summary.averageWindows[0], maxMaxPending);
            assertInRange(1, summary.inefficiency, 1.1);
            assertInRange(0, summary.waste, 0.01);
        }

        { // This setup is not so lucky, and the artificial behaviour pushes it into overload.
            int workPerSuccess = 5;

            ManualTimer timer = new ManualTimer();
            DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
                    .setWindowSizeIncrement(0.1)
                    .setResizeRate(100);
            Summary summary = run(operations, workPerSuccess, numberOfWorkers, maximumTasksPerWorker, workerParallelism, timer, policy);

            double maxMaxPending = numberOfWorkers * maximumTasksPerWorker;
            assertInRange(maxMaxPending, summary.averagePending, maxMaxPending * 1.1);
            assertInRange(maxMaxPending, summary.averageWindows[0], maxMaxPending * 1.1);
            assertInRange(1.2, summary.inefficiency, 1.5);
            assertInRange(0.5, summary.waste, 1.5);
        }

        { // This setup is not so lucky either, and the artificial behaviour keeps it far below a good throughput.
            int workPerSuccess = 4;

            ManualTimer timer = new ManualTimer();
            DynamicThrottlePolicy policy = new DynamicThrottlePolicy(timer).setMinWindowSize(1)
                    .setWindowSizeIncrement(0.1)
                    .setResizeRate(100);
            Summary summary = run(operations, workPerSuccess, numberOfWorkers, maximumTasksPerWorker, workerParallelism, timer, policy);

            double minMaxPending = numberOfWorkers * workerParallelism;
            assertInRange(0.3 * minMaxPending, summary.averagePending, 0.5 * minMaxPending);
            assertInRange(0.3 * minMaxPending, summary.averageWindows[0], 0.5 * minMaxPending);
            assertInRange(2, summary.inefficiency, 4);
            assertInRange(0, summary.waste, 0);
        }
    }
These tests are based on a simulated server, the {@link MockServer} below.
The purpose is to both verify the behaviour of the algorithm, while also providing a playground
for development, tuning, etc..

@author jonmv
messagebus/src/test/java/com/yahoo/messagebus/DynamicThrottlePolicyTest.java