diff --git a/Examples/benchmarker/Program.cs b/Examples/benchmarker/Program.cs index d4fa9473..43d35497 100644 --- a/Examples/benchmarker/Program.cs +++ b/Examples/benchmarker/Program.cs @@ -2,11 +2,12 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.Data.SqlClient; using System.Linq; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; using Microsoft.EntityFrameworkCore; -//using SqlSugar; +using SqlSugar; namespace FreeSql.Bechmarker { @@ -15,33 +16,39 @@ namespace FreeSql.Bechmarker { public static void Main(string[] args) { - var summaryInsert = BenchmarkRunner.Run(); + //var summaryInsert = BenchmarkRunner.Run(); var summarySelect = BenchmarkRunner.Run(); - var summaryUpdate = BenchmarkRunner.Run(); + //var summaryUpdate = BenchmarkRunner.Run(); + + 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=20") + .UseConnectionString(FreeSql.DataType.SqlServer, + "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=20", + 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() { - // //不欺负,让连接池100个最小 - // ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20", - // 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 - // }); - //} + public static SqlSugarClient sugar + { + get => new SqlSugarClient(new ConnectionConfig() + { + ConnectionString = "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20", + 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 { @@ -58,19 +65,20 @@ namespace FreeSql.Bechmarker { public IEnumerable songs; - [Params(1, 500, 1000, 5000, 10000, 50000, 100000)] + [Params(1, 500)] public int size; [GlobalSetup] public void Setup() { - Orm.fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag)); - //Orm.sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag)); - //sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements + 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().Where(a => a.Id > 0).ExecuteAffrows(); - //Orm.sugar.Deleteable().Where(a => a.Id > 0).ExecuteCommand(); + Orm.sugar.Deleteable().Where(a => a.Id > 0).ExecuteCommand(); Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song"); songs = Enumerable.Range(0, size).Select(a => new Song @@ -83,7 +91,7 @@ namespace FreeSql.Bechmarker //预热 Orm.fsql.Insert(songs.First()).ExecuteAffrows(); - //Orm.sugar.Insertable(songs.First()).ExecuteCommand(); + Orm.sugar.Insertable(songs.First()).ExecuteCommand(); using (var db = new SongContext()) { //db.Configuration.AutoDetectChangesEnabled = false; @@ -95,8 +103,8 @@ namespace FreeSql.Bechmarker [Benchmark] public int FreeSqlInsert() => Orm.fsql.Insert(songs).ExecuteAffrows(); - //[Benchmark] - //public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand(); + [Benchmark] + public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand(); [Benchmark] public int EfCoreInsert() @@ -108,6 +116,20 @@ namespace FreeSql.Bechmarker return db.SaveChanges(); } } + + [Benchmark] + public int DapperInsert() + { + using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=31")) + { + 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] @@ -115,7 +137,7 @@ namespace FreeSql.Bechmarker { public List songs; - [Params(1, 500, 1000, 5000, 10000, 50000, 100000)] + [Params(1, 500)] public int size; [GlobalSetup] @@ -127,8 +149,8 @@ namespace FreeSql.Bechmarker [Benchmark] public int FreeSqlUpdate() => Orm.fsql.Update().SetSource(songs).ExecuteAffrows(); - //[Benchmark] - //public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand(); + [Benchmark] + public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand(); [Benchmark] public int EfCoreUpdate() @@ -140,26 +162,94 @@ namespace FreeSql.Bechmarker return db.SaveChanges(); } } + + [Benchmark] + public int DapperUpdate() + { + using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=31")) + { + 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, 1000, 5000, 10000, 50000, 100000)] + [Params(1, 500)] public int size; + [IterationSetup] + public void Setup2() + { + Orm.fsql.Select().Limit(1).ToList(); + Orm.sugar.Queryable().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;Min Pool Size=21;Max Pool Size=31")) + { + Dapper.SqlMapper.Query(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().Where(a => a.Id > 0).ExecuteAffrows(); + Orm.sugar.Deleteable().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;Min Pool Size=21;Max Pool Size=31")) + { + 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 FreeSqlSelect() => Orm.fsql.Select().Limit(size).ToList(); - //[Benchmark] - //public List SqlSugarSelect() => Orm.sugar.Queryable().Take(size).ToList(); + [Benchmark] + public List SqlSugarSelect() => Orm.sugar.Queryable().Take(size).ToList(); [Benchmark] public List EfCoreSelect() @@ -169,15 +259,24 @@ namespace FreeSql.Bechmarker return db.Songs.Take(size).AsNoTracking().ToList(); } } + + [Benchmark] + public List DapperSelete() + { + using (var conn = new SqlConnection("Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=21;Max Pool Size=31")) + { + return Dapper.SqlMapper.Query(conn, $"select top {size} Id,Create_time,Is_deleted,Title,Url from dapper_song").ToList(); + } + } } [FreeSql.DataAnnotations.Table(Name = "freesql_song")] - //[SugarTable("sugar_song")] + [SugarTable("sugar_song")] [Table("efcore_song")] public class Song { [FreeSql.DataAnnotations.Column(IsIdentity = true)] - //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } @@ -185,50 +284,6 @@ namespace FreeSql.Bechmarker public bool? Is_deleted { get; set; } public string Title { get; set; } public string Url { get; set; } - - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual ICollection Tags { get; set; } - } - [FreeSql.DataAnnotations.Table(Name = "freesql_song_tag")] - //[SugarTable("sugar_song_tag")] - [Table("efcore_song_tag")] - public class Song_tag - { - public int Song_id { get; set; } - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual Song Song { get; set; } - - public int Tag_id { get; set; } - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual Tag Tag { get; set; } - } - [FreeSql.DataAnnotations.Table(Name = "freesql_tag")] - //[SugarTable("sugar_tag")] - [Table("efcore_tag")] - public class Tag - { - [FreeSql.DataAnnotations.Column(IsIdentity = true)] - //[SugarColumn(IsPrimaryKey = true, IsIdentity = true)] - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int Id { get; set; } - public int? Parent_id { get; set; } - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual Tag Parent { get; set; } - - public decimal? Ddd { get; set; } - public string Name { get; set; } - - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual ICollection Songs { get; set; } - //[SugarColumn(IsIgnore = true)] - [NotMapped] - public virtual ICollection Tags { get; set; } } } diff --git a/Examples/benchmarker/benchmarker.csproj b/Examples/benchmarker/benchmarker.csproj index 57d35001..92101464 100644 --- a/Examples/benchmarker/benchmarker.csproj +++ b/Examples/benchmarker/benchmarker.csproj @@ -7,12 +7,21 @@ + + - - + + ..\..\FreeSql\bin\Release\netstandard2.0\FreeSql.dll + + + ..\..\Providers\FreeSql.Provider.SqlServer\bin\Release\netstandard2.0\FreeSql.Provider.SqlServer.dll + + + ..\..\..\SqlSugar-SqlSugar5 (1)\SqlSugar-SqlSugar5\Src\Asp.NetCore2\SqlSeverTest\SqlSugar\bin\Release\netstandard2.0\SqlSugar.dll +