|
1 | | -using BenchmarkDotNet.Attributes; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
3 | | -using Thinktecture.Database; |
4 | | - |
5 | | -namespace Thinktecture.Benchmarking; |
6 | | - |
7 | | -[MemoryDiagnoser] |
8 | | -public class ReferenceEqualityValueComparer : IDisposable |
9 | | -{ |
10 | | - private BenchmarkContext? _benchmarkContext; |
11 | | - private IServiceScope? _scope; |
12 | | - private SqlServerBenchmarkDbContext? _sqlServerDbContext; |
13 | | - |
14 | | - private const int _BYTES_LENGTH = 1024; |
15 | | - |
16 | | - private int _counter; |
17 | | - private readonly byte[] _bytesBestCase = new byte[_BYTES_LENGTH]; |
18 | | - private readonly byte[] _bytesWorstCase = new byte[_BYTES_LENGTH]; |
19 | | - |
20 | | - private List<EntityWithByteArray> _entitiesWithDefaultComparer = null!; |
21 | | - private List<EntityWithByteArrayAndValueComparer> _entitiesWithCustomComparer = null!; |
22 | | - |
23 | | - [GlobalSetup] |
24 | | - public void Initialize() |
25 | | - { |
26 | | - _benchmarkContext = new BenchmarkContext(); |
27 | | - _scope = _benchmarkContext.RootServiceProvider.CreateScope(); |
28 | | - _sqlServerDbContext = _scope.ServiceProvider.GetRequiredService<SqlServerBenchmarkDbContext>(); |
29 | | - |
30 | | - _sqlServerDbContext.Database.EnsureDeleted(); |
31 | | - _sqlServerDbContext.Database.EnsureCreated(); |
32 | | - |
33 | | - _sqlServerDbContext.EntitiesWithByteArray.BulkDelete(); |
34 | | - _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.BulkDelete(); |
35 | | - |
36 | | - var bytes = new byte[_BYTES_LENGTH]; |
37 | | - |
38 | | - for (var i = 0; i < 10_000; i++) |
39 | | - { |
40 | | - var id = new Guid($"66AFED1B-92EA-4483-BF4F-{i.ToString("X").PadLeft(12, '0')}"); |
41 | | - |
42 | | - _sqlServerDbContext.EntitiesWithByteArray.Add(new EntityWithByteArray(id, bytes)); |
43 | | - _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.Add(new EntityWithByteArrayAndValueComparer(id, bytes)); |
44 | | - } |
45 | | - |
46 | | - _sqlServerDbContext.SaveChanges(); |
47 | | - _sqlServerDbContext.ChangeTracker.Clear(); |
48 | | - } |
49 | | - |
50 | | - [GlobalCleanup] |
51 | | - public void Dispose() |
52 | | - { |
53 | | - _scope?.Dispose(); |
54 | | - _benchmarkContext?.Dispose(); |
55 | | - } |
56 | | - |
57 | | - [IterationSetup] |
58 | | - public void IterationSetup() |
59 | | - { |
60 | | - _sqlServerDbContext!.ChangeTracker.Clear(); |
61 | | - _entitiesWithDefaultComparer = _sqlServerDbContext.EntitiesWithByteArray.ToList(); |
62 | | - _entitiesWithCustomComparer = _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.ToList(); |
63 | | - |
64 | | - _bytesBestCase[0] = _bytesWorstCase[^1] = (byte)(++_counter % Byte.MaxValue); |
65 | | - } |
66 | | - |
67 | | - [Benchmark] |
68 | | - public async Task Default_BestCase() |
69 | | - { |
70 | | - _entitiesWithDefaultComparer.ForEach(e => e.Bytes = _bytesBestCase); |
71 | | - |
72 | | - await _sqlServerDbContext!.SaveChangesAsync(); |
73 | | - } |
74 | | - |
75 | | - [Benchmark] |
76 | | - public async Task Default_WorstCase() |
77 | | - { |
78 | | - _entitiesWithDefaultComparer.ForEach(e => e.Bytes = _bytesWorstCase); |
79 | | - |
80 | | - await _sqlServerDbContext!.SaveChangesAsync(); |
81 | | - } |
82 | | - |
83 | | - [Benchmark] |
84 | | - public async Task ReferenceEquality_BestCase() |
85 | | - { |
86 | | - _entitiesWithCustomComparer.ForEach(e => e.Bytes = _bytesBestCase); |
87 | | - |
88 | | - await _sqlServerDbContext!.SaveChangesAsync(); |
89 | | - } |
90 | | - |
91 | | - [Benchmark] |
92 | | - public async Task ReferenceEquality_WorstCase() |
93 | | - { |
94 | | - _entitiesWithCustomComparer.ForEach(e => e.Bytes = _bytesWorstCase); |
95 | | - |
96 | | - await _sqlServerDbContext!.SaveChangesAsync(); |
97 | | - } |
98 | | -} |
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using Thinktecture.Database; |
| 4 | + |
| 5 | +namespace Thinktecture.Benchmarking; |
| 6 | + |
| 7 | +[MemoryDiagnoser] |
| 8 | +public class ReferenceEqualityValueComparer : IDisposable |
| 9 | +{ |
| 10 | + private BenchmarkContext? _benchmarkContext; |
| 11 | + private IServiceScope? _scope; |
| 12 | + private SqlServerBenchmarkDbContext? _sqlServerDbContext; |
| 13 | + |
| 14 | + private const int _BYTES_LENGTH = 1024; |
| 15 | + |
| 16 | + private int _counter; |
| 17 | + private readonly byte[] _bytesBestCase = new byte[_BYTES_LENGTH]; |
| 18 | + private readonly byte[] _bytesWorstCase = new byte[_BYTES_LENGTH]; |
| 19 | + |
| 20 | + private List<EntityWithByteArray> _entitiesWithDefaultComparer = null!; |
| 21 | + private List<EntityWithByteArrayAndValueComparer> _entitiesWithCustomComparer = null!; |
| 22 | + |
| 23 | + [GlobalSetup] |
| 24 | + public void Initialize() |
| 25 | + { |
| 26 | + _benchmarkContext = new BenchmarkContext(); |
| 27 | + _scope = _benchmarkContext.RootServiceProvider.CreateScope(); |
| 28 | + _sqlServerDbContext = _scope.ServiceProvider.GetRequiredService<SqlServerBenchmarkDbContext>(); |
| 29 | + |
| 30 | + _sqlServerDbContext.Database.EnsureDeleted(); |
| 31 | + _sqlServerDbContext.Database.EnsureCreated(); |
| 32 | + |
| 33 | + _sqlServerDbContext.EntitiesWithByteArray.ExecuteDelete(); |
| 34 | + _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.ExecuteDelete(); |
| 35 | + |
| 36 | + var bytes = new byte[_BYTES_LENGTH]; |
| 37 | + |
| 38 | + for (var i = 0; i < 10_000; i++) |
| 39 | + { |
| 40 | + var id = new Guid($"66AFED1B-92EA-4483-BF4F-{i.ToString("X").PadLeft(12, '0')}"); |
| 41 | + |
| 42 | + _sqlServerDbContext.EntitiesWithByteArray.Add(new EntityWithByteArray(id, bytes)); |
| 43 | + _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.Add(new EntityWithByteArrayAndValueComparer(id, bytes)); |
| 44 | + } |
| 45 | + |
| 46 | + _sqlServerDbContext.SaveChanges(); |
| 47 | + _sqlServerDbContext.ChangeTracker.Clear(); |
| 48 | + } |
| 49 | + |
| 50 | + [GlobalCleanup] |
| 51 | + public void Dispose() |
| 52 | + { |
| 53 | + _scope?.Dispose(); |
| 54 | + _benchmarkContext?.Dispose(); |
| 55 | + } |
| 56 | + |
| 57 | + [IterationSetup] |
| 58 | + public void IterationSetup() |
| 59 | + { |
| 60 | + _sqlServerDbContext!.ChangeTracker.Clear(); |
| 61 | + _entitiesWithDefaultComparer = _sqlServerDbContext.EntitiesWithByteArray.ToList(); |
| 62 | + _entitiesWithCustomComparer = _sqlServerDbContext.EntitiesWithByteArrayAndValueComparer.ToList(); |
| 63 | + |
| 64 | + _bytesBestCase[0] = _bytesWorstCase[^1] = (byte)(++_counter % Byte.MaxValue); |
| 65 | + } |
| 66 | + |
| 67 | + [Benchmark] |
| 68 | + public async Task Default_BestCase() |
| 69 | + { |
| 70 | + _entitiesWithDefaultComparer.ForEach(e => e.Bytes = _bytesBestCase); |
| 71 | + |
| 72 | + await _sqlServerDbContext!.SaveChangesAsync(); |
| 73 | + } |
| 74 | + |
| 75 | + [Benchmark] |
| 76 | + public async Task Default_WorstCase() |
| 77 | + { |
| 78 | + _entitiesWithDefaultComparer.ForEach(e => e.Bytes = _bytesWorstCase); |
| 79 | + |
| 80 | + await _sqlServerDbContext!.SaveChangesAsync(); |
| 81 | + } |
| 82 | + |
| 83 | + [Benchmark] |
| 84 | + public async Task ReferenceEquality_BestCase() |
| 85 | + { |
| 86 | + _entitiesWithCustomComparer.ForEach(e => e.Bytes = _bytesBestCase); |
| 87 | + |
| 88 | + await _sqlServerDbContext!.SaveChangesAsync(); |
| 89 | + } |
| 90 | + |
| 91 | + [Benchmark] |
| 92 | + public async Task ReferenceEquality_WorstCase() |
| 93 | + { |
| 94 | + _entitiesWithCustomComparer.ForEach(e => e.Bytes = _bytesWorstCase); |
| 95 | + |
| 96 | + await _sqlServerDbContext!.SaveChangesAsync(); |
| 97 | + } |
| 98 | +} |
0 commit comments