Skip to content

Commit aeee2bc

Browse files
committed
Benchmarks: show results per single operation
Fixes dotnet#19875
1 parent d00c9c5 commit aeee2bc

File tree

7 files changed

+53
-47
lines changed

7 files changed

+53
-47
lines changed

benchmark/EFCore.Benchmarks/ChangeTracker/DbSetOperationTests.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class DbSetOperationTests
1515
{
1616
public abstract class DbSetOperationBase
1717
{
18+
public const int OperationsPerInvoke = 20000;
19+
1820
private OrdersFixtureBase _fixture;
1921

2022
protected List<Customer> _customersWithoutPk;
@@ -31,8 +33,8 @@ public virtual void CreateCustomers()
3133
{
3234
_fixture = CreateFixture();
3335
_fixture.Initialize(0, 0, 0, 0);
34-
_customersWithoutPk = _fixture.CreateCustomers(20000, setPrimaryKeys: false);
35-
_customersWithPk = _fixture.CreateCustomers(20000, setPrimaryKeys: true);
36+
_customersWithoutPk = _fixture.CreateCustomers(OperationsPerInvoke, setPrimaryKeys: false);
37+
_customersWithPk = _fixture.CreateCustomers(OperationsPerInvoke, setPrimaryKeys: true);
3638
}
3739

3840
public virtual void InitializeContext()
@@ -56,7 +58,7 @@ public override void InitializeContext()
5658
base.InitializeContext();
5759
}
5860

59-
[Benchmark]
61+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
6062
public virtual void Add()
6163
{
6264
foreach (var customer in _customersWithoutPk)
@@ -65,13 +67,13 @@ public virtual void Add()
6567
}
6668
}
6769

68-
[Benchmark]
70+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
6971
public virtual void AddRange()
7072
{
7173
_context.Customers.AddRange(_customersWithoutPk);
7274
}
7375

74-
[Benchmark]
76+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
7577
public virtual void Attach()
7678
{
7779
foreach (var customer in _customersWithPk)
@@ -80,7 +82,7 @@ public virtual void Attach()
8082
}
8183
}
8284

83-
[Benchmark]
85+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
8486
public virtual void AttachRange()
8587
{
8688
_context.Customers.AttachRange(_customersWithPk);
@@ -96,7 +98,7 @@ public override void InitializeContext()
9698
_context.Customers.AttachRange(_customersWithPk);
9799
}
98100

99-
[Benchmark]
101+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
100102
public virtual void Remove()
101103
{
102104
foreach (var customer in _customersWithPk)
@@ -105,13 +107,13 @@ public virtual void Remove()
105107
}
106108
}
107109

108-
[Benchmark]
110+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
109111
public virtual void RemoveRange()
110112
{
111113
_context.Customers.RemoveRange(_customersWithPk);
112114
}
113115

114-
[Benchmark]
116+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
115117
public virtual void Update()
116118
{
117119
foreach (var customer in _customersWithPk)
@@ -120,7 +122,7 @@ public virtual void Update()
120122
}
121123
}
122124

