由於這周比較忙,所以本來想做的性能測試,一直沒時間,想想還是今天給補上吧
由於很多人都擔心性能問題,封裝之後跟Dapper的性能差距是多少,今天我給出我的測試方法,僅供參考.
1 public class DBHelper
2 {
3 private static string localStr = "server=(local);User ID=sa;Password=password01!;Database=LocalDB;Persist Security Info=True;Pooling=true;Max Pool Size=700";
4 public static DbContext GetContext()
5 {
6 return new DbContext().ConnectionString(localStr);
7 }
8 public static System.Data.IDbConnection GetDapperConnection()
9 {
10 return new System.Data.SqlClient.SqlConnection(localStr);
11 }
12 }
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(1, 3014) });
}
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(1, 3014) }).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(1, 3014));
}
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(1, 3014)).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 = "18922223333", Status = 0 });
}
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", "18912345678")
.Parameter("Status", 0).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 = "18911112222", Status = 0 }).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(1, 10000) });
}
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(1, 10000) }).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(1, 10000)).Execute();
}
timer.Stop();
return timer.ElapsedMilliseconds;
}

總體來說,測試的結果還在預期范圍之類,在使用方便和些許的性能中各位抉擇。如果有時間的話,考慮換掉Dapper,再重新比較一下。堅持。。。。