mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-22 02:32:50 +08:00
update bechmarker
This commit is contained in:
parent
d8996ff973
commit
36b6ad3747
@ -2,11 +2,12 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using BenchmarkDotNet.Attributes;
|
using BenchmarkDotNet.Attributes;
|
||||||
using BenchmarkDotNet.Running;
|
using BenchmarkDotNet.Running;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
//using SqlSugar;
|
using SqlSugar;
|
||||||
|
|
||||||
namespace FreeSql.Bechmarker
|
namespace FreeSql.Bechmarker
|
||||||
{
|
{
|
||||||
@ -15,33 +16,39 @@ namespace FreeSql.Bechmarker
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var summaryInsert = BenchmarkRunner.Run<OrmVsInsert>();
|
//var summaryInsert = BenchmarkRunner.Run<OrmVsInsert>();
|
||||||
var summarySelect = BenchmarkRunner.Run<OrmVsSelect>();
|
var summarySelect = BenchmarkRunner.Run<OrmVsSelect>();
|
||||||
var summaryUpdate = BenchmarkRunner.Run<OrmVsUpdate>();
|
//var summaryUpdate = BenchmarkRunner.Run<OrmVsUpdate>();
|
||||||
|
|
||||||
|
Console.ReadKey();
|
||||||
|
Console.ReadKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Orm
|
public class Orm
|
||||||
{
|
{
|
||||||
public static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
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")
|
//.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)
|
.UseAutoSyncStructure(false)
|
||||||
.UseNoneCommandParameter(true)
|
.UseNoneCommandParameter(true)
|
||||||
//.UseConfigEntityFromDbFirst(true)
|
//.UseConfigEntityFromDbFirst(true)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
//public static SqlSugarClient sugar {
|
public static SqlSugarClient sugar
|
||||||
// get => new SqlSugarClient(new ConnectionConfig() {
|
{
|
||||||
// //不欺负,让连接池100个最小
|
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=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Min Pool Size=20;Max Pool Size=20",
|
||||||
// //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.SqlServer,
|
||||||
// //DbType = DbType.MySql,
|
//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",
|
||||||
// IsAutoCloseConnection = true,
|
//DbType = DbType.MySql,
|
||||||
// InitKeyType = InitKeyType.Attribute
|
IsAutoCloseConnection = true,
|
||||||
// });
|
InitKeyType = InitKeyType.Attribute
|
||||||
//}
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
class SongContext : DbContext
|
class SongContext : DbContext
|
||||||
{
|
{
|
||||||
@ -58,19 +65,20 @@ namespace FreeSql.Bechmarker
|
|||||||
{
|
{
|
||||||
public IEnumerable<Song> songs;
|
public IEnumerable<Song> songs;
|
||||||
|
|
||||||
[Params(1, 500, 1000, 5000, 10000, 50000, 100000)]
|
[Params(1, 500)]
|
||||||
public int size;
|
public int size;
|
||||||
|
|
||||||
[GlobalSetup]
|
[GlobalSetup]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
{
|
{
|
||||||
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), typeof(Song_tag), typeof(Tag));
|
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "freesql_song");
|
||||||
//Orm.sugar.CodeFirst.InitTables(typeof(Song), typeof(Song_tag), typeof(Tag));
|
Orm.fsql.CodeFirst.SyncStructure(typeof(Song), "sugar_song");
|
||||||
//sugar创建表失败:SqlSugar.SqlSugarException: Sequence contains no elements
|
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.fsql.Delete<Song>().Where(a => a.Id > 0).ExecuteAffrows();
|
||||||
//Orm.sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
|
Orm.sugar.Deleteable<Song>().Where(a => a.Id > 0).ExecuteCommand();
|
||||||
Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song");
|
Orm.fsql.Ado.ExecuteNonQuery("delete from efcore_song");
|
||||||
|
|
||||||
songs = Enumerable.Range(0, size).Select(a => new Song
|
songs = Enumerable.Range(0, size).Select(a => new Song
|
||||||
@ -83,7 +91,7 @@ namespace FreeSql.Bechmarker
|
|||||||
|
|
||||||
//预热
|
//预热
|
||||||
Orm.fsql.Insert(songs.First()).ExecuteAffrows();
|
Orm.fsql.Insert(songs.First()).ExecuteAffrows();
|
||||||
//Orm.sugar.Insertable(songs.First()).ExecuteCommand();
|
Orm.sugar.Insertable(songs.First()).ExecuteCommand();
|
||||||
using (var db = new SongContext())
|
using (var db = new SongContext())
|
||||||
{
|
{
|
||||||
//db.Configuration.AutoDetectChangesEnabled = false;
|
//db.Configuration.AutoDetectChangesEnabled = false;
|
||||||
@ -95,8 +103,8 @@ namespace FreeSql.Bechmarker
|
|||||||
[Benchmark]
|
[Benchmark]
|
||||||
public int FreeSqlInsert() => Orm.fsql.Insert(songs).ExecuteAffrows();
|
public int FreeSqlInsert() => Orm.fsql.Insert(songs).ExecuteAffrows();
|
||||||
|
|
||||||
//[Benchmark]
|
[Benchmark]
|
||||||
//public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand();
|
public int SqlSugarInsert() => Orm.sugar.Insertable(songs.ToArray()).ExecuteCommand();
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public int EfCoreInsert()
|
public int EfCoreInsert()
|
||||||
@ -108,6 +116,20 @@ namespace FreeSql.Bechmarker
|
|||||||
return db.SaveChanges();
|
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]
|
[RPlotExporter, RankColumn]
|
||||||
@ -115,7 +137,7 @@ namespace FreeSql.Bechmarker
|
|||||||
{
|
{
|
||||||
public List<Song> songs;
|
public List<Song> songs;
|
||||||
|
|
||||||
[Params(1, 500, 1000, 5000, 10000, 50000, 100000)]
|
[Params(1, 500)]
|
||||||
public int size;
|
public int size;
|
||||||
|
|
||||||
[GlobalSetup]
|
[GlobalSetup]
|
||||||
@ -127,8 +149,8 @@ namespace FreeSql.Bechmarker
|
|||||||
[Benchmark]
|
[Benchmark]
|
||||||
public int FreeSqlUpdate() => Orm.fsql.Update<Song>().SetSource(songs).ExecuteAffrows();
|
public int FreeSqlUpdate() => Orm.fsql.Update<Song>().SetSource(songs).ExecuteAffrows();
|
||||||
|
|
||||||
//[Benchmark]
|
[Benchmark]
|
||||||
//public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand();
|
public int SqlSugarUpdate() => Orm.sugar.Updateable(songs).ExecuteCommand();
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public int EfCoreUpdate()
|
public int EfCoreUpdate()
|
||||||
@ -140,26 +162,94 @@ namespace FreeSql.Bechmarker
|
|||||||
return db.SaveChanges();
|
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]
|
[RPlotExporter, RankColumn]
|
||||||
public class OrmVsSelect
|
public class OrmVsSelect
|
||||||
{
|
{
|
||||||
|
|
||||||
[Params(1, 500, 1000, 5000, 10000, 50000, 100000)]
|
[Params(1, 500)]
|
||||||
public int size;
|
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;Min Pool Size=21;Max Pool Size=31"))
|
||||||
|
{
|
||||||
|
Dapper.SqlMapper.Query<Song>(conn, $"select top {1} Id,Create_time,Is_deleted,Title,Url from dapper_song").ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[GlobalSetup]
|
[GlobalSetup]
|
||||||
public void Setup()
|
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;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]
|
[Benchmark]
|
||||||
public List<Song> FreeSqlSelect() => Orm.fsql.Select<Song>().Limit(size).ToList();
|
public List<Song> FreeSqlSelect() => Orm.fsql.Select<Song>().Limit(size).ToList();
|
||||||
|
|
||||||
//[Benchmark]
|
[Benchmark]
|
||||||
//public List<Song> SqlSugarSelect() => Orm.sugar.Queryable<Song>().Take(size).ToList();
|
public List<Song> SqlSugarSelect() => Orm.sugar.Queryable<Song>().Take(size).ToList();
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public List<Song> EfCoreSelect()
|
public List<Song> EfCoreSelect()
|
||||||
@ -169,15 +259,24 @@ namespace FreeSql.Bechmarker
|
|||||||
return db.Songs.Take(size).AsNoTracking().ToList();
|
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;Min Pool Size=21;Max Pool Size=31"))
|
||||||
|
{
|
||||||
|
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")]
|
[FreeSql.DataAnnotations.Table(Name = "freesql_song")]
|
||||||
//[SugarTable("sugar_song")]
|
[SugarTable("sugar_song")]
|
||||||
[Table("efcore_song")]
|
[Table("efcore_song")]
|
||||||
public class Song
|
public class Song
|
||||||
{
|
{
|
||||||
[FreeSql.DataAnnotations.Column(IsIdentity = true)]
|
[FreeSql.DataAnnotations.Column(IsIdentity = true)]
|
||||||
//[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
|
||||||
[Key]
|
[Key]
|
||||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
@ -185,50 +284,6 @@ namespace FreeSql.Bechmarker
|
|||||||
public bool? Is_deleted { get; set; }
|
public bool? Is_deleted { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
|
|
||||||
//[SugarColumn(IsIgnore = true)]
|
|
||||||
[NotMapped]
|
|
||||||
public virtual ICollection<Tag> 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<Song> Songs { get; set; }
|
|
||||||
//[SugarColumn(IsIgnore = true)]
|
|
||||||
[NotMapped]
|
|
||||||
public virtual ICollection<Tag> Tags { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,12 +7,21 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
|
||||||
|
<PackageReference Include="Dapper" Version="2.0.78" />
|
||||||
|
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0" />
|
||||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.0-rc1.final" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\FreeSql\FreeSql.csproj" />
|
<Reference Include="FreeSql">
|
||||||
|
<HintPath>..\..\FreeSql\bin\Release\netstandard2.0\FreeSql.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="FreeSql.Provider.SqlServer">
|
||||||
|
<HintPath>..\..\Providers\FreeSql.Provider.SqlServer\bin\Release\netstandard2.0\FreeSql.Provider.SqlServer.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="SqlSugar">
|
||||||
|
<HintPath>..\..\..\SqlSugar-SqlSugar5 (1)\SqlSugar-SqlSugar5\Src\Asp.NetCore2\SqlSeverTest\SqlSugar\bin\Release\netstandard2.0\SqlSugar.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user