public void DistributionShouldBeUniform(double rate) { const int total = 1_000_000; var startCheckingAfter = Convert.ToInt32(total * 0.1); // i.e., after 10% const double allowedDiffInRate = 0.01; var sampledCount = 0; var noopLogger = new NoopLogger(); var currentExecutionSegmentsContainer = new NoopCurrentExecutionSegmentsContainer(); var noopPayloadSender = new NoopPayloadSender(); var configurationReader = new MockConfigSnapshot(noopLogger); var sampler = new Sampler(rate); total.Repeat(i => { // reset current activity, otherwise all transactions share the same traceid which influences the sampling decision Activity.Current = null; var transaction = new Transaction(noopLogger, "test transaction name", "test transaction type", sampler, /* distributedTracingData: */ null, noopPayloadSender, configurationReader, currentExecutionSegmentsContainer); if (transaction.IsSampled) { ++sampledCount; } // ReSharper disable once InvertIf if (i + 1 >= startCheckingAfter) { var actualRate = (double)sampledCount / (i + 1); var diffInRate = actualRate - rate; Assert.True(Math.Abs(diffInRate) <= allowedDiffInRate, "Abs(diffInRate) should be <= allowedDiffInRate. " + $"diffInRate: {diffInRate}, allowedDiffInRate: {allowedDiffInRate}, " + $"i: {i}, " + $"actual rate: {actualRate}, expected rate: {rate}, " + $"actual sampled count: {sampledCount}, expected sampled count: {Convert.ToInt32((i + 1) * rate)}" ); } }); }
public void DistributionShouldBeUniform(double rate) { const int total = 1_000_000; var startCheckingAfter = Convert.ToInt32(total * 0.1); // i.e., after 10% const double allowedDiffInRate = 0.01; var sampledCount = 0; var noopLogger = new NoopLogger(); var currentExecutionSegmentsContainer = new NoopCurrentExecutionSegmentsContainer(); var noopPayloadSender = new NoopPayloadSender(); var configurationReader = new TestAgentConfigurationReader(noopLogger); var sampler = new Sampler(rate); total.Repeat(i => { var transaction = new Transaction(noopLogger, "test transaction name", "test transaction type", sampler, /* distributedTracingData: */ null, noopPayloadSender, configurationReader, currentExecutionSegmentsContainer); if (transaction.IsSampled) { ++sampledCount; } if (i + 1 >= startCheckingAfter) { var actualRate = (double)sampledCount / (i + 1); var diffInRate = actualRate - rate; Assert.True(Math.Abs(diffInRate) <= allowedDiffInRate, "Abs(diffInRate) should be <= allowedDiffInRate. " + $"diffInRate: {diffInRate}, allowedDiffInRate: {allowedDiffInRate}, " + $"i: {i}, " + $"actual rate: {actualRate}, expected rate: {rate}, " + $"actual sampled count: {sampledCount}, expected sampled count: {Convert.ToInt32((i + 1) * rate)}" ); } }); }