/
SimpleUpdatePipelineTests.cs
122 lines (103 loc) · 3.79 KB
/
SimpleUpdatePipelineTests.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Microsoft.EntityFrameworkCore.Benchmarks.Models.Orders;
using Xunit;
#pragma warning disable CA1034 // Nested types should not be visible
namespace Microsoft.EntityFrameworkCore.Benchmarks.UpdatePipeline
{
#pragma warning disable CA1052 // Static holder types should be Static or NotInheritable
public class SimpleUpdatePipelineTests
#pragma warning restore CA1052 // Static holder types should be Static or NotInheritable
{
public abstract class UpdatePipelineBase
{
protected static readonly OrdersFixture _fixture
= new OrdersFixture("Perf_UpdatePipeline_Simple_EF6", 0, 1000, 0, 0);
protected OrdersContext _context;
private DbContextTransaction _transaction;
private int _recordsAffected = -1;
[Params(true, false)]
public bool Async;
// NB: Unused. Only here for comparison to EF Core
[Params(true, false)]
public bool Batching;
[IterationSetup]
public virtual void InitializeContext()
{
_context = _fixture.CreateContext();
_transaction = _context.Database.BeginTransaction();
}
[IterationCleanup]
public virtual void CleanupContext()
{
if (_recordsAffected != -1)
{
Assert.Equal(1000, _recordsAffected);
}
_transaction.Dispose();
_context.Dispose();
}
[Benchmark]
public async Task UpdatePipeline()
{
_recordsAffected = Async
? await _context.SaveChangesAsync()
: _context.SaveChanges();
}
}
public class Insert : UpdatePipelineBase
{
[IterationSetup]
public override void InitializeContext()
{
base.InitializeContext();
var customers = _fixture.CreateCustomers(1000, setPrimaryKeys: false);
_context.Customers.AddRange(customers);
}
}
public class Update : UpdatePipelineBase
{
[IterationSetup]
public override void InitializeContext()
{
base.InitializeContext();
foreach (var customer in _context.Customers)
{
customer.FirstName += " Modified";
}
}
}
public class Delete : UpdatePipelineBase
{
[IterationSetup]
public override void InitializeContext()
{
base.InitializeContext();
_context.Customers.RemoveRange(_context.Customers.ToList());
}
}
public class Mixed : UpdatePipelineBase
{
[IterationSetup]
public override void InitializeContext()
{
base.InitializeContext();
var existingCustomers = _context.Customers.ToArray();
var newCustomers = _fixture.CreateCustomers(333, setPrimaryKeys: false);
_context.Customers.AddRange(newCustomers);
for (var i = 0; i < 1000; i += 3)
{
_context.Customers.Remove(existingCustomers[i]);
}
for (var i = 1; i < 1000; i += 3)
{
existingCustomers[i].FirstName += " Modified";
}
}
}
}
}