Refactored benchmark a bit

This commit is contained in:
Koen Bekkenutte
2021-06-03 19:45:02 +08:00
parent 11582d467c
commit 45618966f8
5 changed files with 120 additions and 4 deletions
@@ -12,5 +12,8 @@ namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
[Projectable] [Projectable]
public int IdPlus1 => Id + 1; public int IdPlus1 => Id + 1;
[Projectable]
public int IdPlus1Method() => Id + 1;
} }
} }
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
{
public static class TestEntityExtensions
{
public static int IdPlus1ExtensionMethod(this TestEntity entity) => entity.Id + 1;
}
}
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.Projectables.Benchmarks.Helpers;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.Benchmarks
{
public class ProjectableExtensionMethods
{
const int innerLoop = 10000;
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
using var dbContext = new TestDbContext(false);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.Id + 1).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithFullCompatibility()
{
using var dbContext = new TestDbContext(true);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.IdPlus1Method()).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.IdPlus1Method()).ToQueryString();
}
}
}
}
@@ -0,0 +1,48 @@
using System;
using System.Linq;
using BenchmarkDotNet.Attributes;
using EntityFrameworkCore.Projectables.Benchmarks.Helpers;
using EntityFrameworkCore.Projectables.Extensions;
using EntityFrameworkCore.Projectables.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore.Projectables.Benchmarks
{
public class ProjectableMethods
{
const int innerLoop = 10000;
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
using var dbContext = new TestDbContext(false);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.Id + 1).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithFullCompatibility()
{
using var dbContext = new TestDbContext(true);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.IdPlus1Method()).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.IdPlus1Method()).ToQueryString();
}
}
}
}
@@ -10,14 +10,17 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
{ {
public class ProjectableProperties public class ProjectableProperties
{ {
const int innerLoop = 10000;
[Benchmark(Baseline = true)] [Benchmark(Baseline = true)]
public void WithoutProjectables() public void WithoutProjectables()
{ {
using var dbContext = new TestDbContext(false); using var dbContext = new TestDbContext(false);
for (int i = 0; i < 10000; i++) for (int i = 0; i < innerLoop; i++)
{ {
dbContext.Entities.Select(x => x.Id + 1).ToQueryString(); if (1 == i)
throw new Exception(dbContext.Entities.Select(x => x.Id + 1).ToQueryString());
} }
} }
@@ -26,7 +29,7 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
{ {
using var dbContext = new TestDbContext(true); using var dbContext = new TestDbContext(true);
for (int i = 0; i < 10000; i++) for (int i = 0; i < innerLoop; i++)
{ {
dbContext.Entities.Select(x => x.IdPlus1).ToQueryString(); dbContext.Entities.Select(x => x.IdPlus1).ToQueryString();
} }
@@ -37,7 +40,7 @@ namespace EntityFrameworkCore.Projectables.Benchmarks
{ {
using var dbContext = new TestDbContext(true, false); using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < 10000; i++) for (int i = 0; i < innerLoop; i++)
{ {
dbContext.Entities.Select(x => x.IdPlus1).ToQueryString(); dbContext.Entities.Select(x => x.IdPlus1).ToQueryString();
} }