-
Notifications
You must be signed in to change notification settings - Fork 4
Added benchmarks Added a feature flag to enable/disable PerformanceBehaviour #608
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
Draft
FrostyApeOne
wants to merge
1
commit into
main
Choose a base branch
from
feature/added-benchmarks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
Benchmarks/Dfe.PersonsApi.Benchmarks/ConstituencyQueryHandlerBenchmark.cs
This file contains hidden or 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,119 @@ | ||
| using AutoMapper; | ||
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Running; | ||
| using Dfe.Academies.Application.Common.Behaviours; | ||
| using Dfe.Academies.Application.Constituencies.Queries.GetMemberOfParliamentByConstituencies; | ||
| using Dfe.Academies.Application.Constituencies.Queries.GetMemberOfParliamentByConstituency; | ||
| using Dfe.Academies.Application.MappingProfiles; | ||
| using Dfe.Academies.Domain.Interfaces.Caching; | ||
| using Dfe.Academies.Domain.Interfaces.Repositories; | ||
| using Dfe.Academies.Infrastructure; | ||
| using Dfe.Academies.Infrastructure.Caching; | ||
| using Dfe.Academies.Infrastructure.Repositories; | ||
| using Dfe.Academies.Testing.Common.Helpers; | ||
| using Dfe.Academies.Utils.Caching; | ||
| using MediatR; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Caching.Memory; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Dfe.PersonsApi.Benchmarks | ||
| { | ||
| [MemoryDiagnoser] | ||
| [SimpleJob(launchCount: 1, warmupCount: 2, iterationCount: 10, invocationCount: 50)] | ||
| public class ConstituencyQueryHandlerBenchmark | ||
| { | ||
| [Params("Test Constituency 1")] | ||
| public string? ConstituencyName; | ||
|
|
||
| [Params(true, false)] | ||
| public bool IncludePerformanceBehaviour; | ||
|
|
||
| private IMediator? _mediator; | ||
| private GetMembersOfParliamentByConstituenciesQuery? _query; | ||
| private IConstituencyRepository? _realRepository; | ||
| private ICacheService? _cacheService; | ||
| private IMapper? _mapper; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| var services = new ServiceCollection(); | ||
|
|
||
| var dbContext = DbContextHelper<MopContext>.CreateDbContext(services); | ||
| _realRepository = new ConstituencyRepository(dbContext); | ||
|
|
||
| var config = new MapperConfiguration(cfg => | ||
| { | ||
| cfg.AddProfile<ConstituencyProfile>(); | ||
| }); | ||
| _mapper = config.CreateMapper(); | ||
|
|
||
| var httpContextAccessor = new HttpContextAccessor | ||
| { | ||
| HttpContext = new DefaultHttpContext() | ||
| }; | ||
|
|
||
| var memoryCache = new MemoryCache(new MemoryCacheOptions()); | ||
| var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); | ||
| var logger = loggerFactory.CreateLogger<MemoryCacheService>(); | ||
|
|
||
| var cacheSettings = Options.Create(new CacheSettings | ||
| { | ||
| DefaultDurationInSeconds = 600, // 10 minutes | ||
| Durations = new Dictionary<string, int> | ||
| { | ||
| { nameof(GetMemberOfParliamentByConstituencyQueryHandler), 300 } // 5 minutes | ||
| } | ||
| }); | ||
|
|
||
| _cacheService = new MemoryCacheService(memoryCache, logger, cacheSettings); | ||
|
|
||
| services.AddMediatR(cfg => | ||
| { | ||
| cfg.RegisterServicesFromAssembly(typeof(GetMemberOfParliamentByConstituencyQueryHandler).Assembly); | ||
|
|
||
| if (IncludePerformanceBehaviour) | ||
| { | ||
| cfg.AddBehavior(typeof(IPipelineBehavior<,>), typeof(PerformanceBehaviour<,>)); | ||
| } | ||
| }); | ||
|
|
||
| services.AddSingleton(loggerFactory); | ||
| services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); | ||
| services.AddSingleton(_realRepository); | ||
| services.AddSingleton(_cacheService); | ||
| services.AddSingleton(_mapper); | ||
| services.AddSingleton<IHttpContextAccessor>(httpContextAccessor); | ||
|
|
||
| var provider = services.BuildServiceProvider(); | ||
| _mediator = provider.GetRequiredService<IMediator>(); | ||
|
|
||
| _query = new GetMembersOfParliamentByConstituenciesQuery(dbContext.Constituencies.Select(x => x.ConstituencyName).Take(100).ToList()); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task RunHandlerWithCacheAsync() | ||
| { | ||
| await _mediator?.Send(_query!)!; | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public async Task RunHandlerWithoutCacheAsync() | ||
| { | ||
| var cacheKey = $"MemberOfParliament_{CacheKeyHelper.GenerateHashedCacheKey(_query?.ConstituencyNames!)}"; | ||
| _cacheService?.Remove(cacheKey); | ||
| await _mediator?.Send(_query!)!; | ||
| } | ||
|
|
||
| public static class Program | ||
| { | ||
| public static void Main(string[] args) | ||
| { | ||
| BenchmarkRunner.Run<ConstituencyQueryHandlerBenchmark>(); | ||
| } | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
Benchmarks/Dfe.PersonsApi.Benchmarks/Dfe.PersonsApi.Benchmarks.csproj
This file contains hidden or 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,30 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <OutputType>Exe</OutputType> | ||
| <SkipBuild>true</SkipBuild> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Remove="appsettings.json" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\Dfe.Academies.Domain\Dfe.Academies.Domain.csproj" /> | ||
| <ProjectReference Include="..\..\Tests\Dfe.Academies.Testing.Common\Dfe.Academies.Testing.Common.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="appsettings.json"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or 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,5 @@ | ||
| { | ||
| "ConnectionStrings": { | ||
| "DefaultConnection": "Server=localhost;Database=sip;User ID=sa;Password=StrongPassword905;TrustServerCertificate=True" | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / SonarCloud
Database passwords should not be disclosed