-
Notifications
You must be signed in to change notification settings - Fork 21
/
Program.cs
62 lines (52 loc) · 1.53 KB
/
Program.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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using EF7Bulk;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
BenchmarkRunner.Run<UpdateBenchmark>();
[MemoryDiagnoser]
public class UpdateBenchmark
{
private PeopleContext _peopleContext;
[Params(100, 1_000)] public int RowsToUpdate { get; set; }
[GlobalSetup]
public void Setup()
{
_peopleContext = CreateContext();
CreateContext();
FillWithData();
}
[Benchmark(Baseline = true)]
public async Task OneByOneUpdate()
{
var entries = await _peopleContext.People.ToListAsync();
foreach (var entry in entries)
{
entry.Age = 21;
}
await _peopleContext.SaveChangesAsync();
}
[Benchmark]
public async Task BulkUpdate()
{
await _peopleContext.People
.ExecuteUpdateAsync(s => s.SetProperty(p => p.Age, p => 21));
}
private void FillWithData()
{
using var context = CreateContext();
context.Database.EnsureCreated();
for (var i = 1; i <= RowsToUpdate; i++)
context.People.Add(new Person(i, "Steven", 31));
context.SaveChanges();
}
private static PeopleContext CreateContext()
{
var connection = new SqliteConnection("DataSource=myshareddb;mode=memory;cache=shared");
connection.Open();
var options = new DbContextOptionsBuilder()
.UseSqlite(connection)
.Options;
return new PeopleContext(options);
}
}