SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

7 years ago
using Dapper.Contrib.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
7 years ago
using System.Data.Entity;
7 years ago
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PerformanceTest.TestItems
{
public class TestGetById
{
7 years ago
public void Init(OrmType type)
7 years ago
{
7 years ago
Database.SetInitializer<EFContext>(null);
7 years ago
Console.WriteLine("测试一次读取1条数据的速度");
var eachCount = 1000;
7 years ago
var beginDate = DateTime.Now;
7 years ago
for (int i = 0; i < 20; i++)
7 years ago
{
7 years ago
switch (type)
{
case OrmType.SqlSugar:
SqlSugar(eachCount);
break;
case OrmType.Dapper:
Dapper(eachCount);
break;
7 years ago
case OrmType.EF:
EF(eachCount);
break;
4 years ago
case OrmType.FREE:
Free(eachCount);
break;
7 years ago
default:
break;
}
7 years ago
}
7 years ago
7 years ago
Console.Write("总计:"+(DateTime.Now-beginDate).TotalMilliseconds/1000.0);
7 years ago
}
private static void SqlSugar(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "SqlSugar", () =>
{
7 years ago
using (SqlSugarClient conn = Config.GetSugarConn())
7 years ago
{
4 years ago
var list2 = conn.Queryable<Test>().First(it=>it.Id==1);
7 years ago
}
});
}
4 years ago
private static void Free(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
7 years ago
4 years ago
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)报错
});
}
7 years ago
private static void Dapper(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "Dapper", () =>
{
using (SqlConnection conn = new SqlConnection(Config.connectionString))
{
7 years ago
var list = conn.Get<Test>(1);
7 years ago
}
});
}
7 years ago
private static void EF(int eachCount)
{
GC.Collect();//回收资源
System.Threading.Thread.Sleep(1);//休息1秒
PerHelper.Execute(eachCount, "EF", () =>
{
using (EFContext conn = new EFContext(Config.connectionString))
{
var list = conn.TestList.AsNoTracking().Single(it=>it.Id==1);
}
});
}
7 years ago
}
}