forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (66 loc) · 2.33 KB
/
Program.cs
File metadata and controls
78 lines (66 loc) · 2.33 KB
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
77
78
using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Samples.Model;
namespace Samples
{
public class Program
{
private static void Main()
{
// Warmup
using (var db = new AdventureWorksContext())
{
var customer = db.Customers.First();
}
RunTest(
accountNumbers =>
{
using (var db = new AdventureWorksContext())
{
foreach (var id in accountNumbers)
{
// Use a regular auto-compiled query
var customer = db.Customers.Single(c => c.AccountNumber == id);
}
}
},
name: "Regular");
RunTest(
accountNumbers =>
{
// Create an explicit compiled query
var query = EF.CompileQuery((AdventureWorksContext db, string id)
=> db.Customers.Single(c => c.AccountNumber == id));
using (var db = new AdventureWorksContext())
{
foreach (var id in accountNumbers)
{
// Invoke the compiled query
query(db, id);
}
}
},
name: "Compiled");
}
private static void RunTest(Action<string[]> test, string name)
{
var accountNumbers = GetAccountNumbers(500);
var stopwatch = new Stopwatch();
stopwatch.Start();
test(accountNumbers);
stopwatch.Stop();
Console.WriteLine($"{name}: {stopwatch.ElapsedMilliseconds.ToString().PadLeft(4)}ms");
}
private static string[] GetAccountNumbers(int count)
{
var accountNumbers = new string[count];
for (var i = 0; i < count; i++)
{
accountNumbers[i] = "AW" + (i + 1).ToString().PadLeft(8, '0');
}
return accountNumbers;
}
}
}