-
Notifications
You must be signed in to change notification settings - Fork 2k
/
SaveChangesPerformanceSample.cs
180 lines (155 loc) · 5.49 KB
/
SaveChangesPerformanceSample.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
171
172
173
174
175
176
177
178
179
180
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace NewInEfCore7;
public static class SaveChangesPerformanceSample
{
public static Task SaveChanges_SQL_generation_samples_SqlServer()
{
PrintSampleName();
return SaveChangesTest<PerfContextSqlServer>();
}
public static Task SaveChanges_SQL_generation_samples_Sqlite()
{
PrintSampleName();
return SaveChangesTest<PerfContextSqlite>();
}
private static async Task SaveChangesTest<TContext>()
where TContext : PerfContext, new()
{
await using (var context = new TContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
if (context.Database.IsSqlServer())
{
context.Database.ExecuteSqlRaw(
@"
CREATE TRIGGER TRG_InsertUpdateBlog
ON BlogsWithTriggers
AFTER INSERT, UPDATE AS
BEGIN
IF @@ROWCOUNT = 0
return
SET nocount on;
UPDATE BlogsWithTriggers set StoreUpdated = StoreUpdated + 1
WHERE Id IN(SELECT INSERTED.Id FROM INSERTED);
END");
}
}
await using (var context = new TContext())
{
#region SimpleInsert
await context.AddAsync(new Blog { Name = "MyBlog" });
await context.SaveChangesAsync();
#endregion
}
await using (var context = new TContext())
{
#region MultipleInsert
for (var i = 0; i < 4; i++)
{
await context.AddAsync(new Blog { Name = "Foo" + i });
}
await context.SaveChangesAsync();
#endregion
}
await using (var context = new TContext())
{
await context.AddAsync(new BlogWithTrigger { Name = "MyBlog" });
await context.SaveChangesAsync();
}
await using (var context = new TContext())
{
for (var i = 0; i < 4; i++)
{
await context.AddAsync(new BlogWithTrigger { Name = "Foo" + i });
}
await context.SaveChangesAsync();
}
await using (var context = new TContext())
{
#region InsertGraph
await context.AddAsync(
new Blog { Name = "MyBlog", Posts = { new() { Title = "My first post" }, new() { Title = "My second post" } } });
await context.SaveChangesAsync();
#endregion
}
await using (var context = new TContext())
{
#region InsertGraph
await context.AddAsync(
new Blog { Name = "MyBlog", Posts = { new() { Title = "My first post" }, new() { Title = "My second post" } } });
await context.SaveChangesAsync();
#endregion
}
}
private static void PrintSampleName([CallerMemberName] string? methodName = null)
{
Console.WriteLine($">>>> Sample: {methodName}");
Console.WriteLine();
}
public abstract class PerfContext : DbContext
{
public DbSet<Blog> Blogs => Set<Blog>();
public DbSet<Post> Posts => Set<Post>();
public DbSet<BlogWithTrigger> BlogsWithTriggers => Set<BlogWithTrigger>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<BlogWithTrigger>()
.Property(e => e.StoreUpdated)
.HasDefaultValue(0)
.ValueGeneratedOnAddOrUpdate();
if (Database.IsSqlServer())
{
#region HasTrigger
modelBuilder
.Entity<BlogWithTrigger>()
.ToTable(tb => tb.HasTrigger("TRG_InsertUpdateBlog"));
#endregion
#region UseHiLo
modelBuilder.Entity<Blog>().Property(e => e.Id).UseHiLo();
modelBuilder.Entity<Post>().Property(e => e.Id).UseHiLo();
#endregion
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.LogTo(
Console.WriteLine,
new[]
{
RelationalEventId.CommandExecuted, RelationalEventId.TransactionStarted, RelationalEventId.TransactionCommitted
})
.EnableSensitiveDataLogging();
}
public class PerfContextSqlServer : PerfContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> base.OnConfiguring(
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=SaveChangesPerf;ConnectRetryCount=0"));
}
public class PerfContextSqlite : PerfContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> base.OnConfiguring(
optionsBuilder.UseSqlite("Data Source = savechangesperf.db"));
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; } = default!;
public List<Post> Posts { get; } = new();
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; } = default!;
public Blog Blog { get; set; } = default!;
}
public class BlogWithTrigger
{
public int Id { get; set; }
public string Name { get; set; } = default!;
public virtual int StoreUpdated { get; set; }
}
}