|
2 | 2 | using Microsoft.EntityFrameworkCore; |
3 | 3 | using Microsoft.Extensions.DependencyInjection; |
4 | 4 |
|
5 | | -namespace EntityFrameworkCore.Triggered.Benchmarks |
| 5 | +namespace EntityFrameworkCore.Triggered.Benchmarks; |
| 6 | + |
| 7 | +[MemoryDiagnoser] |
| 8 | +public class EmbracingFeaturesBenchmarks |
6 | 9 | { |
7 | | - [MemoryDiagnoser] |
8 | | - public class EmbracingFeaturesBenchmarks |
| 10 | + private IServiceProvider _serviceProvider; |
| 11 | + |
| 12 | + [GlobalSetup] |
| 13 | + public void GlobalSetup() => _serviceProvider = new ServiceCollection() |
| 14 | + .AddDbContext<ApplicationContext>(options => { |
| 15 | + options |
| 16 | + .UseInMemoryDatabase(nameof(WithDbContext)); |
| 17 | + }) |
| 18 | + .AddTriggeredDbContext<TriggeredApplicationContext>(options => { |
| 19 | + options |
| 20 | + .UseInMemoryDatabase(nameof(WithTriggeredDbContext)) |
| 21 | + .UseTriggers(triggerOptions => { |
| 22 | + triggerOptions.AddTrigger<Triggers.SetStudentRegistrationDateTrigger>(); |
| 23 | + triggerOptions.AddTrigger<Triggers.SignStudentUpForMandatoryCourses>(); |
| 24 | + }); |
| 25 | + }) |
| 26 | + .BuildServiceProvider(); |
| 27 | + |
| 28 | + [Params(50)] |
| 29 | + public int OuterBatches; |
| 30 | + |
| 31 | + [Params(1, 10, 100)] |
| 32 | + public int InnerBatches; |
| 33 | + |
| 34 | + private void Validate(IApplicationContextContract applicationContextContract) |
9 | 35 | { |
10 | | - private IServiceProvider _serviceProvider; |
11 | | - |
12 | | - [GlobalSetup] |
13 | | - public void GlobalSetup() => _serviceProvider = new ServiceCollection() |
14 | | - .AddDbContext<ApplicationContext>(options => { |
15 | | - options |
16 | | - .UseInMemoryDatabase(nameof(WithDbContext)); |
17 | | - }) |
18 | | - .AddTriggeredDbContext<TriggeredApplicationContext>(options => { |
19 | | - options |
20 | | - .UseInMemoryDatabase(nameof(WithTriggeredDbContext)) |
21 | | - .UseTriggers(triggerOptions => { |
22 | | - triggerOptions.AddTrigger<Triggers.SetStudentRegistrationDateTrigger>(); |
23 | | - triggerOptions.AddTrigger<Triggers.SignStudentUpForMandatoryCourses>(); |
24 | | - }); |
25 | | - }) |
26 | | - .BuildServiceProvider(); |
27 | | - |
28 | | - [Params(50)] |
29 | | - public int OuterBatches; |
30 | | - |
31 | | - [Params(1, 10, 100)] |
32 | | - public int InnerBatches; |
33 | | - |
34 | | - private void Validate(IApplicationContextContract applicationContextContract) |
35 | | - { |
36 | | - var studentCoursesCount = applicationContextContract.StudentCourses.Count(); |
37 | | - var expectedCoursesCount = OuterBatches * InnerBatches; |
| 36 | + var studentCoursesCount = applicationContextContract.StudentCourses.Count(); |
| 37 | + var expectedCoursesCount = OuterBatches * InnerBatches; |
38 | 38 |
|
39 | | - if (studentCoursesCount != expectedCoursesCount) |
40 | | - { |
41 | | - throw new InvalidOperationException($"Found {studentCoursesCount}, expected {expectedCoursesCount}"); |
42 | | - } |
| 39 | + if (studentCoursesCount != expectedCoursesCount) |
| 40 | + { |
| 41 | + throw new InvalidOperationException($"Found {studentCoursesCount}, expected {expectedCoursesCount}"); |
43 | 42 | } |
| 43 | + } |
44 | 44 |
|
45 | | - [Benchmark(Baseline = true)] |
46 | | - public void WithDbContext() |
| 45 | + [Benchmark(Baseline = true)] |
| 46 | + public void WithDbContext() |
| 47 | + { |
| 48 | + // setup |
47 | 49 | { |
48 | | - // setup |
49 | | - { |
50 | | - using var scope = _serviceProvider.CreateScope(); |
51 | | - using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
| 50 | + using var scope = _serviceProvider.CreateScope(); |
| 51 | + using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
52 | 52 |
|
53 | | - context.Database.EnsureDeleted(); |
| 53 | + context.Database.EnsureDeleted(); |
54 | 54 |
|
55 | | - context.Courses.Add(new Course { Id = Guid.NewGuid(), DisplayName = "Test", IsMandatory = true }); |
56 | | - context.SaveChanges(); |
57 | | - } |
| 55 | + context.Courses.Add(new Course { Id = Guid.NewGuid(), DisplayName = "Test", IsMandatory = true }); |
| 56 | + context.SaveChanges(); |
| 57 | + } |
58 | 58 |
|
59 | | - // execute |
60 | | - for (var outerBatch = 0; outerBatch < OuterBatches; outerBatch++) |
61 | | - { |
62 | | - using var scope = _serviceProvider.CreateScope(); |
63 | | - using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
| 59 | + // execute |
| 60 | + for (var outerBatch = 0; outerBatch < OuterBatches; outerBatch++) |
| 61 | + { |
| 62 | + using var scope = _serviceProvider.CreateScope(); |
| 63 | + using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
64 | 64 |
|
65 | | - for (var innerBatch = 0; innerBatch < InnerBatches; innerBatch++) |
66 | | - { |
67 | | - var student = new Student { Id = Guid.NewGuid(), DisplayName = "Test" }; |
68 | | - student.RegistrationDate = DateTimeOffset.Now; |
| 65 | + for (var innerBatch = 0; innerBatch < InnerBatches; innerBatch++) |
| 66 | + { |
| 67 | + var student = new Student { Id = Guid.NewGuid(), DisplayName = "Test" }; |
| 68 | + student.RegistrationDate = DateTimeOffset.Now; |
69 | 69 |
|
70 | | - context.Add(student); |
| 70 | + context.Add(student); |
71 | 71 |
|
72 | | - var mandatoryCourses = context.Courses.Where(x => x.IsMandatory).ToList(); |
| 72 | + var mandatoryCourses = context.Courses.Where(x => x.IsMandatory).ToList(); |
73 | 73 |
|
74 | | - foreach (var mandatoryCourse in mandatoryCourses) |
75 | | - { |
76 | | - context.StudentCourses.Add(new StudentCourse { |
77 | | - CourseId = mandatoryCourse.Id, |
78 | | - StudentId = student.Id |
79 | | - }); |
80 | | - } |
| 74 | + foreach (var mandatoryCourse in mandatoryCourses) |
| 75 | + { |
| 76 | + context.StudentCourses.Add(new StudentCourse { |
| 77 | + CourseId = mandatoryCourse.Id, |
| 78 | + StudentId = student.Id |
| 79 | + }); |
81 | 80 | } |
82 | | - |
83 | | - context.SaveChanges(); |
84 | 81 | } |
85 | 82 |
|
86 | | - // validate |
87 | | - { |
88 | | - using var scope = _serviceProvider.CreateScope(); |
89 | | - using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
90 | | - |
91 | | - Validate(context); |
92 | | - } |
| 83 | + context.SaveChanges(); |
93 | 84 | } |
94 | 85 |
|
95 | | - [Benchmark] |
96 | | - public void WithTriggeredDbContext() |
| 86 | + // validate |
97 | 87 | { |
98 | | - // setup |
99 | | - { |
100 | | - using var scope = _serviceProvider.CreateScope(); |
101 | | - using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
| 88 | + using var scope = _serviceProvider.CreateScope(); |
| 89 | + using var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>(); |
102 | 90 |
|
103 | | - context.Database.EnsureDeleted(); |
| 91 | + Validate(context); |
| 92 | + } |
| 93 | + } |
104 | 94 |
|
105 | | - context.Courses.Add(new Course { Id = Guid.NewGuid(), DisplayName = "Test", IsMandatory = true }); |
106 | | - context.SaveChanges(); |
107 | | - } |
| 95 | + [Benchmark] |
| 96 | + public void WithTriggeredDbContext() |
| 97 | + { |
| 98 | + // setup |
| 99 | + { |
| 100 | + using var scope = _serviceProvider.CreateScope(); |
| 101 | + using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
108 | 102 |
|
109 | | - // execute |
110 | | - for (var outerBatch = 0; outerBatch < OuterBatches; outerBatch++) |
111 | | - { |
112 | | - using var scope = _serviceProvider.CreateScope(); |
113 | | - using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
| 103 | + context.Database.EnsureDeleted(); |
114 | 104 |
|
115 | | - for (var innerBatch = 0; innerBatch < InnerBatches; innerBatch++) |
116 | | - { |
117 | | - var student = new Student { Id = Guid.NewGuid(), DisplayName = "Test" }; |
118 | | - context.Add(student); |
119 | | - } |
| 105 | + context.Courses.Add(new Course { Id = Guid.NewGuid(), DisplayName = "Test", IsMandatory = true }); |
| 106 | + context.SaveChanges(); |
| 107 | + } |
| 108 | + |
| 109 | + // execute |
| 110 | + for (var outerBatch = 0; outerBatch < OuterBatches; outerBatch++) |
| 111 | + { |
| 112 | + using var scope = _serviceProvider.CreateScope(); |
| 113 | + using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
120 | 114 |
|
121 | | - context.SaveChanges(); |
| 115 | + for (var innerBatch = 0; innerBatch < InnerBatches; innerBatch++) |
| 116 | + { |
| 117 | + var student = new Student { Id = Guid.NewGuid(), DisplayName = "Test" }; |
| 118 | + context.Add(student); |
122 | 119 | } |
123 | 120 |
|
| 121 | + context.SaveChanges(); |
| 122 | + } |
124 | 123 |
|
125 | | - // validate |
126 | | - { |
127 | | - using var scope = _serviceProvider.CreateScope(); |
128 | | - using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
129 | 124 |
|
130 | | - Validate(context); |
131 | | - } |
| 125 | + // validate |
| 126 | + { |
| 127 | + using var scope = _serviceProvider.CreateScope(); |
| 128 | + using var context = scope.ServiceProvider.GetRequiredService<TriggeredApplicationContext>(); |
| 129 | + |
| 130 | + Validate(context); |
132 | 131 | } |
133 | 132 | } |
134 | 133 | } |
0 commit comments