Merge pull request #3 from koenbeuk/benchmark

Added a benchmark
This commit is contained in:
Koen
2021-06-04 00:40:06 +08:00
committed by GitHub
16 changed files with 466 additions and 22 deletions
+10 -1
View File
@@ -29,7 +29,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Project
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.Projectables.FunctionalTests", "tests\EntityFrameworkCore.Projectables.FunctionalTests\EntityFrameworkCore.Projectables.FunctionalTests.csproj", "{A36F5471-0C16-4453-811B-818326931313}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadmeSample", "samples\ReadmeSample\ReadmeSample.csproj", "{6F63E04C-9267-4211-8AC7-290C60331D60}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReadmeSample", "samples\ReadmeSample\ReadmeSample.csproj", "{6F63E04C-9267-4211-8AC7-290C60331D60}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{D1DB50EE-D639-46B0-8966-D0AA5F569620}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkCore.Projectables.Benchmarks", "benchmarks\EntityFrameworkCore.Projectables.Benchmarks\EntityFrameworkCore.Projectables.Benchmarks.csproj", "{F2F01B61-5FB8-4F96-A6DE-824C9756B365}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -69,6 +73,10 @@ Global
{6F63E04C-9267-4211-8AC7-290C60331D60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6F63E04C-9267-4211-8AC7-290C60331D60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6F63E04C-9267-4211-8AC7-290C60331D60}.Release|Any CPU.Build.0 = Release|Any CPU
{F2F01B61-5FB8-4F96-A6DE-824C9756B365}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2F01B61-5FB8-4F96-A6DE-824C9756B365}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2F01B61-5FB8-4F96-A6DE-824C9756B365}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2F01B61-5FB8-4F96-A6DE-824C9756B365}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -82,6 +90,7 @@ Global
{2F0DD7D7-867F-4478-9E22-45C114B61C46} = {F5E4436F-87F2-46AB-A9EB-59B4BF21BF7A}
{A36F5471-0C16-4453-811B-818326931313} = {F5E4436F-87F2-46AB-A9EB-59B4BF21BF7A}
{6F63E04C-9267-4211-8AC7-290C60331D60} = {07584D01-2D30-404B-B0D1-32080C0CC18A}
{F2F01B61-5FB8-4F96-A6DE-824C9756B365} = {D1DB50EE-D639-46B0-8966-D0AA5F569620}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D17BD356-592C-4628-9D81-A04E24FF02F3}
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>,
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables.Generator\EntityFrameworkCore.Projectables.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using EntityFrameworkCore.Projectables.Extensions;
using EntityFrameworkCore.Projectables.Infrastructure;
namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
{
class TestDbContext : DbContext
{
readonly bool _useProjectables;
readonly bool _useFullCompatibiltyMode;
public TestDbContext(bool useProjectables, bool useFullCompatibiltyMode = true)
{
_useProjectables = useProjectables;
_useFullCompatibiltyMode = useFullCompatibiltyMode;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=ReadmeSample;Trusted_Connection=True");
if (_useProjectables)
{
optionsBuilder.UseProjectables(projectableOptions => {
projectableOptions.CompatibilityMode(_useFullCompatibiltyMode ? CompatibilityMode.Full : CompatibilityMode.Limited);
});
}
}
public DbSet<TestEntity> Entities => Set<TestEntity>();
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCore.Projectables.Benchmarks.Helpers
{
public class TestEntity
{
public int Id { get; set; }
[Projectable]
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.IO.Compression;
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 PlainOverhead
{
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
using var dbContext = new TestDbContext(false);
for (int i = 0; i < 10000; i++)
{
dbContext.Entities.Select(x => x.Id).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithFullCompatibility()
{
using var dbContext = new TestDbContext(true);
for (int i = 0; i < 10000; i++)
{
dbContext.Entities.Select(x => x.Id).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < 10000; i++)
{
dbContext.Entities.Select(x => x.Id).ToQueryString();
}
}
}
}
@@ -0,0 +1,5 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;
using EntityFrameworkCore.Projectables.Benchmarks;
BenchmarkSwitcher.FromAssembly(typeof(PlainOverhead).Assembly).Run(args, DefaultConfig.Instance.WithOption(ConfigOptions.DisableOptimizationsValidator, true));
@@ -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();
}
}
}
}
@@ -0,0 +1,49 @@
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 ProjectableProperties
{
const int innerLoop = 10000;
[Benchmark(Baseline = true)]
public void WithoutProjectables()
{
using var dbContext = new TestDbContext(false);
for (int i = 0; i < innerLoop; i++)
{
if (1 == i)
throw new Exception(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.IdPlus1).ToQueryString();
}
}
[Benchmark]
public void WithProjectablesWithLimitedCompatibility()
{
using var dbContext = new TestDbContext(true, false);
for (int i = 0; i < innerLoop; i++)
{
dbContext.Entities.Select(x => x.IdPlus1).ToQueryString();
}
}
}
}
+1 -5
View File
@@ -13,11 +13,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.6" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables.Generator\EntityFrameworkCore.Projectables.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\EntityFrameworkCore.Projectables\EntityFrameworkCore.Projectables.csproj" />
<PackageReference Include="EntityFrameworkCore.Projectables" Version="0.2.0" />
</ItemGroup>
</Project>
@@ -1,4 +1,5 @@
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using EntityFrameworkCore.Projectables.Infrastructure;
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System;
@@ -12,15 +13,17 @@ namespace EntityFrameworkCore.Projectables.Extensions
{
public static class DbContextOptionsExtensions
{
public static DbContextOptionsBuilder<TContext> UseProjectables<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder)
public static DbContextOptionsBuilder<TContext> UseProjectables<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder, Action<ProjectableOptionsBuilder>? configure = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseProjectables((DbContextOptionsBuilder)optionsBuilder);
=> (DbContextOptionsBuilder<TContext>)UseProjectables((DbContextOptionsBuilder)optionsBuilder, configure);
public static DbContextOptionsBuilder UseProjectables(this DbContextOptionsBuilder optionsBuilder)
public static DbContextOptionsBuilder UseProjectables(this DbContextOptionsBuilder optionsBuilder, Action<ProjectableOptionsBuilder>? configure = null)
{
var extension = optionsBuilder.Options.FindExtension<ProjectionOptionsExtension>() ?? new ProjectionOptionsExtension();
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
configure?.Invoke(new ProjectableOptionsBuilder(optionsBuilder));
return optionsBuilder;
}
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFrameworkCore.Projectables.Infrastructure
{
public enum CompatibilityMode
{
Full,
Limited
}
}
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Services;
using Microsoft.EntityFrameworkCore.Query;
namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
public class CustomQueryTranslationPreprocessorFactory : IQueryTranslationPreprocessorFactory
{
readonly IQueryTranslationPreprocessorFactory _decoratedFactory;
readonly QueryTranslationPreprocessorDependencies _queryTranslationPreprocessorDependencies;
readonly ProjectableExpressionReplacer _projectableExpressionReplacer = new(new ProjectionExpressionResolver());
public CustomQueryTranslationPreprocessorFactory(IQueryTranslationPreprocessorFactory decoratedFactory, QueryTranslationPreprocessorDependencies queryTranslationPreprocessorDependencies)
{
_decoratedFactory = decoratedFactory;
_queryTranslationPreprocessorDependencies = queryTranslationPreprocessorDependencies;
}
public QueryTranslationPreprocessor Create(QueryCompilationContext queryCompilationContext)
=> new CustomQueryTranslationPreprocessor(_queryTranslationPreprocessorDependencies, queryCompilationContext, _projectableExpressionReplacer);
}
public class CustomQueryTranslationPreprocessor : QueryTranslationPreprocessor
{
readonly ProjectableExpressionReplacer _projectableExpressionReplacer;
public CustomQueryTranslationPreprocessor(QueryTranslationPreprocessorDependencies dependencies, QueryCompilationContext queryCompilationContext, ProjectableExpressionReplacer projectableExpressionReplacer) : base(dependencies, queryCompilationContext)
{
_projectableExpressionReplacer = projectableExpressionReplacer;
}
public override Expression Process(Expression query)
=> base.Process(_projectableExpressionReplacer.Visit(query));
}
}
@@ -14,11 +14,21 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
public class ProjectionOptionsExtension : IDbContextOptionsExtension
{
CompatibilityMode _compatibilityMode = CompatibilityMode.Full;
public ProjectionOptionsExtension()
{
Info = new ExtensionInfo(this);
}
public ProjectionOptionsExtension(ProjectionOptionsExtension copyFrom)
: this()
{
_compatibilityMode = copyFrom._compatibilityMode;
}
protected ProjectionOptionsExtension Clone() => new(this);
public DbContextOptionsExtensionInfo Info { get; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "EF1001:Internal EF Core API usage.", Justification = "Needed")]
@@ -37,19 +47,47 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
return ActivatorUtilities.GetServiceOrCreateInstance(services, descriptor.ImplementationType!);
}
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryCompiler));
if (targetDescriptor is null)
if (_compatibilityMode is CompatibilityMode.Full)
{
throw new InvalidOperationException("No QueryProvider is configured yet. Please make sure to configure a database provider first"); ;
}
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryProvider), new [] { targetDescriptor.ServiceType });
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryCompiler));
if (targetDescriptor is null)
{
throw new InvalidOperationException("No QueryProvider is configured yet. Please make sure to configure a database provider first"); ;
}
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryProvider), new[] { targetDescriptor.ServiceType });
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
}
else
{
var targetDescriptor = services.FirstOrDefault(x => x.ServiceType == typeof(IQueryTranslationPreprocessorFactory));
if (targetDescriptor is null)
{
throw new InvalidOperationException("No QueryTranslationPreprocessorFactory is configured yet. Please make sure to configure a database provider first"); ;
}
var decoratorObjectFactory = ActivatorUtilities.CreateFactory(typeof(CustomQueryTranslationPreprocessorFactory), new[] { targetDescriptor.ServiceType });
services.Replace(ServiceDescriptor.Describe(
targetDescriptor.ServiceType,
serviceProvider => decoratorObjectFactory(serviceProvider, new[] { CreateTargetInstance(serviceProvider, targetDescriptor) }),
targetDescriptor.Lifetime
));
}
}
public ProjectionOptionsExtension WithCompatibilityMode(CompatibilityMode compatibilityMode)
{
var clone = Clone();
clone._compatibilityMode = compatibilityMode;
return clone;
}
public void Validate(IDbContextOptions options)
@@ -58,13 +96,29 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
private int? _serviceProviderHash;
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension)
{
}
public override bool IsDatabaseProvider => false;
public override string LogFragment => string.Empty;
public override long GetServiceProviderHashCode() => 0;
public override long GetServiceProviderHashCode()
{
if (_serviceProviderHash == null)
{
var hashCode = nameof(ProjectionOptionsExtension).GetHashCode();
var extension = (ProjectionOptionsExtension)Extension;
hashCode ^= extension._compatibilityMode.GetHashCode();
_serviceProviderHash = hashCode;
}
return _serviceProviderHash.Value;
}
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
@@ -72,6 +126,8 @@ namespace EntityFrameworkCore.Projectables.Infrastructure.Internal
{
throw new ArgumentNullException(nameof(debugInfo));
}
debugInfo["Projectables:CompatibilityMode"] = ((ProjectionOptionsExtension)Extension)._compatibilityMode.ToString();
}
}
}
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFrameworkCore.Projectables.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace EntityFrameworkCore.Projectables.Infrastructure
{
public class ProjectableOptionsBuilder
{
readonly DbContextOptionsBuilder _optionsBuilder;
public ProjectableOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
{
_optionsBuilder = optionsBuilder ?? throw new ArgumentNullException(nameof(optionsBuilder));
}
public ProjectableOptionsBuilder CompatibilityMode(CompatibilityMode mode)
=> WithOption(x => x.WithCompatibilityMode(mode));
/// <summary>
/// Sets an option by cloning the extension used to store the settings. This ensures the builder
/// does not modify options that are already in use elsewhere.
/// </summary>
/// <param name="setAction"> An action to set the option. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
protected virtual ProjectableOptionsBuilder WithOption(Func<ProjectionOptionsExtension, ProjectionOptionsExtension> setAction)
{
((IDbContextOptionsBuilderInfrastructure)_optionsBuilder).AddOrUpdateExtension(
setAction(_optionsBuilder.Options.FindExtension<ProjectionOptionsExtension>() ?? new ProjectionOptionsExtension()));
return this;
}
}
}