-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDatabaseQueryBenchmark.cs
63 lines (54 loc) · 1.63 KB
/
DatabaseQueryBenchmark.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
using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
#if EF_CORE
using Microsoft.Data.Sqlite;
namespace EntityFrameworkCore.CommonTools.Benchmarks
#else
using System.Data.Common;
namespace EntityFramework.CommonTools.Benchmarks
#endif
{
public class DatabaseQueryBenchmark
{
#if EF_CORE
private readonly SqliteConnection _connection = Context.CreateConnection();
#else
private readonly DbConnection _connection = Context.CreateConnection();
#endif
[Benchmark(Baseline = true)]
public object RawQuery()
{
using (var context = new Context(_connection))
{
DateTime today = DateTime.Now.Date;
return context.Users
.Where(u => u.Posts.Any(p => p.Date > today))
.FirstOrDefault();
}
}
[Benchmark]
public object ExpandableQuery()
{
using (var context = new Context(_connection))
{
return context.Users
.AsExpandable()
.Where(u => u.Posts.FilterToday().Any())
.ToList();
}
}
private readonly Random _random = new Random();
[Benchmark]
public object NotCachedQuery()
{
using (var context = new Context(_connection))
{
int[] postIds = new[] { _random.Next(), _random.Next() };
return context.Users
.Where(u => u.Posts.Any(p => postIds.Contains(p.Id)))
.ToList();
}
}
}
}