123-
[Benchmark]
125+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
124126
public virtual void UpdateRange()
125127
{
126128
_context.Customers.UpdateRange(_customersWithPk);

benchmark/EFCore.Benchmarks/ChangeTracker/FixupTests.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class FixupTests
1717
{
1818
public abstract class FixupBase
1919
{
20+
protected const int Customers = 5000;
21+
protected const int OrdersPerCustomer = 2;
22+
protected const int TotalOrders = Customers * OrdersPerCustomer;
23+
2024
private OrdersFixtureBase _fixture;
2125

2226
protected List<Customer> _customers;
@@ -33,12 +37,12 @@ public abstract class FixupBase
3337
public virtual void CheckData()
3438
{
3539
_fixture = CreateFixture();
36-
_fixture.Initialize(0, 5000, 2, 0);
40+
_fixture.Initialize(0, Customers, OrdersPerCustomer, 0);
3741

3842
using (var context = _fixture.CreateContext())
3943
{
40-
Assert.Equal(5000, context.Customers.Count());
41-
Assert.Equal(10000, context.Orders.Count());
44+
Assert.Equal(Customers, context.Customers.Count());
45+
Assert.Equal(TotalOrders, context.Orders.Count());
4246
}
4347
}
4448

@@ -47,9 +51,9 @@ public virtual void InitializeContext()
4751
_context = _fixture.CreateContext();
4852
_context.ChangeTracker.AutoDetectChangesEnabled = AutoDetectChanges;
4953

50-
_customers = _fixture.CreateCustomers(5000, setPrimaryKeys: true);
51-
_ordersWithoutPk = _fixture.CreateOrders(_customers, ordersPerCustomer: 2, setPrimaryKeys: false);
52-
_ordersWithPk = _fixture.CreateOrders(_customers, ordersPerCustomer: 2, setPrimaryKeys: true);
54+
_customers = _fixture.CreateCustomers(Customers, setPrimaryKeys: true);
55+
_ordersWithoutPk = _fixture.CreateOrders(_customers, ordersPerCustomer: OrdersPerCustomer, setPrimaryKeys: false);
56+
_ordersWithPk = _fixture.CreateOrders(_customers, ordersPerCustomer: OrdersPerCustomer, setPrimaryKeys: true);
5357
}
5458

5559
[IterationCleanup]
@@ -68,7 +72,7 @@ public override void InitializeContext()
6872
_context.Customers.AttachRange(_customers);
6973
}
7074

71-
[Benchmark]
75+
[Benchmark(OperationsPerInvoke = TotalOrders)]
7276
public virtual void AddChildren()
7377
{
7478
foreach (var order in _ordersWithoutPk)
@@ -77,7 +81,7 @@ public virtual void AddChildren()
7781
}
7882
}
7983

80-
[Benchmark]
84+
[Benchmark(OperationsPerInvoke = TotalOrders)]
8185
public virtual void AttachChildren()
8286
{
8387
foreach (var order in _ordersWithPk)
@@ -86,7 +90,7 @@ public virtual void AttachChildren()
8690
}
8791
}
8892

89-
[Benchmark]
93+
[Benchmark(OperationsPerInvoke = TotalOrders)]
9094
public virtual void QueryChildren()
9195
{
9296
_context.Orders.ToList();
@@ -102,7 +106,7 @@ public override void InitializeContext()
102106
_context.Orders.AttachRange(_ordersWithPk);
103107
}
104108

105-
[Benchmark]
109+
[Benchmark(OperationsPerInvoke = Customers)]
106110
public virtual void AddParents()
107111
{
108112
foreach (var customer in _customers)
@@ -111,7 +115,7 @@ public virtual void AddParents()
111115
}
112116
}
113117

114-
[Benchmark]
118+
[Benchmark(OperationsPerInvoke = Customers)]
115119
public virtual void AttachParents()
116120
{
117121
foreach (var customer in _customers)
@@ -120,7 +124,7 @@ public virtual void AttachParents()
120124
}
121125
}
122126

123-
[Benchmark]
127+
[Benchmark(OperationsPerInvoke = Customers)]
124128
public virtual void QueryParents()
125129
{
126130
_context.Customers.ToList();

benchmark/EFCore.Benchmarks/Extensions/Extensions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
using System.Linq;
55

6+
// ReSharper disable once CheckNamespace
67
namespace Microsoft.EntityFrameworkCore.Benchmarks
78
{
89
public static class Extensions
910
{
1011
public static IQueryable<TEntity> ApplyTracking<TEntity>(this IQueryable<TEntity> query, bool tracking)
1112
where TEntity : class
12-
{
13-
return tracking
14-
? query
15-
: query.AsNoTracking();
16-
}
13+
=> tracking ? query : query.AsNoTracking();
1714
}
1815
}

benchmark/EFCore.Benchmarks/Initialization/InitializationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class InitializationTests
1515
protected abstract AdventureWorksContextBase CreateContext();
1616
protected abstract ConventionSet CreateConventionSet();
1717

18-
[Benchmark]
18+
[Benchmark(OperationsPerInvoke = 10000)]
1919
public virtual void CreateAndDisposeUnusedContext()
2020
{
2121
for (var i = 0; i < 10000; i++)
@@ -27,7 +27,7 @@ public virtual void CreateAndDisposeUnusedContext()
2727
}
2828
}
2929

30-
[Benchmark]
30+
[Benchmark(OperationsPerInvoke = 1000)]
3131
public virtual void InitializeAndQuery_AdventureWorks()
3232
{
3333
for (var i = 0; i < 1000; i++)
@@ -39,7 +39,7 @@ public virtual void InitializeAndQuery_AdventureWorks()
3939
}
4040
}
4141

