-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathBulkMatchSingleColumnBenchmarks.cs
170 lines (139 loc) · 4.59 KB
/
BulkMatchSingleColumnBenchmarks.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.SqlServer.SimpleBulks.Benchmarks.Database;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkInsert;
using EntityFrameworkCore.SqlServer.SimpleBulks.BulkMatch;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.SqlServer.SimpleBulks.Benchmarks;
[WarmupCount(0)]
[IterationCount(1)]
[InvocationCount(1)]
[MemoryDiagnoser]
public class BulkMatchSingleColumnBenchmarks1
{
private TestDbContext _context;
private List<Customer> _customers;
private List<Guid> _customerIds;
[Params(100, 1000, 10_000, 100_000)]
public int RowsCount { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_context = new TestDbContext($"Server=127.0.0.1;Database=SimpleBulks.Benchmarks.{Guid.NewGuid()};User Id=sa;Password=sqladmin123!@#;Encrypt=False");
_context.Database.EnsureCreated();
_context.Database.SetCommandTimeout(TimeSpan.FromMinutes(2));
var isoCodes = new string[] { "VN", "US", "GB" };
var random = new Random(2024);
_customers = new List<Customer>(RowsCount);
for (int i = 0; i < RowsCount; i++)
{
var customer = new Customer
{
FirstName = "FirstName " + i,
LastName = "LastName " + i,
Index = i,
CurrentCountryIsoCode = isoCodes[random.Next(isoCodes.Length)]
};
_customers.Add(customer);
}
_context.BulkInsert(_customers);
_customerIds = _customers.Select(x => x.Id).ToList();
}
[GlobalCleanup]
public void GlobalCleanup()
{
_context.Database.EnsureDeleted();
}
[Benchmark]
public void EFCoreSelect()
{
var customers = new List<Customer>();
foreach (var id in _customerIds)
{
customers.Add(_context.Customers.Where(x => x.Id == id).AsNoTracking().First());
}
}
[Benchmark]
public void EFCoreBatchSelect()
{
var pageSize = 10_000;
var pages = _customerIds.Chunk(pageSize);
var customers = new List<Customer>();
foreach (var page in pages)
{
customers.AddRange(_context.Customers.Where(x => page.Contains(x.Id)).AsNoTracking().ToList());
}
}
[Benchmark]
public void BulkMatch()
{
var matchedCustomers = _customerIds.Select(x => new Customer { Id = x }).ToList();
var customers = _context.BulkMatch(matchedCustomers,
x => x.Id,
opt =>
{
opt.Timeout = 0;
});
}
}
[WarmupCount(0)]
[IterationCount(1)]
[InvocationCount(1)]
[MemoryDiagnoser]
public class BulkMatchSingleColumnBenchmarks2
{
private TestDbContext _context;
private List<Customer> _customers;
private List<Guid> _customerIds;
[Params(250_000, 500_000, 1_000_000)]
public int RowsCount { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_context = new TestDbContext($"Server=127.0.0.1;Database=SimpleBulks.Benchmarks.{Guid.NewGuid()};User Id=sa;Password=sqladmin123!@#;Encrypt=False");
_context.Database.EnsureCreated();
_context.Database.SetCommandTimeout(TimeSpan.FromMinutes(2));
var isoCodes = new string[] { "VN", "US", "GB" };
var random = new Random(2024);
_customers = new List<Customer>(RowsCount);
for (int i = 0; i < RowsCount; i++)
{
var customer = new Customer
{
FirstName = "FirstName " + i,
LastName = "LastName " + i,
Index = i,
CurrentCountryIsoCode = isoCodes[random.Next(isoCodes.Length)]
};
_customers.Add(customer);
}
_context.BulkInsert(_customers);
_customerIds = _customers.Select(x => x.Id).ToList();
}
[GlobalCleanup]
public void GlobalCleanup()
{
_context.Database.EnsureDeleted();
}
[Benchmark]
public void EFCoreBatchSelect()
{
var pageSize = 10_000;
var pages = _customerIds.Chunk(pageSize);
var customers = new List<Customer>();
foreach (var page in pages)
{
customers.AddRange(_context.Customers.Where(x => page.Contains(x.Id)).AsNoTracking().ToList());
}
}
[Benchmark]
public void BulkMatch()
{
var matchedCustomers = _customerIds.Select(x => new Customer { Id = x }).ToList();
var customers = _context.BulkMatch(matchedCustomers,
x => x.Id,
opt =>
{
opt.Timeout = 0;
});
}
}