-
Notifications
You must be signed in to change notification settings - Fork 871
/
Copy pathProgram.cs
291 lines (256 loc) · 10.8 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using SqlSugar;
namespace FreeSql.Bechmarker
{
public class Program
{
public static void Main(string[] args)
{
//var summaryInsert = BenchmarkRunner.Run<OrmVsInsert>();
var summarySelect = BenchmarkRunner.Run<OrmVsSelect>(new BenchmarkDotNet.Configs.DebugBuildConfig
{
});
//var summaryUpdate = BenchmarkRunner.Run<OrmVsUpdate>();
Console.ReadKey();
Console.ReadKey();
}
}
public class Orm
{
public static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer,
"Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=103;TrustServerCertificate=true;Encrypt=False",
typeof(FreeSql.SqlServer.SqlServerProvider<>))
//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=20")
.UseAutoSyncStructure(false)
.UseNoneCommandParameter(true)
//.UseConfigEntityFromDbFirst(true)
.Build();
public static SqlSugarClient sugar
{
get => new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=102;TrustServerCertificate=true;Encrypt=False",
DbType = DbType.SqlServer,
//ConnectionString = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=20;Max Pool Size=20",
//DbType = DbType.MySql,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
}
}
class SongContext : DbContext
{
public DbSet<Song> Songs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=101;TrustServerCertificate=true;Encrypt=False");
//optionsBuilder.UseMySql("Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;Min Pool Size=21;Max Pool Size=21");
}
}
[RPlotExporter, RankColumn]
public class OrmVsInsert
{
public IEnumerable<Song> songs;
[Params(1, 500)]
public int size;
[GlobalSetup]
public void Setup()
{
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "freesql_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "sugar_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "efcore_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "dapper_song");
//测试前清空数据
Orm.fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows();
Orm.sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song");
songs = Enumerable.Range(0, size).Select(a => new Song
{
Create_time = DateTime.Now,
Is_deleted = false,
Title = $"Insert_{a}",
Url = $"Url_{a}"
});
//预热
Orm.fsql.Insert(songs.First()).ExecuteAffrows();
Orm.sugar.Insertable(songs.First()).ExecuteCommand();
using (var db = new SongContext())
{
//db.Configuration.AutoDetectChangesEnabled = false;
db.Songs.AddRange(songs.First());
db.SaveChanges();
}
}
[Benchmark]
public int FreeSqlInsert() => Orm.fsql.Insert(songs).ExecuteAffrows();
[Benchmark]
public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand();
[Benchmark]
public int EfCoreInsert()
{
using (var db = new SongContext())
{
//db.Configuration.AutoDetectChangesEnabled = false;
db.Songs.AddRange(songs.ToArray());
return db.SaveChanges();
}
}
[Benchmark]
public int DapperInsert()
{
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=100;TrustServerCertificate=true;Encrypt=False"))
{
foreach (var song in songs)
{
Dapper.SqlMapper.Execute(conn, @$"insert into dapper_song(Create_time,Is_deleted,Title,Url)
values('{song.Create_time.Value.ToString("yyyy-MM-dd HH:mm:ss")}',{(song.Is_deleted == true ? 1 : 0)},'{song.Title}','{song.Url}')");
}
}
return songs.Count();
}
}
[RPlotExporter, RankColumn]
public class OrmVsUpdate
{
public List<Song> songs;
[Params(1, 500)]
public int size;
[GlobalSetup]
public void Setup()
{
songs = Orm.fsql.Select<Song>().Limit(size).ToList();
}
[Benchmark]
public int FreeSqlUpdate() => Orm.fsql.Update<Song>().SetSource(songs).ExecuteAffrows();
[Benchmark]
public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand();
[Benchmark]
public int EfCoreUpdate()
{
using (var db = new SongContext())
{
//db.Configuration.AutoDetectChangesEnabled = false;
db.Songs.UpdateRange(songs.ToArray());
return db.SaveChanges();
}
}
[Benchmark]
public int DapperUpdate()
{
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=100;TrustServerCertificate=true;Encrypt=False"))
{
foreach (var song in songs)
{
Dapper.SqlMapper.Execute(conn, @$"update dapper_song set
Create_time = '{song.Create_time.Value.ToString("yyyy-MM-dd HH:mm:ss")}',
Is_deleted = {(song.Is_deleted == true ? 1 : 0)},
Title = '{song.Title}',
Url = '{song.Url}'
where id = {song.Id}");
}
}
return songs.Count();
}
}
[RPlotExporter, RankColumn]
public class OrmVsSelect
{
[Params(1, 500)]
public int size;
[IterationSetup]
public void Setup2()
{
Orm.fsql.Select<Song>().Limit(1).ToList();
Orm.sugar.Queryable<Song>().Take(1).ToList();
using (var db = new SongContext())
{
db.Songs.Take(1).AsNoTracking().ToList();
}
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=100;TrustServerCertificate=true;Encrypt=False"))
{
Dapper.SqlMapper.Query<Song>(conn, $"select top {1} Id,Create_time,Is_deleted,Title,Url from dapper_song").ToList();
}
}
[GlobalSetup]
public void Setup()
{
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "freesql_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "sugar_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "efcore_song");
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "dapper_song");
//测试前清空数据
Orm.fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows();
Orm.sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song");
Orm.fsql.Ado.ExecuteNonQuery("delete from dapper_song");
var songs = Enumerable.Range(0, size).Select(a => new Song
{
Create_time = DateTime.Now,
Is_deleted = false,
Title = $"Insert_{a}",
Url = $"Url_{a}"
});
//预热
Orm.fsql.Insert(songs).ExecuteAffrows();
Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand();
using (var db = new SongContext())
{
//db.Configuration.AutoDetectChangesEnabled = false;
db.Songs.AddRange(songs);
db.SaveChanges();
}
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=100;TrustServerCertificate=true;Encrypt=False"))
{
foreach (var song in songs)
{
Dapper.SqlMapper.Execute(conn, @$"insert into dapper_song(Create_time,Is_deleted,Title,Url)
values('{song.Create_time.Value.ToString("yyyy-MM-dd HH:mm:ss")}',{(song.Is_deleted == true ? 1 : 0)},'{song.Title}','{song.Url}')");
}
}
}
[Benchmark]
public List<Song> FreeSqlSelect() => Orm.fsql.Select<Song>().Limit(size).ToList();
[Benchmark]
public List<Song> SqlSugarSelect() => Orm.sugar.Queryable<Song>().Take(size).ToList();
[Benchmark]
public List<Song> EfCoreSelect()
{
using (var db = new SongContext())
{
return db.Songs.Take(size).AsNoTracking().ToList();
}
}
[Benchmark]
public List<Song> DapperSelete()
{
using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=100;TrustServerCertificate=true;Encrypt=False"))
{
return Dapper.SqlMapper.Query<Song>(conn, $"select top {size} Id,Create_time,Is_deleted,Title,Url from dapper_song").ToList();
}
}
}
[FreeSql.DataAnnotations.Table(Name = "freesql_song")]
[SugarTable("sugar_song")]
[Table("efcore_song")]
public class Song
{
[FreeSql.DataAnnotations.Column(IsIdentity = true)]
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime? Create_time { get; set; }
public bool? Is_deleted { get; set; }
public string Title { get; set; }
public string Url { get; set; }
}
}