Skip to content

Commit 3b0cd9a

Browse files
yesmeyNickCraver
authored andcommitted
Updated Hand Coded benchmarks (#1206)
Hand coded version did not operate under the same circumstances as others. - Add CommandBehavior.SingleResult and CommandBehavior.SingleRow. All other framworks does this under the hood. - Run SqlCommand.Prepare() (perhaps it should be benchmarked as "Compiled"?) - Use GetNullableValue because it's easier to read and slightly faster - _table.Rows was incrementally added, which made the benchmark run slower the longer it executed. Added _table.Rows.Clear() to prevent the growth. - Changed select to use * like other benchmarks
1 parent 77cd19d commit 3b0cd9a

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

Dapper.Tests.Performance/Benchmarks.HandCoded.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public class HandCodedBenchmarks : BenchmarkBase
1919
public void Setup()
2020
{
2121
BaseSetup();
22-
_postCommand = new SqlCommand()
23-
{
24-
Connection = _connection,
25-
CommandText = @"select Id, [Text], [CreationDate], LastChangeDate,
26-
Counter1,Counter2,Counter3,Counter4,Counter5,Counter6,Counter7,Counter8,Counter9 from Posts where Id = @Id"
27-
};
22+
_postCommand = new SqlCommand("select * from Posts where Id = @Id", _connection);
2823
_idParam = _postCommand.Parameters.Add("@Id", SqlDbType.Int);
24+
_postCommand.Prepare();
2925
#if !NETCOREAPP1_0
3026
_table = new DataTable
3127
{
@@ -55,7 +51,7 @@ public Post SqlCommand()
5551
Step();
5652
_idParam.Value = i;
5753

58-
using (var reader = _postCommand.ExecuteReader())
54+
using (var reader = _postCommand.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.SingleRow))
5955
{
6056
reader.Read();
6157
return new Post
@@ -65,15 +61,15 @@ public Post SqlCommand()
6561
CreationDate = reader.GetDateTime(2),
6662
LastChangeDate = reader.GetDateTime(3),
6763

68-
Counter1 = reader.IsDBNull(4) ? null : (int?)reader.GetInt32(4),
69-
Counter2 = reader.IsDBNull(5) ? null : (int?)reader.GetInt32(5),
70-
Counter3 = reader.IsDBNull(6) ? null : (int?)reader.GetInt32(6),
71-
Counter4 = reader.IsDBNull(7) ? null : (int?)reader.GetInt32(7),
72-
Counter5 = reader.IsDBNull(8) ? null : (int?)reader.GetInt32(8),
73-
Counter6 = reader.IsDBNull(9) ? null : (int?)reader.GetInt32(9),
74-
Counter7 = reader.IsDBNull(10) ? null : (int?)reader.GetInt32(10),
75-
Counter8 = reader.IsDBNull(11) ? null : (int?)reader.GetInt32(11),
76-
Counter9 = reader.IsDBNull(12) ? null : (int?)reader.GetInt32(12)
64+
Counter1 = reader.GetNullableValue<int>(4),
65+
Counter2 = reader.GetNullableValue<int>(5),
66+
Counter3 = reader.GetNullableValue<int>(6),
67+
Counter4 = reader.GetNullableValue<int>(7),
68+
Counter5 = reader.GetNullableValue<int>(8),
69+
Counter6 = reader.GetNullableValue<int>(9),
70+
Counter7 = reader.GetNullableValue<int>(10),
71+
Counter8 = reader.GetNullableValue<int>(11),
72+
Counter9 = reader.GetNullableValue<int>(12)
7773
};
7874
}
7975
}
@@ -83,13 +79,13 @@ public dynamic DataTableDynamic()
8379
{
8480
Step();
8581
_idParam.Value = i;
82+
_table.Rows.Clear();
8683
var values = new object[13];
87-
using (var reader = _postCommand.ExecuteReader())
84+
using (var reader = _postCommand.ExecuteReader(CommandBehavior.SingleResult | CommandBehavior.SingleRow))
8885
{
8986
reader.Read();
9087
reader.GetValues(values);
91-
_table.Rows.Add(values);
92-
return _table.Rows[_table.Rows.Count - 1];
88+
return _table.Rows.Add(values);
9389
}
9490
}
9591
}

0 commit comments

Comments
 (0)