/
Benchmark.cs
76 lines (61 loc) · 2.09 KB
/
Benchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace Test
{
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using global::Benchmark;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Data;
using System.Linq;
[MemoryDiagnoser]
public class Benchmark
{
private readonly string _connstring = "Server=.; Integrated Security=sspi; Initial Catalog=AdventureWorks2019;Encrypt=false";
private SqlConnection _conn;
private AdventureWorks2019Context _dbcontext;
public int Count { get; set; }
[GlobalSetup]
public void GlobalSetup()
{
_conn = new SqlConnection(_connstring);
_conn.Open();
var contextOptions = new DbContextOptionsBuilder<AdventureWorks2019Context>()
.UseSqlServer(_connstring)
.Options;
_dbcontext = new AdventureWorks2019Context(contextOptions);
}
[Benchmark(Baseline = true)]
public List<short> ReadDataUsingDataReader()
{
var sql = "select OrderQty from Sales.SalesOrderDetail";
using var cmd = new SqlCommand(sql, _conn);
using var reader = cmd.ExecuteReader();
var result = new List<short>();
while (reader.Read())
{
result.Add(reader.GetInt16(0));
}
return result;
}
[Benchmark]
public List<short> ReadDataUsingDataSet()
{
var sql = "select OrderQty from Sales.SalesOrderDetail";
var ds = new DataSet();
var adapter = new SqlDataAdapter(sql, _conn);
adapter.Fill(ds);
var result = new List<short>();
foreach (DataRow row in ds.Tables[0].Rows)
{
result.Add((short)row[0]);
}
return result;
}
[Benchmark]
public List<short> ReadDataUsingEntityFramework()
{
return _dbcontext.SalesOrderDetails.Select(x => x.OrderQty).ToList();
}
}
}