42-
[Benchmark]
42+
[Benchmark(OperationsPerInvoke = 100)]
4343
public virtual void InitializeAndSaveChanges_AdventureWorks()
4444
{
4545
for (var i = 0; i < 100; i++)

benchmark/EFCore.Benchmarks/Query/FuncletizationTests.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public abstract class FuncletizationTests
1616
{
1717
private OrdersContextBase _context;
1818

19-
protected virtual int FuncletizationIterationCount => 100;
19+
public const int OperationsPerInvoke = 100;
20+
2021
protected abstract OrdersFixtureBase CreateFixture();
2122

2223
[GlobalSetup]
@@ -36,34 +37,34 @@ public virtual void CleanupContext()
3637
_context.Dispose();
3738
}
3839

39-
[Benchmark]
40+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
4041
public virtual void NewQueryInstance()
4142
{
4243
var val = 11;
43-
for (var i = 0; i < FuncletizationIterationCount; i++)
44+
for (var i = 0; i < OperationsPerInvoke; i++)
4445
{
4546
_context.Products.Where(p => p.ProductId < val).ToList();
4647
}
4748
}
4849

49-
[Benchmark]
50+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
5051
public virtual void SameQueryInstance()
5152
{
5253
var val = 11;
5354
var query = _context.Products.Where(p => p.ProductId < val);
5455

55-
for (var i = 0; i < FuncletizationIterationCount; i++)
56+
for (var i = 0; i < OperationsPerInvoke; i++)
5657
{
5758
// ReSharper disable once PossibleMultipleEnumeration
5859
query.ToList();
5960
}
6061
}
6162

62-
[Benchmark]
63+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
6364
public virtual void ValueFromObject()
6465
{
6566
var valueHolder = new ValueHolder();
66-
for (var i = 0; i < FuncletizationIterationCount; i++)
67+
for (var i = 0; i < OperationsPerInvoke; i++)
6768
{
6869
_context.Products.Where(p => p.ProductId < valueHolder.SecondLevelProperty).ToList();
6970
}

benchmark/EFCore.Benchmarks/Query/NavigationsQueryTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public abstract class NavigationsQueryTests
1616
private AdventureWorksContextBase _context;
1717
private IQueryable<Store> _query;
1818

19-
protected virtual int QueriesPerIteration => 10;
20-
protected virtual int UnfilteredCount => 466;
19+
public const int OperationsPerInvoke = 10;
20+
public const int UnfilteredCount = 466;
2121

2222
[Params(true, false)]
2323
public bool Async { get; set; }
@@ -44,10 +44,10 @@ public virtual void CleanupContext()
4444
_context.Dispose();
4545
}
4646

47-
[Benchmark]
47+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
4848
public virtual async Task PredicateAcrossOptionalNavigation()
4949
{
50-
for (var i = 0; i < QueriesPerIteration; i++)
50+
for (var i = 0; i < OperationsPerInvoke; i++)
5151
{
5252
if (Async)
5353
{

benchmark/EFCore.Benchmarks/Query/QueryCompilationTests.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Microsoft.EntityFrameworkCore.Benchmarks.Query
1919
[DisplayName(nameof(QueryCompilationTests))]
2020
public abstract class QueryCompilationTests
2121
{
22+
public const int OperationsPerInvoke = 10;
23+
2224
private OrdersContextBase _context;
2325
private IQueryable<Product> _simpleQuery;
2426
private IQueryable<DTO> _complexQuery;
@@ -70,28 +72,28 @@ public virtual void CleanupContext()
7072
_context.Dispose();
7173
}
7274

73-
[Benchmark]
75+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
7476
public virtual void ToList()
7577
{
76-
for (var i = 0; i < 10; i++)
78+
for (var i = 0; i < OperationsPerInvoke; i++)
7779
{
7880
_simpleQuery.ToList();
7981
}
8082
}
8183

82-
[Benchmark]
84+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
8385
public virtual void FilterOrderProject()
8486
{
85-
for (var i = 0; i < 10; i++)
87+
for (var i = 0; i < OperationsPerInvoke; i++)
8688
{
8789
_complexQuery.ToList();
8890
}
8991
}
9092

91-
[Benchmark]
93+
[Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
9294
public virtual void MultipleJoins()
9395
{
94-
for (var i = 0; i < 10; i++)
96+
for (var i = 0; i < OperationsPerInvoke; i++)
9597
{
9698
_multipleJoinQuery.ToList();
9799
}

0 commit comments

Comments
 (0)