Browse Source

性能对比

pull/1/head
skx 4 years ago
parent
commit
4cd25576fa
  1. 3
      Src/Asp.Net/PerformanceTest/Common/OrmType.cs
  2. 14
      Src/Asp.Net/PerformanceTest/PerformanceTest.csproj
  3. 10
      Src/Asp.Net/PerformanceTest/Program.cs
  4. 19
      Src/Asp.Net/PerformanceTest/TestItems/TestGetById.cs
  5. 81
      Src/Asp.Net/PerformanceTest/TestItems/TestLike.cs
  6. 4
      Src/Asp.Net/PerformanceTest/packages.config

3
Src/Asp.Net/PerformanceTest/Common/OrmType.cs

@ -10,6 +10,7 @@ namespace PerformanceTest
{
SqlSugar,
Dapper,
EF
EF,
FREE
}
}

14
Src/Asp.Net/PerformanceTest/PerformanceTest.csproj

@ -49,13 +49,26 @@
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FreeSql, Version=2.0.101.0, Culture=neutral, PublicKeyToken=a33928e5d4a4b39c, processorArchitecture=MSIL">
<HintPath>..\packages\FreeSql.2.0.101\lib\net45\FreeSql.dll</HintPath>
</Reference>
<Reference Include="FreeSql.Provider.SqlServer, Version=2.0.101.0, Culture=neutral, PublicKeyToken=d313b98af285bd88, processorArchitecture=MSIL">
<HintPath>..\packages\FreeSql.Provider.SqlServer.2.0.101\lib\net451\FreeSql.Provider.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="SyntacticSugar, Version=2.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\SyntacticSugar.2.4.1\lib\net40\SyntacticSugar.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.Common, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.Common.4.3.0\lib\net451\System.Data.Common.dll</HintPath>
</Reference>
<Reference Include="System.Data.Linq" />
<Reference Include="System.Data.SqlClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SqlClient.4.8.2\lib\net451\System.Data.SqlClient.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -74,6 +87,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestItems\TestGetSql.cs" />
<Compile Include="TestItems\TestInsert.cs" />
<Compile Include="TestItems\TestLike.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

10
Src/Asp.Net/PerformanceTest/Program.cs

@ -18,8 +18,8 @@ namespace PerformanceTest
{
InitData();
var type = DemoType.Insert;
var ormType = OrmType.EF;
var type = DemoType.GetById;
var ormType = OrmType.SqlSugar;
switch (type)
{
case DemoType.GetAll:
@ -34,6 +34,9 @@ namespace PerformanceTest
case DemoType.Insert:
new TestInsert().Init(ormType);
break;
case DemoType.Like:
new TestLike().Init(ormType);
break;
default:
break;
}
@ -78,7 +81,8 @@ namespace PerformanceTest
GetAll,
GetById,
GetSql,
Insert
Insert,
Like
}
}
}

19
Src/Asp.Net/PerformanceTest/TestItems/TestGetById.cs

@ -33,6 +33,9 @@ namespace PerformanceTest.TestItems
case OrmType.EF:
EF(eachCount);
break;
case OrmType.FREE:
Free(eachCount);
break;
default:
break;
}
@ -50,11 +53,25 @@ namespace PerformanceTest.TestItems
{
using (SqlSugarClient conn = Config.GetSugarConn())
{
var list2 = conn.Queryable<Test>().InSingle(1);
var list2 = conn.Queryable<Test>().First(it=>it.Id==1);
}
});
}
private static void Free(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "free", () =>
{
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer, Config.connectionString)
.UseAutoSyncStructure(false) //自动同步实体结构到数据库
.Build();
var list2 = fsql.Queryable<Test>().Where(it=>it.Id==1).First();
//用.First(it=>it.Id==1)报错
});
}
private static void Dapper(int eachCount)
{
GC.Collect();//回收资源

81
Src/Asp.Net/PerformanceTest/TestItems/TestLike.cs

@ -0,0 +1,81 @@
using Dapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PerformanceTest.TestItems
{
public class TestLike
{
public void Init(OrmType type)
{
Database.SetInitializer<EFContext>(null);
Console.WriteLine("测试SQL查询的速度");
var eachCount = 1;
var beginDate = DateTime.Now;
for (int i = 0; i < 20; i++)
{
switch (type)
{
case OrmType.SqlSugar:
SqlSugar(eachCount);
break;
case OrmType.Dapper:
Dapper(eachCount);
break;
case OrmType.EF:
EF(eachCount);
break;
case OrmType.FREE:
Free(eachCount);
break;
}
}
Console.Write("总计:" + (DateTime.Now - beginDate).TotalMilliseconds / 1000.0);
}
private static void Free(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "Free like ", () =>
{
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.SqlServer,Config.connectionString)
.UseAutoSyncStructure(false) //自动同步实体结构到数据库
.Build();
var list2 = fsql.Queryable<Test>().Where(it => it.F_String.Contains("abc")).ToList();
});
}
private static void SqlSugar(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "SqlSugar like ", () =>
{
SqlSugarClient conn = Config.GetSugarConn();
var list2 = conn.Queryable<Test>().Where(it => it.F_String.Contains("abc")).ToList();
});
}
private static void Dapper(int eachCount)
{
throw new Exception("未实现");
}
private static void EF(int eachCount)
{
throw new Exception("未实现");
}
}
}

4
Src/Asp.Net/PerformanceTest/packages.config

@ -4,5 +4,9 @@
<package id="Dapper.Contrib" version="1.50.0" targetFramework="net452" />
<package id="Dapper.Extension" version="1.0.0.1" targetFramework="net452" />
<package id="EntityFramework" version="6.1.3" targetFramework="net452" />
<package id="FreeSql" version="2.0.101" targetFramework="net452" />
<package id="FreeSql.Provider.SqlServer" version="2.0.101" targetFramework="net452" />
<package id="SyntacticSugar" version="2.4.1" targetFramework="net452" />
<package id="System.Data.Common" version="4.3.0" targetFramework="net452" />
<package id="System.Data.SqlClient" version="4.8.2" targetFramework="net452" />
</packages>
Loading…
Cancel
Save