|
| 1 | +//using SqlSugar; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace orm_vs |
| 11 | +{ |
| 12 | + class Program |
| 13 | + { |
| 14 | + static IFreeSql fsql = new FreeSql.FreeSqlBuilder() |
| 15 | + //.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20") |
| 16 | + .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") |
| 17 | + .UseAutoSyncStructure(false) |
| 18 | + .UseNoneCommandParameter(true) |
| 19 | + //.UseConfigEntityFromDbFirst(true) |
| 20 | + .Build(); |
| 21 | + |
| 22 | + //static SqlSugarClient sugar |
| 23 | + //{ |
| 24 | + // get => new SqlSugarClient(new ConnectionConfig() |
| 25 | + // { |
| 26 | + // //不欺负,让连接池100个最小 |
| 27 | + // //ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20", |
| 28 | + // //DbType = DbType.SqlServer, |
| 29 | + // 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", |
| 30 | + // DbType = DbType.MySql, |
| 31 | + // IsAutoCloseConnection = true, |
| 32 | + // InitKeyType = InitKeyType.Attribute |
| 33 | + // }); |
| 34 | + //} |
| 35 | + |
| 36 | + static void Main(string[] args) |
| 37 | + { |
| 38 | + var testlist1 = fsql.Select<Song>().OrderBy(a => a.Id).ToList(); |
| 39 | + var testlist2 = new List<Song>(); |
| 40 | + fsql.Select<Song>().OrderBy(a => a.Id).ToChunk(0, list => |
| 41 | + { |
| 42 | + testlist2.AddRange(list); |
| 43 | + }); |
| 44 | + |
| 45 | + fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag)); |
| 46 | + //sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag)); |
| 47 | + //sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements |
| 48 | + |
| 49 | + //sugar.Aop.OnLogExecuted = (s, e) => |
| 50 | + //{ |
| 51 | + // Trace.WriteLine(s); |
| 52 | + //}; |
| 53 | + //测试前清空数据 |
| 54 | + fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows(); |
| 55 | + //sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand(); |
| 56 | + fsql.Ado.ExecuteNonQuery("delete from efcore_song"); |
| 57 | + |
| 58 | + var sb = new StringBuilder(); |
| 59 | + Console.WriteLine("插入性能:"); |
| 60 | + Insert(sb, 1000, 1); |
| 61 | + Console.Write(sb.ToString()); |
| 62 | + sb.Clear(); |
| 63 | + Insert(sb, 1000, 10); |
| 64 | + Console.Write(sb.ToString()); |
| 65 | + sb.Clear(); |
| 66 | + |
| 67 | + Insert(sb, 1, 1000); |
| 68 | + Console.Write(sb.ToString()); |
| 69 | + sb.Clear(); |
| 70 | + Insert(sb, 1, 10000); |
| 71 | + Console.Write(sb.ToString()); |
| 72 | + sb.Clear(); |
| 73 | + Insert(sb, 1, 50000); |
| 74 | + Console.Write(sb.ToString()); |
| 75 | + sb.Clear(); |
| 76 | + Insert(sb, 1, 100000); |
| 77 | + Console.Write(sb.ToString()); |
| 78 | + sb.Clear(); |
| 79 | + |
| 80 | + Console.WriteLine("查询性能:"); |
| 81 | + Select(sb, 1000, 1); |
| 82 | + Console.Write(sb.ToString()); |
| 83 | + sb.Clear(); |
| 84 | + Select(sb, 1000, 10); |
| 85 | + Console.Write(sb.ToString()); |
| 86 | + sb.Clear(); |
| 87 | + |
| 88 | + Select(sb, 1, 1000); |
| 89 | + Console.Write(sb.ToString()); |
| 90 | + sb.Clear(); |
| 91 | + Select(sb, 1, 10000); |
| 92 | + Console.Write(sb.ToString()); |
| 93 | + sb.Clear(); |
| 94 | + Select(sb, 1, 50000); |
| 95 | + Console.Write(sb.ToString()); |
| 96 | + sb.Clear(); |
| 97 | + Select(sb, 1, 100000); |
| 98 | + Console.Write(sb.ToString()); |
| 99 | + sb.Clear(); |
| 100 | + |
| 101 | + Console.WriteLine("更新:"); |
| 102 | + Update(sb, 1000, 1); |
| 103 | + Console.Write(sb.ToString()); |
| 104 | + sb.Clear(); |
| 105 | + Update(sb, 1000, 10); |
| 106 | + Console.Write(sb.ToString()); |
| 107 | + sb.Clear(); |
| 108 | + |
| 109 | + Update(sb, 1, 1000); |
| 110 | + Console.Write(sb.ToString()); |
| 111 | + sb.Clear(); |
| 112 | + Update(sb, 1, 10000); |
| 113 | + Console.Write(sb.ToString()); |
| 114 | + sb.Clear(); |
| 115 | + Update(sb, 1, 50000); |
| 116 | + Console.Write(sb.ToString()); |
| 117 | + sb.Clear(); |
| 118 | + Update(sb, 1, 100000); |
| 119 | + Console.Write(sb.ToString()); |
| 120 | + sb.Clear(); |
| 121 | + |
| 122 | + Console.WriteLine("测试结束,按任意键退出..."); |
| 123 | + Console.ReadKey(); |
| 124 | + } |
| 125 | + |
| 126 | + static void Select(StringBuilder sb, int forTime, int size) |
| 127 | + { |
| 128 | + Stopwatch sw = new Stopwatch(); |
| 129 | + sw.Restart(); |
| 130 | + for (var a = 0; a < forTime; a++) |
| 131 | + fsql.Select<Song>().Limit(size).ToList(); |
| 132 | + sw.Stop(); |
| 133 | + sb.AppendLine($"FreeSql Select {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms"); |
| 134 | + |
| 135 | + //sw.Restart(); |
| 136 | + //for (var a = 0; a < forTime; a++) |
| 137 | + // sugar.Queryable<Song>().Take(size).ToList(); |
| 138 | + //sw.Stop(); |
| 139 | + //sb.AppendLine($"SqlSugar Select {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms"); |
| 140 | + } |
| 141 | + |
| 142 | + static void Insert(StringBuilder sb, int forTime, int size) |
| 143 | + { |
| 144 | + var songs = Enumerable.Range(0, size).Select(a => new Song |
| 145 | + { |
| 146 | + Create_time = DateTime.Now, |
| 147 | + Is_deleted = false, |
| 148 | + Title = $"Insert_{a}", |
| 149 | + Url = $"Url_{a}" |
| 150 | + }); |
| 151 | + |
| 152 | + //预热 |
| 153 | + fsql.Insert(songs.First()).ExecuteAffrows(); |
| 154 | + //sugar.Insertable(songs.First()).ExecuteCommand(); |
| 155 | + Stopwatch sw = new Stopwatch(); |
| 156 | + |
| 157 | + sw.Restart(); |
| 158 | + for (var a = 0; a < forTime; a++) |
| 159 | + { |
| 160 | + fsql.Insert(songs).ExecuteAffrows(); |
| 161 | + //using (var db = new FreeSongContext()) { |
| 162 | + // //db.Configuration.AutoDetectChangesEnabled = false; |
| 163 | + // db.Songs.AddRange(songs.ToArray()); |
| 164 | + // db.SaveChanges(); |
| 165 | + //} |
| 166 | + } |
| 167 | + sw.Stop(); |
| 168 | + sb.AppendLine($"FreeSql Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms"); |
| 169 | + |
| 170 | + //sw.Restart(); |
| 171 | + //Exception sugarEx = null; |
| 172 | + //try |
| 173 | + //{ |
| 174 | + // for (var a = 0; a < forTime; a++) |
| 175 | + // sugar.Insertable(songs.ToArray()).ExecuteCommand(); |
| 176 | + //} |
| 177 | + //catch (Exception ex) |
| 178 | + //{ |
| 179 | + // sugarEx = ex; |
| 180 | + //} |
| 181 | + //sw.Stop(); |
| 182 | + //sb.AppendLine($"SqlSugar Insert {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms" + (sugarEx != null ? $"成绩无效,错误:{sugarEx.Message}" : "")); |
| 183 | + } |
| 184 | + |
| 185 | + static void Update(StringBuilder sb, int forTime, int size) |
| 186 | + { |
| 187 | + Stopwatch sw = new Stopwatch(); |
| 188 | + |
| 189 | + var songs = fsql.Select<Song>().Limit(size).ToList(); |
| 190 | + sw.Restart(); |
| 191 | + for (var a = 0; a < forTime; a++) |
| 192 | + { |
| 193 | + fsql.Update<Song>().SetSource(songs).ExecuteAffrows(); |
| 194 | + } |
| 195 | + sw.Stop(); |
| 196 | + sb.AppendLine($"FreeSql Update {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms"); |
| 197 | + |
| 198 | + //songs = sugar.Queryable<Song>().Take(size).ToList(); |
| 199 | + //sw.Restart(); |
| 200 | + //Exception sugarEx = null; |
| 201 | + //try |
| 202 | + //{ |
| 203 | + // for (var a = 0; a < forTime; a++) |
| 204 | + // sugar.Updateable(songs).ExecuteCommand(); |
| 205 | + //} |
| 206 | + //catch (Exception ex) |
| 207 | + //{ |
| 208 | + // sugarEx = ex; |
| 209 | + //} |
| 210 | + //sw.Stop(); |
| 211 | + //sb.AppendLine($"SqlSugar Update {size}条数据,循环{forTime}次,耗时{sw.ElapsedMilliseconds}ms" + (sugarEx != null ? $"成绩无效,错误:{sugarEx.Message}" : "")); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + [FreeSql.DataAnnotations.Table(Name = "freesql_song")] |
| 216 | + //[SugarTable("sugar_song")] |
| 217 | + public class Song |
| 218 | + { |
| 219 | + [FreeSql.DataAnnotations.Column(IsIdentity = true)] |
| 220 | + //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] |
| 221 | + public int Id { get; set; } |
| 222 | + public DateTime? Create_time { get; set; } |
| 223 | + public bool? Is_deleted { get; set; } |
| 224 | + public string Title { get; set; } |
| 225 | + public string Url { get; set; } |
| 226 | + |
| 227 | + //[SugarColumn(IsIgnore = true)] |
| 228 | + public virtual ICollection<Tag> Tags { get; set; } |
| 229 | + } |
| 230 | + [FreeSql.DataAnnotations.Table(Name = "freesql_song_tag")] |
| 231 | + //[SugarTable("sugar_song_tag")] |
| 232 | + public class Song_tag |
| 233 | + { |
| 234 | + public int Song_id { get; set; } |
| 235 | + //[SugarColumn(IsIgnore = true)] |
| 236 | + public virtual Song Song { get; set; } |
| 237 | + |
| 238 | + public int Tag_id { get; set; } |
| 239 | + //[SugarColumn(IsIgnore = true)] |
| 240 | + public virtual Tag Tag { get; set; } |
| 241 | + } |
| 242 | + [FreeSql.DataAnnotations.Table(Name = "freesql_tag")] |
| 243 | + //[SugarTable("sugar_tag")] |
| 244 | + public class Tag |
| 245 | + { |
| 246 | + [FreeSql.DataAnnotations.Column(IsIdentity = true)] |
| 247 | + //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] |
| 248 | + public int Id { get; set; } |
| 249 | + public int? Parent_id { get; set; } |
| 250 | + //[SugarColumn(IsIgnore = true)] |
| 251 | + public virtual Tag Parent { get; set; } |
| 252 | + |
| 253 | + public decimal? Ddd { get; set; } |
| 254 | + public string Name { get; set; } |
| 255 | + |
| 256 | + //[SugarColumn(IsIgnore = true)] |
| 257 | + public virtual ICollection<Song> Songs { get; set; } |
| 258 | + //[SugarColumn(IsIgnore = true)] |
| 259 | + public virtual ICollection<Tag> Tags { get; set; } |
| 260 | + } |
| 261 | +} |
0 commit comments