SqlSugar/Src/Asp.Net/PerformanceTest/TestItems/TestGetAll.cs

87 lines
2.4 KiB
C#
Raw Normal View History

2017-09-21 13:52:52 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Dapper;
using SqlSugar;
using Dapper.Contrib.Extensions;
2017-09-22 14:12:58 +08:00
using System.Data.Entity;
2017-09-21 13:52:52 +08:00
2017-09-21 14:22:23 +08:00
namespace PerformanceTest.TestItems
2017-09-21 13:52:52 +08:00
{
2017-09-21 14:15:16 +08:00
public class TestGetAll
2017-09-21 13:52:52 +08:00
{
2017-09-21 14:38:40 +08:00
public void Init(OrmType type)
2017-09-21 13:52:52 +08:00
{
2017-09-22 14:12:58 +08:00
Database.SetInitializer<EFContext>(null);
2019-12-12 19:26:15 +08:00
Console.WriteLine("测试一次读取10万条数据的速度");
2017-09-21 14:15:16 +08:00
var eachCount = 1;
2017-09-21 14:57:18 +08:00
var beginDate = DateTime.Now;
2017-09-21 13:52:52 +08:00
for (int i = 0; i < 10; i++)
{
2017-09-21 14:38:40 +08:00
switch (type)
{
case OrmType.SqlSugar:
SqlSugar(eachCount);
break;
case OrmType.Dapper:
Dapper(eachCount);
break;
2017-09-22 14:12:58 +08:00
case OrmType.EF:
EF(eachCount);
break;
2017-09-21 14:38:40 +08:00
default:
break;
}
2017-09-21 13:52:52 +08:00
}
2017-09-21 14:57:18 +08:00
Console.Write("总计:" + (DateTime.Now - beginDate).TotalMilliseconds / 1000.0);
2017-09-21 13:52:52 +08:00
}
private static void SqlSugar(int eachCount)
{
GC.Collect();//回收资源
2017-09-21 14:21:16 +08:00
System.Threading.Thread.Sleep(1);//休息1秒
2017-09-21 13:52:52 +08:00
PerHelper.Execute(eachCount, "SqlSugar", () =>
{
2017-09-21 14:46:29 +08:00
using (SqlSugarClient conn = Config.GetSugarConn())
2017-09-21 13:52:52 +08:00
{
2017-09-21 14:46:29 +08:00
var list2 = conn.Queryable<Test>().ToList();
2017-09-21 13:52:52 +08:00
}
});
}
2017-09-21 14:46:29 +08:00
2017-09-21 13:52:52 +08:00
private static void Dapper(int eachCount)
{
GC.Collect();//回收资源
2017-09-21 14:21:16 +08:00
System.Threading.Thread.Sleep(1);//休息1秒
2017-09-21 13:52:52 +08:00
PerHelper.Execute(eachCount, "Dapper", () =>
{
2017-09-21 14:15:16 +08:00
using (SqlConnection conn = new SqlConnection(Config.connectionString))
2017-09-21 13:52:52 +08:00
{
2017-09-21 14:15:16 +08:00
var list = conn.GetAll<Test>();
2017-09-21 13:52:52 +08:00
}
});
}
2017-09-22 14:12:58 +08:00
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().ToList();
}
});
}
2017-09-21 13:52:52 +08:00
}
}