-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathEntityFrameworkBenchmark.cs
More file actions
58 lines (49 loc) · 1.64 KB
/
EntityFrameworkBenchmark.cs
File metadata and controls
58 lines (49 loc) · 1.64 KB
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
using BenchmarkDotNet.Attributes;
using Microsoft.EntityFrameworkCore;
[MemoryDiagnoser]
public class EntityFrameworkBenchmark
{
private const int Iterations = 1000;
private TestDbContextWithThreadSafety? _dbContextWithThreadSafety;
private TestDbContextWithoutThreadSafety? _dbContextWithoutThreadSafety;
[GlobalSetup]
public void Setup()
{
_dbContextWithThreadSafety = new TestDbContextWithThreadSafety();
_dbContextWithThreadSafety.Database.EnsureCreated();
_dbContextWithoutThreadSafety = new TestDbContextWithoutThreadSafety();
_dbContextWithoutThreadSafety.Database.EnsureCreated();
}
[GlobalCleanup]
public async Task Cleanup()
{
if (_dbContextWithThreadSafety is not null)
{
await _dbContextWithThreadSafety.DisposeAsync();
}
if (_dbContextWithoutThreadSafety is not null)
{
await _dbContextWithoutThreadSafety.DisposeAsync();
}
}
[Benchmark(Baseline = true)]
public async Task<List<TestEntity>> WithThreadSafetyChecks()
{
var results = new List<TestEntity>();
for (var i = 0; i < Iterations; i++)
{
results.AddRange(await _dbContextWithThreadSafety!.TestEntities.ToListAsync());
}
return results;
}
[Benchmark]
public async Task<List<TestEntity>> WithoutThreadSafetyChecks()
{
var results = new List<TestEntity>();
for (var i = 0; i < Iterations; i++)
{
results.AddRange(await _dbContextWithoutThreadSafety!.TestEntities.ToListAsync());
}
return results;
}
}