由于这周比较忙,所以本来想做的性能测试,一直没时间,想想还是今天给补上吧
由于很多人都担心性能问题,封装之后跟Dapper的性能差距是多少,今天我给出我的测试方法,仅供参考.
- 创建IDbConnection;(DapperLambda 已经把IDbConnection封装在DbContext,所以创建的是DbContext)
public class DBHelper
{
private static string localStr = "server=(local);User ID=sa;Password=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700";
public static DbContext GetContext()
{
return new DbContext().ConnectionString(localStr);
}
public static System.Data.IDbConnection GetDapperConnection()
{
return new System.Data.SqlClient.SqlConnection(localStr);
}
}
2.主要针对几个增删改查,几个做比较,但是用到Dapper比较简单,都是执行SQL+参数。。
1).查询语句的比较
public static long DapperSelect()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var conn = DBHelper.GetDapperConnection())
{
var item = conn.Query<Models.MobileForTest>("SELECT * FROM MobileForTest mft WHERE mft.ID=@ID", new { ID = r.Next(, ) });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLSelect()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Sql("SELECT * FROM MobileForTest mft WHERE mft.ID=@ID", new { ID = r.Next(, ) }).QueryMany<MobileForTest>();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSelectByID()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Select<MobileForTest>(r.Next(, ));
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSelectByLambda()
{ Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
var item = context.Select<MobileForTest>(p => p.ID == r.Next(, )).QueryMany();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
原本计划是起四个线程,在各自线程里调用相应的方法500次,取总耗时时间,发现线程启动的顺序,对测试的结果有明显的影响。因此改成同步的调用每个方法500次,取平均时长。测试结果如下如。
跟预期差不多是一致。
2.单条数据插入的
public static long DapperSQLInsert()
{
Stopwatch timer = new Stopwatch();
timer.Start();
using (var conn = DBHelper.GetDapperConnection())
{
conn.Execute(@"INSERT INTO [dbo].[MobileForTest]
([MobileHolder]
,[MobilePhone]
,[Status])
VALUES
(@MobileHolder
,@MobilePhone
,@Status)", new { MobileHolder = "InsterWithTran", MobilePhone = "", Status = });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLInsert()
{
Stopwatch timer = new Stopwatch();
timer.Start();
using (var context = DBHelper.GetContext())
{
context.Sql(@"INSERT INTO [dbo].[MobileForTest]
([MobileHolder]
,[MobilePhone]
,[Status])
VALUES
(@MobileHolder
,@MobilePhone
,@Status)").Parameter("MobileHolder", "DapperLambdaSQLInsert")
.Parameter("MobilePhone", "")
.Parameter("Status", ).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaInsert()
{
List<MobileForTest> ls = new List<MobileForTest>();
Stopwatch timer = new Stopwatch();
timer.Start();
using (var context = DBHelper.GetContext())
{
context.Insert<MobileForTest>(new MobileForTest { MobileHolder = "DapperLambdaInsert", MobilePhone = "", Status = }).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
循环500次执行,取平均耗时。
3.更新方法测试
public static long DapperSQLUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var conn = DBHelper.GetDapperConnection())
{
conn.Execute("UPDATE MobileForTest SET MobileHolder = @MobileHolder WHERE ID=@ID", new { MobileHolder = "DapperSQLUpdate", ID = r.Next(, ) });
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaSQLUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
context.Sql("UPDATE MobileForTest SET MobileHolder = @MobileHolder WHERE ID=@ID", new { MobileHolder = "DapperLambdaSQLUpdate", ID = r.Next(, ) }).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
public static long DapperLambdaUpdate()
{
Stopwatch timer = new Stopwatch();
timer.Start();
Random r = new Random();
using (var context = DBHelper.GetContext())
{
context.Update<MobileForTest>().Set(new { MobileHolder = "DapperLambdaUpdate" }).Where(p => p.ID == r.Next(, )).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}
总体来说,测试的结果还在预期范围之类,在使用方便和些许的性能中各位抉择。如果有时间的话,考虑换掉Dapper,再重新比较一下。坚持。。。。