Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PostSharp attribute to inject random, configurable latency
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly,
AllowMultiple = false)]
public class InjectLatencyAttribute : OnMethodBoundaryAspect
Random random = new Random((int)DateTime.UtcNow.Ticks);
public override void OnEntry(MethodExecutionArgs args)
// you need a mechanism to turn latency injection, preferably via a
// configuration mechanism that allows you to update on the fly
if (LatencyInjectionConfig.Enabled)
var lagSeconds = random.Next(LatencyInjectionConfig.MaxLatencyLagSeconds);
var latencyLag = TimeSpan.FromSeconds(lagSeconds);
// execute the underlying method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment