using System.Collections.Generic;
using Polly.Contrib.WaitAndRetry;
public static void Main()
var classUnderTest = new MeasureBackoff();
MedianJitterBackoff(1, 4, true);
MedianJitterBackoff(1, 4, false);
MedianJitterBackoff(0.5, 5, true);
MedianJitterBackoff(1, 5, true);
MedianJitterBackoff(1, 5, false);
MedianJitterBackoff(2, 6, false);
Console.WriteLine("--- SUMMARY ---------------------");
Console.WriteLine(_summary);
System.Text.StringBuilder _summary = new();
public void MedianJitterBackoff(double medianWaitSec, int retryCount, bool firstFast)
string scenario = $"{medianWaitSec} sec, retryCount: {retryCount}, firstFast: {firstFast}";
Console.WriteLine(scenario);
int testIterationCount = 20;
for (int i = 0; i < testIterationCount; ++i)
var delays = Backoff.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromSeconds(medianWaitSec),
foreach (TimeSpan delay in delays)
retryDelay += delay.TotalSeconds;
totalDelay += delay.TotalSeconds;
Console.Write($"\t{delay}");
Console.WriteLine($"\tsum: {retryDelay:0.00} sec");
double median = totalDelay / testIterationCount;
_summary.AppendLine($"{scenario} median wait: {median:0.00}");
string scenario = $"our one";
Console.WriteLine(scenario);
int testIterationCount = 2;
for (int i = 0; i < testIterationCount; ++i)
var delays = Enumerable.Range(0, 5).Select(retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
foreach (TimeSpan delay in delays)
retryDelay += delay.TotalSeconds;
totalDelay += delay.TotalSeconds;
Console.Write($"\t{delay}");
Console.WriteLine($"\tsum: {retryDelay:0.00} sec");
double median = totalDelay / testIterationCount;
_summary.AppendLine($"{scenario} median wait: {median:0.00}");