-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathBenchmarks.cs
64 lines (57 loc) · 1.99 KB
/
Benchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
namespace Masa.Contrib.Dispatcher.Events.BenchmarkDotnet.Tests;
[MarkdownExporter, AsciiDocExporter, HtmlExporter]
[SimpleJob(RunStrategy.ColdStart, RuntimeMoniker.Net60, targetCount: 100)]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
public class Benchmarks
{
private RegisterUserEvent _userEvent;
private AddShoppingCartEvent _shoppingCartEvent;
private IServiceProvider _serviceProvider;
private IEventBus _eventBus;
private IMediator _mediator;
[GlobalSetup]
public void GlobalSetup()
{
IServiceCollection services = new ServiceCollection();
services.AddLogging(loggingBuilder => loggingBuilder.ClearProviders());
services.AddMediatR(typeof(Benchmarks));
services.AddEventBus();
_serviceProvider = services.BuildServiceProvider();
_eventBus = _serviceProvider.GetRequiredService<IEventBus>();
_mediator = _serviceProvider.GetRequiredService<IMediator>();
_userEvent = new RegisterUserEvent()
{
Name = "tom",
PhoneNumber = "18888888888"
};
_shoppingCartEvent = new AddShoppingCartEvent()
{
Count = 1,
GoodsId = "Microsoft"
};
}
[Benchmark(Baseline = true)]
public async Task SendCouponByDirect()
{
var _couponHandler = new CouponHandler(_serviceProvider);
await _couponHandler.SendCoupon(_userEvent);
await _couponHandler.SendNotice(_userEvent);
}
[Benchmark]
public async Task SendCouponByEventBus()
{
await _eventBus.PublishAsync(_userEvent);
}
[Benchmark]
public async Task AddShoppingCartByEventBusAsync()
{
await _eventBus.PublishAsync(_shoppingCartEvent);
}
[Benchmark]
public async Task AddShoppingCartByMediatRAsync()
{
await _mediator.Send(_shoppingCartEvent);
}
}