|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel.DataAnnotations; |
| 4 | +using System.ComponentModel.DataAnnotations.Schema; |
| 5 | +using System.Linq; |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | +using BenchmarkDotNet.Running; |
| 8 | +using Microsoft.EntityFrameworkCore; |
| 9 | +//using SqlSugar; |
| 10 | + |
| 11 | +namespace FreeSql.Bechmarker { |
| 12 | + |
| 13 | + public class Program { |
| 14 | + public static void Main(string[] args) { |
| 15 | + var summaryInsert = BenchmarkRunner.Run<OrmVsInsert>(); |
| 16 | + var summarySelect = BenchmarkRunner.Run<OrmVsSelect>(); |
| 17 | + var summaryUpdate = BenchmarkRunner.Run<OrmVsUpdate>(); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public class Orm { |
| 22 | + public static IFreeSql fsql = new FreeSql.FreeSqlBuilder() |
| 23 | + .UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20") |
| 24 | + //.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") |
| 25 | + .UseAutoSyncStructure(false) |
| 26 | + .UseNoneCommandParameter(true) |
| 27 | + //.UseConfigEntityFromDbFirst(true) |
| 28 | + .Build(); |
| 29 | + |
| 30 | + //public static SqlSugarClient sugar { |
| 31 | + // get => new SqlSugarClient(new ConnectionConfig() { |
| 32 | + // //不欺负,让连接池100个最小 |
| 33 | + // ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20", |
| 34 | + // DbType = DbType.SqlServer, |
| 35 | + // //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", |
| 36 | + // //DbType = DbType.MySql, |
| 37 | + // IsAutoCloseConnection = true, |
| 38 | + // InitKeyType = InitKeyType.Attribute |
| 39 | + // }); |
| 40 | + //} |
| 41 | + } |
| 42 | + class SongContext : DbContext { |
| 43 | + public DbSet<Song> Songs { get; set; } |
| 44 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { |
| 45 | + optionsBuilder.UseSqlServer(@"Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=21"); |
| 46 | + //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"); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [CoreJob] |
| 51 | + [RPlotExporter, RankColumn] |
| 52 | + public class OrmVsInsert { |
| 53 | + public IEnumerable<Song> songs; |
| 54 | + |
| 55 | + [Params(1, 500, 1000, 5000, 10000, 50000, 100000)] |
| 56 | + public int size; |
| 57 | + |
| 58 | + [GlobalSetup] |
| 59 | + public void Setup() { |
| 60 | + Orm.fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag)); |
| 61 | + //Orm.sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag)); |
| 62 | + //sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements |
| 63 | + |
| 64 | + //测试前清空数据 |
| 65 | + Orm.fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows(); |
| 66 | + //Orm.sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand(); |
| 67 | + Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song"); |
| 68 | + |
| 69 | + songs = Enumerable.Range(0, size).Select(a => new Song { |
| 70 | + Create_time = DateTime.Now, |
| 71 | + Is_deleted = false, |
| 72 | + Title = $"Insert_{a}", |
| 73 | + Url = $"Url_{a}" |
| 74 | + }); |
| 75 | + |
| 76 | + //预热 |
| 77 | + Orm.fsql.Insert(songs.First()).ExecuteAffrows(); |
| 78 | + //Orm.sugar.Insertable(songs.First()).ExecuteCommand(); |
| 79 | + using (var db = new SongContext()) { |
| 80 | + //db.Configuration.AutoDetectChangesEnabled = false; |
| 81 | + db.Songs.AddRange(songs.First()); |
| 82 | + db.SaveChanges(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Benchmark] |
| 87 | + public int FreeSqlInsert() => Orm.fsql.Insert(songs).ExecuteAffrows(); |
| 88 | + |
| 89 | + //[Benchmark] |
| 90 | + //public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand(); |
| 91 | + |
| 92 | + [Benchmark] |
| 93 | + public int EfCoreInsert() { |
| 94 | + using (var db = new SongContext()) { |
| 95 | + //db.Configuration.AutoDetectChangesEnabled = false; |
| 96 | + db.Songs.AddRange(songs.ToArray()); |
| 97 | + return db.SaveChanges(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [CoreJob] |
| 103 | + [RPlotExporter, RankColumn] |
| 104 | + public class OrmVsUpdate { |
| 105 | + public List<Song> songs; |
| 106 | + |
| 107 | + [Params(1, 500, 1000, 5000, 10000, 50000, 100000)] |
| 108 | + public int size; |
| 109 | + |
| 110 | + [GlobalSetup] |
| 111 | + public void Setup() { |
| 112 | + songs = Orm.fsql.Select<Song>().Limit(size).ToList(); |
| 113 | + } |
| 114 | + |
| 115 | + [Benchmark] |
| 116 | + public int FreeSqlUpdate() => Orm.fsql.Update<Song>().SetSource(songs).ExecuteAffrows(); |
| 117 | + |
| 118 | + //[Benchmark] |
| 119 | + //public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand(); |
| 120 | + |
| 121 | + [Benchmark] |
| 122 | + public int EfCoreUpdate() { |
| 123 | + using (var db = new SongContext()) { |
| 124 | + //db.Configuration.AutoDetectChangesEnabled = false; |
| 125 | + db.Songs.UpdateRange(songs.ToArray()); |
| 126 | + return db.SaveChanges(); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + [CoreJob] |
| 132 | + [RPlotExporter, RankColumn] |
| 133 | + public class OrmVsSelect { |
| 134 | + |
| 135 | + [Params(1, 500, 1000, 5000, 10000, 50000, 100000)] |
| 136 | + public int size; |
| 137 | + |
| 138 | + [GlobalSetup] |
| 139 | + public void Setup() { |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + [Benchmark] |
| 144 | + public List<Song> FreeSqlSelect() => Orm.fsql.Select<Song>().Limit(size).ToList(); |
| 145 | + |
| 146 | + //[Benchmark] |
| 147 | + //public List<Song> SqlSugarSelect() => Orm.sugar.Queryable<Song>().Take(size).ToList(); |
| 148 | + |
| 149 | + [Benchmark] |
| 150 | + public List<Song> EfCoreSelect() { |
| 151 | + using (var db = new SongContext()) { |
| 152 | + return db.Songs.Take(size).AsNoTracking().ToList(); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + [FreeSql.DataAnnotations.Table(Name = "freesql_song")] |
| 158 | + //[SugarTable("sugar_song")] |
| 159 | + [Table("efcore_song")] |
| 160 | + public class Song { |
| 161 | + [FreeSql.DataAnnotations.Column(IsIdentity = true)] |
| 162 | + //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] |
| 163 | + [Key] |
| 164 | + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] |
| 165 | + public int Id { get; set; } |
| 166 | + public DateTime? Create_time { get; set; } |
| 167 | + public bool? Is_deleted { get; set; } |
| 168 | + public string Title { get; set; } |
| 169 | + public string Url { get; set; } |
| 170 | + |
| 171 | + //[SugarColumn(IsIgnore = true)] |
| 172 | + [NotMapped] |
| 173 | + public virtual ICollection<Tag> Tags { get; set; } |
| 174 | + } |
| 175 | + [FreeSql.DataAnnotations.Table(Name = "freesql_song_tag")] |
| 176 | + //[SugarTable("sugar_song_tag")] |
| 177 | + [Table("efcore_song_tag")] |
| 178 | + public class Song_tag { |
| 179 | + public int Song_id { get; set; } |
| 180 | + //[SugarColumn(IsIgnore = true)] |
| 181 | + [NotMapped] |
| 182 | + public virtual Song Song { get; set; } |
| 183 | + |
| 184 | + public int Tag_id { get; set; } |
| 185 | + //[SugarColumn(IsIgnore = true)] |
| 186 | + [NotMapped] |
| 187 | + public virtual Tag Tag { get; set; } |
| 188 | + } |
| 189 | + [FreeSql.DataAnnotations.Table(Name = "freesql_tag")] |
| 190 | + //[SugarTable("sugar_tag")] |
| 191 | + [Table("efcore_tag")] |
| 192 | + public class Tag { |
| 193 | + [FreeSql.DataAnnotations.Column(IsIdentity = true)] |
| 194 | + //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] |
| 195 | + [Key] |
| 196 | + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] |
| 197 | + public int Id { get; set; } |
| 198 | + public int? Parent_id { get; set; } |
| 199 | + //[SugarColumn(IsIgnore = true)] |
| 200 | + [NotMapped] |
| 201 | + public virtual Tag Parent { get; set; } |
| 202 | + |
| 203 | + public decimal? Ddd { get; set; } |
| 204 | + public string Name { get; set; } |
| 205 | + |
| 206 | + //[SugarColumn(IsIgnore = true)] |
| 207 | + [NotMapped] |
| 208 | + public virtual ICollection<Song> Songs { get; set; } |
| 209 | + //[SugarColumn(IsIgnore = true)] |
| 210 | + [NotMapped] |
| 211 | + public virtual ICollection<Tag> Tags { get; set; } |
| 212 | + } |
| 213 | +} |
| 214 | + |
0 commit comments