-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to context pooling docs (#3460)
- Loading branch information
Showing
8 changed files
with
134 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Optimize>true</Optimize> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="5.0.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0-rc.1.21452.10" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0-rc.1.21452.10" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
|
||
namespace Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
public class ContextPooling | ||
{ | ||
private DbContextOptions<BloggingContext> _options; | ||
private PooledDbContextFactory<BloggingContext> _poolingFactory; | ||
|
||
[Params(1)] | ||
public int NumBlogs { get; set; } | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_options = new DbContextOptionsBuilder<BloggingContext>() | ||
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True") | ||
.Options; | ||
|
||
using var context = new BloggingContext(_options); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
context.SeedData(NumBlogs); | ||
|
||
_poolingFactory = new PooledDbContextFactory<BloggingContext>(_options); | ||
} | ||
|
||
[Benchmark] | ||
public List<Blog> WithoutContextPooling() | ||
{ | ||
using var context = new BloggingContext(_options); | ||
|
||
return context.Blogs.ToList(); | ||
} | ||
|
||
[Benchmark] | ||
public List<Blog> WithContextPooling() | ||
{ | ||
using var context = _poolingFactory.CreateDbContext(); | ||
|
||
return context.Blogs.ToList(); | ||
} | ||
|
||
public class BloggingContext : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
|
||
public BloggingContext(DbContextOptions options) : base(options) {} | ||
|
||
public void SeedData(int numBlogs) | ||
{ | ||
Blogs.AddRange(Enumerable.Range(0, numBlogs).Select(i => new Blog { Url = $"http://www.someblog{i}.com"})); | ||
SaveChanges(); | ||
} | ||
} | ||
|
||
public class Blog | ||
{ | ||
public int BlogId { get; set; } | ||
public string Url { get; set; } | ||
public int Rating { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Performance | ||
{ | ||
public class PooledBloggingContext : DbContext | ||
{ | ||
public PooledBloggingContext(DbContextOptions options) : base(options) {} | ||
|
||
public DbSet<Blog> Blogs { get; set; } | ||
public DbSet<Post> Posts { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters