Exemple #1
0
        public static async Task ExecuteParallelCommandsWithMediatr()
        {
            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddMediatR();
            serviceCollection.AddTransient <IRequestHandler <SimpleMediatrRequest, SimpleResult>, SimpleMediatrRequestHandler>();
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            IMediator            mediator = serviceProvider.GetService <IMediator>();
            SimpleMediatrRequest request  = new SimpleMediatrRequest();
            int perTaskCommands           = CommandsToExecute / ParallelTasks;

            Task[]    tasks = new Task[ParallelTasks];
            Stopwatch sw    = Stopwatch.StartNew();

            for (int taskIndex = 0; taskIndex < ParallelTasks; taskIndex++)
            {
                tasks[taskIndex] = Task.Run(async() =>
                {
                    for (int index = 0; index < perTaskCommands; index++)
                    {
                        await mediator.Send(request);
                    }
                });
            }

            await Task.WhenAll(tasks);

            sw.Stop();
            System.Console.WriteLine($"Took {sw.ElapsedMilliseconds}ms");
            System.Console.WriteLine($"Took {(double)sw.ElapsedMilliseconds / (double)CommandsToExecute}ms on average per command");
        }
Exemple #2
0
        public static async Task ExecuteCommandsWithMediatr()
        {
            IServiceCollection serviceCollection = new ServiceCollection();

            serviceCollection.AddMediatR();
            serviceCollection.AddTransient <IRequestHandler <SimpleMediatrRequest, SimpleResult>, SimpleMediatrRequestHandler>();
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            IMediator            mediator = serviceProvider.GetService <IMediator>();
            SimpleMediatrRequest request  = new SimpleMediatrRequest();
            Stopwatch            sw       = Stopwatch.StartNew();

            for (int index = 0; index < CommandsToExecute; index++)
            {
                await mediator.Send(request);
            }
            sw.Stop();
            System.Console.WriteLine($"Took {sw.ElapsedMilliseconds}ms");
            System.Console.WriteLine($"Took {(double)sw.ElapsedMilliseconds / (double)CommandsToExecute}ms on average per command");
        }