private void RunSamplerTest( ISampler sampler, int iterations, float expectedAutoKeepRate, float expectedUserKeepRate, float acceptableVariancePercent) { var sampleSize = iterations; var autoKeeps = 0; var userKeeps = 0; int seed = new Random().Next(); var idGenerator = new SpanIdGenerator(seed); while (sampleSize-- > 0) { var traceId = idGenerator.CreateNew(); var span = GetMyServiceSpan(traceId); var priority = sampler.GetSamplingPriority(span); if (priority == SamplingPriorityValues.AutoKeep) { autoKeeps++; } else if (priority == SamplingPriorityValues.UserKeep) { userKeeps++; } } // AUTO_KEEP var autoKeepRate = autoKeeps / (float)iterations; var autoKeepRateLowerLimit = expectedAutoKeepRate * (1 - acceptableVariancePercent); var autoKeepRateUpperLimit = expectedAutoKeepRate * (1 + acceptableVariancePercent); Assert.True( autoKeepRate >= autoKeepRateLowerLimit && autoKeepRate <= autoKeepRateUpperLimit, $"Sampling AUTO_KEEP rate expected between {autoKeepRateLowerLimit} and {autoKeepRateUpperLimit}, actual rate is {autoKeepRate}. Random generator seeded with {seed}."); // USER_KEEP (aka MANUAL_KEEP) var userKeepRate = userKeeps / (float)iterations; var userKeepRateLowerLimit = expectedUserKeepRate * (1 - acceptableVariancePercent); var userKeepRateUpperLimit = expectedUserKeepRate * (1 + acceptableVariancePercent); Assert.True( userKeepRate >= userKeepRateLowerLimit && userKeepRate <= userKeepRateUpperLimit, $"Sampling USER_KEEP rate expected between {userKeepRateLowerLimit} and {userKeepRateUpperLimit}, actual rate is {userKeepRate}. Random generator seeded with {seed}."); }