Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DbContextOptions composibility support #32518

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions benchmark/EFCore.Benchmarks/Initialization/InitializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,77 @@
using BenchmarkDotNet.Attributes;
using Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore.Benchmarks.Initialization;

[DisplayName("InitializationTests")]
public abstract class InitializationTests
{
private ServiceProvider _serviceProvider;
private ServiceProvider _pooledServiceProvider;

protected abstract AdventureWorksContextBase CreateContext();
protected abstract ConventionSet CreateConventionSet();
protected abstract IServiceCollection AddContext(IServiceCollection services);
protected abstract IServiceCollection AddContextPool(IServiceCollection services);

[GlobalSetup]
public virtual void Initialize()
{
_serviceProvider = AddContext(new ServiceCollection()).BuildServiceProvider();
_pooledServiceProvider = AddContextPool(new ServiceCollection()).BuildServiceProvider();
}

[Benchmark]
public virtual void CreateAndDisposeUnusedContext()
{
for (var i = 0; i < 10000; i++)
{
// ReSharper disable once UnusedVariable
using (var context = CreateContext())
{
}
using var context = CreateContext();
}
}

[Benchmark]
public virtual void CreateAndDisposeUnusedContextFromDi()
{
for (var i = 0; i < 10000; i++)
{
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetService<AdventureWorksContextBase>();
}
}

[Benchmark]
public virtual void CreateAndDisposeUnusedContextFromDiFactory()
{
var factory = _serviceProvider.GetService<IDbContextFactory<AdventureWorksContextBase>>();
for (var i = 0; i < 10000; i++)
{
using var _ = factory.CreateDbContext();
}
}

[Benchmark]
public virtual void CreateAndDisposePooledContextFromDi()
{
for (var i = 0; i < 10000; i++)
{
using var scope = _pooledServiceProvider.CreateScope();
var context = (AdventureWorksContextBase)scope.ServiceProvider.GetService(typeof(AdventureWorksContextBase));
var _ = context.Model;
}
}

[Benchmark]
public virtual void CreateAndDisposePooledContextFromDiFactory()
{
var factory = _pooledServiceProvider.GetService<IDbContextFactory<AdventureWorksContextBase>>();
for (var i = 0; i < 10000; i++)
{
using var context = factory.CreateDbContext();
var _ = context.Model;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public abstract class AdventureWorksContextBase : DbContext
{
public AdventureWorksContextBase()
{
}

public AdventureWorksContextBase(DbContextOptions options)
: base(options)
{
}

public virtual DbSet<Address> Address { get; set; }
public virtual DbSet<AddressType> AddressType { get; set; }
public virtual DbSet<BillOfMaterials> BillOfMaterials { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.EntityFrameworkCore.Benchmarks.Initialization;

Expand All @@ -13,4 +15,37 @@ protected override AdventureWorksContextBase CreateContext()

protected override ConventionSet CreateConventionSet()
=> SqlServerConventionSetBuilder.Build();

protected override IServiceCollection AddContext(IServiceCollection services)
{
services.AddDbContext<AdventureWorksContextBase, AdventureWorksSqlServerContext>()
.AddDbContextFactory<AdventureWorksSqlServerContext>()
.TryAddSingleton<IDbContextFactory<AdventureWorksContextBase>,
AdventureWorksSqlServerContextFactory<AdventureWorksSqlServerContext>>();

return services;
}

protected override IServiceCollection AddContextPool(IServiceCollection services)
{
services.AddDbContextPool<AdventureWorksContextBase, AdventureWorksPoolableSqlServerContext>(
AdventureWorksSqlServerFixture.ConfigureOptions)
.AddPooledDbContextFactory<AdventureWorksPoolableSqlServerContext>(AdventureWorksSqlServerFixture.ConfigureOptions)
.TryAddSingleton<IDbContextFactory<AdventureWorksContextBase>,
AdventureWorksSqlServerContextFactory<AdventureWorksPoolableSqlServerContext>>();

return services;
}

private class AdventureWorksSqlServerContextFactory<T> : IDbContextFactory<AdventureWorksContextBase>
where T : AdventureWorksContextBase
{
private readonly IDbContextFactory<T> _factory;

public AdventureWorksSqlServerContextFactory(IDbContextFactory<T> factory)
=> _factory = factory;

public AdventureWorksContextBase CreateDbContext()
=> _factory.CreateDbContext();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public class AdventureWorksPoolableSqlServerContext : AdventureWorksContextBase
{
public AdventureWorksPoolableSqlServerContext(DbContextOptions<AdventureWorksPoolableSqlServerContext> options)
: base(options)
{
}

protected override void ConfigureProvider(DbContextOptionsBuilder optionsBuilder)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public class AdventureWorksSqlServerContext : AdventureWorksContextBase
{
private readonly string _connectionString;

public AdventureWorksSqlServerContext(string connectionString)
{
_connectionString = connectionString;
}

protected override void ConfigureProvider(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(_connectionString);
=> optionsBuilder.UseSqlServer(AdventureWorksSqlServerFixture.ConnectionString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public static class AdventureWorksSqlServerFixture
{
private static readonly string _connectionString = SqlServerBenchmarkEnvironment.CreateConnectionString("AdventureWorks2014");
public static string ConnectionString { get; } = SqlServerBenchmarkEnvironment.CreateConnectionString("AdventureWorks2014");

// This method is called from timed code, be careful when changing it
public static AdventureWorksContextBase CreateContext()
=> new AdventureWorksSqlServerContext(_connectionString);
=> new AdventureWorksSqlServerContext();

public static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(ConnectionString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.EntityFrameworkCore.Benchmarks.Initialization;

Expand All @@ -13,4 +15,37 @@ protected override AdventureWorksContextBase CreateContext()

protected override ConventionSet CreateConventionSet()
=> SqliteConventionSetBuilder.Build();

protected override IServiceCollection AddContext(IServiceCollection services)
{
services.AddDbContext<AdventureWorksContextBase, AdventureWorksSqliteContext>()
.AddDbContextFactory<AdventureWorksSqliteContext>()
.TryAddSingleton<IDbContextFactory<AdventureWorksContextBase>,
AdventureWorksSqliteContextFactory<AdventureWorksSqliteContext>>();

return services;
}

protected override IServiceCollection AddContextPool(IServiceCollection services)
{
services.AddDbContextPool<AdventureWorksContextBase, AdventureWorksPoolableSqliteContext>(
AdventureWorksSqliteFixture.ConfigureOptions)
.AddPooledDbContextFactory<AdventureWorksPoolableSqliteContext>(AdventureWorksSqliteFixture.ConfigureOptions)
.TryAddSingleton<IDbContextFactory<AdventureWorksContextBase>,
AdventureWorksSqliteContextFactory<AdventureWorksPoolableSqliteContext>>();

return services;
}

private class AdventureWorksSqliteContextFactory<T> : IDbContextFactory<AdventureWorksContextBase>
where T : AdventureWorksContextBase
{
private readonly IDbContextFactory<T> _factory;

public AdventureWorksSqliteContextFactory(IDbContextFactory<T> factory)
=> _factory = factory;

public AdventureWorksContextBase CreateDbContext()
=> _factory.CreateDbContext();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public class AdventureWorksPoolableSqliteContext : AdventureWorksContextBase
{
public AdventureWorksPoolableSqliteContext(DbContextOptions<AdventureWorksPoolableSqliteContext> options)
: base(options)
{
}

protected override void ConfigureProvider(DbContextOptionsBuilder optionsBuilder)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ namespace Microsoft.EntityFrameworkCore.Benchmarks.Models.AdventureWorks;

public class AdventureWorksSqliteContext : AdventureWorksContextBase
{
private readonly string _connectionString;

public AdventureWorksSqliteContext(string connectionString)
{
_connectionString = connectionString;
}

protected override void ConfigureProvider(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite(_connectionString);
=> optionsBuilder.UseSqlite(AdventureWorksSqliteFixture.ConnectionString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ public static class AdventureWorksSqliteFixture
private static readonly string _baseDirectory
= Path.GetDirectoryName(typeof(AdventureWorksSqliteFixture).Assembly.Location);

private static readonly string _connectionString
= $"Data Source={Path.Combine(_baseDirectory, "AdventureWorks2014.db")}";
public static string ConnectionString { get; } = $"Data Source={Path.Combine(_baseDirectory, "AdventureWorks2014.db")}";

// This method is called from timed code, be careful when changing it
public static AdventureWorksContextBase CreateContext()
=> new AdventureWorksSqliteContext(_connectionString);
=> new AdventureWorksSqliteContext();

public static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite(ConnectionString);
}
Loading
Loading