public async Task Run(int samples, int ops) { Console.WriteLine($"Running {samples} samples of {ops} iterations"); var builder = new ContainerBuilder(); builder.RegisterHandler <OrdinaryMessageHandler>(); builder.RegisterHandler <PolymorphicMessageHandler>(); builder.RegisterHandler <PolymorphicMessageHandler2>(); var activator = new AutofacHandlerActivator(builder, (_, _) => { }, startBus: false, enablePolymorphicDispatch: true, multipleRegistrationsCheckEnabled: true); var timeSpans = new List <TimeSpan>(); await using (builder.Build()) { foreach (var sample in Enumerable.Range(1, samples)) { var elapsed = await TakeSample(sample, ops, activator); timeSpans.Add(elapsed); } } Console.WriteLine($@"Results: {string.Join(Environment.NewLine, timeSpans.Select(t => $" {t.TotalSeconds:0.000} s"))} AVG: {timeSpans.Select(t => t.TotalSeconds).Average():0.000}"); }
public IHandlerActivator CreateActivator(Action <IHandlerRegistry> configureHandlers, out IActivatedContainer container) { var builder = new ContainerBuilder(); configureHandlers(new HandlerRegistry(builder)); var containerAdapter = new AutofacHandlerActivator(builder, (conf, con) => { }, startBus: false, enablePolymorphicDispatch: true); var autofacContainer = builder.Build(); container = new ActivatedContainer(autofacContainer); return(containerAdapter); }
static async Task <TimeSpan> TakeSample(int sample, int ops, AutofacHandlerActivator activator) { Console.WriteLine($"Running sample # {sample}"); var stopwatch = Stopwatch.StartNew(); for (var counter = 0; counter < ops; counter++) { using (var scope = new RebusTransactionScope()) { var handlers1 = await activator.GetHandlers(new OrdinaryMessage(), scope.TransactionContext); } using (var scope = new RebusTransactionScope()) { var handlers2 = await activator.GetHandlers(new PolymorphicMessage(), scope.TransactionContext); } } return(stopwatch.Elapsed); }