Skip to content

Commit

Permalink
Work/duge/97 (#133)
Browse files Browse the repository at this point in the history
* bug in master branch

* adding benchmark files
  • Loading branch information
lonesomegeek committed Jan 22, 2020
1 parent 23fe90d commit 8fbdc34
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 2 deletions.
22 changes: 22 additions & 0 deletions LSG.GenericCrud.Benchmark/LSG.GenericCrud.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
<PackageReference Include="Bogus" Version="28.4.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LSG.GenericCrud\LSG.GenericCrud.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions LSG.GenericCrud.Benchmark/Models/BenchmarkingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using LSG.GenericCrud.Models;
using LSG.GenericCrud.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;

namespace LSG.GenericCrud.Benchmark.Models
{
public class BenchmarkingContext :
BaseDbContext,
IDbContext
{
public BenchmarkingContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options, serviceProvider)
{
}

public DbSet<Item> Items { get; set; }
public DbSet<HistoricalEvent> Events { get; set; }
public DbSet<HistoricalChangeset> Changesets { get; set; }
}
}
14 changes: 14 additions & 0 deletions LSG.GenericCrud.Benchmark/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using LSG.GenericCrud.Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace LSG.GenericCrud.Benchmark.Models
{
public class Item :
IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
}
}
187 changes: 187 additions & 0 deletions LSG.GenericCrud.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;
using Bogus;
using LSG.GenericCrud.Benchmark.Models;
using LSG.GenericCrud.Controllers;
using LSG.GenericCrud.Repositories;
using LSG.GenericCrud.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LSG.GenericCrud.Benchmark
{
public class MyController :
ControllerBase,
ICrudController<Guid, Item>
{
private readonly ICrudController<Guid, Item> _controller;

public MyController(ICrudController<Guid, Item> controller)
{

_controller = controller;
}

public Task<ActionResult<Item>> Create([FromBody] Item entity) => _controller.Create(entity);

public Task<ActionResult<Item>> Delete(Guid id) => _controller.Delete(id);

public Task<ActionResult<IEnumerable<Item>>> GetAll() => _controller.GetAll();

public Task<ActionResult<Item>> GetById(Guid id) => _controller.GetById(id);

public Task<IActionResult> HeadById(Guid id) => _controller.HeadById(id);

public Task<IActionResult> Update(Guid id, [FromBody] Item entity) => _controller.Update(id, entity);
}
[MemoryDiagnoser]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
[SimpleJob(RunStrategy.Throughput, launchCount: 1, warmupCount: 5, targetCount: 20)]
public class CrudBenchmark
{
private readonly ICrudController<Guid, Item> _controller;
private readonly ICrudRepository _repository;
private readonly ICrudService<Guid, Item> _service;
private readonly IDbContext _context;
private readonly List<Item> _entities;
private readonly Item _entity;

public CrudBenchmark()
{
var optionsBuilder = new DbContextOptionsBuilder()
.UseInMemoryDatabase("test");
//.UseSqlServer("server=(localdb)\\mssqllocaldb;Initial Catalog=BenchmarkDb");
_context = new BenchmarkingContext(optionsBuilder.Options, null);
((DbContext)_context).Database.EnsureCreated();
_repository = new CrudRepository(_context);
_service = new CrudServiceBase<Guid, Item>(_repository);
_controller = new CrudControllerBase<Guid, Item>(_service);

var entityFaker = new Faker<Item>().
RuleFor(_ => _.Id, Guid.NewGuid()).
RuleFor(_ => _.Name, _ => _.Lorem.Word());
_entities = entityFaker.Generate(5);
_entity = entityFaker.Generate();
}

[Benchmark]
public async Task Create() => await _controller.Create(new Item());
//[Benchmark]
//public async Task Read() => await _controller.GetById(Guid.NewGuid());
//[Benchmark]
//public async Task ReadAll() => await _controller.GetAll();
//[Benchmark]
//public async Task Update() => await _controller.Update(Guid.NewGuid(), new Item());
//[Benchmark]
//public async Task Delete() => await _controller.Delete(Guid.NewGuid());
//[Benchmark]
//public async Task CreateAndRead()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// await _controller.GetById(createdEntity.Id);
//}
//[Benchmark]
//public async Task CreateAndDelete()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// await _controller.Delete(createdEntity.Id);
//}
//[Benchmark]
//public async Task CreateAndUpdate()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// createdEntity.Name = "Modified Value";
// var updatedEntityResult = await _controller.Update(createdEntity.Id, createdEntity);
//}
}

[MemoryDiagnoser]
[MinColumn, MaxColumn, MeanColumn, MedianColumn]
[SimpleJob(RunStrategy.Throughput, launchCount: 1, warmupCount: 5, targetCount: 20)]
public class HistoricalCrudBenchmark
{
private readonly ICrudController<Guid, Item> _controller;
private readonly ICrudRepository _repository;
private readonly ICrudService<Guid, Item> _service;
private readonly IDbContext _context;
private readonly List<Item> _entities;
private readonly Item _entity;

public HistoricalCrudBenchmark()
{
var optionsBuilder = new DbContextOptionsBuilder()
.UseInMemoryDatabase("test");
//.UseSqlServer("server=(localdb)\\mssqllocaldb;Initial Catalog=BenchmarkDb");
_context = new BenchmarkingContext(optionsBuilder.Options, null);
((DbContext)_context).Database.EnsureCreated();
_repository = new CrudRepository(_context);
_service = new HistoricalCrudServiceBase<Guid, Item>(
new CrudServiceBase<Guid, Item>(_repository),
_repository,
null,
null,
null);
_controller = new HistoricalCrudControllerBase<Guid, Item>(
new CrudControllerBase<Guid, Item>(new CrudServiceBase<Guid, Item>(_repository)),
(IHistoricalCrudService<Guid, Item>)_service);

var entityFaker = new Faker<Item>().
RuleFor(_ => _.Id, Guid.NewGuid()).
RuleFor(_ => _.Name, _ => _.Lorem.Word());
_entities = entityFaker.Generate(5);
_entity = entityFaker.Generate();
}

[Benchmark]
public async Task Create() => await _controller.Create(new Item());
//[Benchmark]
//public async Task Read() => await _controller.GetById(Guid.NewGuid());
//[Benchmark]
//public async Task ReadAll() => await _controller.GetAll();
//[Benchmark]
//public async Task Update() => await _controller.Update(Guid.NewGuid(), new Item());
//[Benchmark]
//public async Task Delete() => await _controller.Delete(Guid.NewGuid());
//[Benchmark]
//public async Task CreateAndRead()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// await _controller.GetById(createdEntity.Id);
//}
//[Benchmark]
//public async Task CreateAndDelete()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// await _controller.Delete(createdEntity.Id);
//}
//[Benchmark]
//public async Task CreateAndUpdate()
//{
// var createdEntityResult = await _controller.Create(new Item());
// var createdEntity = ((Item)((CreatedAtActionResult)createdEntityResult.Result).Value);
// createdEntity.Name = "Modified Value";
// var updatedEntityResult = await _controller.Update(createdEntity.Id, createdEntity);
//}
}
class Program
{
static void Main(string[] args)
{
var summary = BenchmarkRunner
.Run<HistoricalCrudBenchmark>(/*new DebugInProcessConfig()*/);
//.Run<IntroSetupCleanupIteration>(/*new DebugInProcessConfig()*/);
Console.WriteLine(summary);
}
}
}
8 changes: 7 additions & 1 deletion LSG.GenericCrud.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LSG.GenericCrud.Dto", "LSG.
EndProject
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "LSG.GenericCrud.Database", "LSG.GenericCrud.Database\LSG.GenericCrud.Database.sqlproj", "{556742EA-0D22-44E7-9FE2-759312C6492F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSG.GenericCrud.Samples", "LSG.GenericCrud.Samples\LSG.GenericCrud.Samples.csproj", "{CAEF783B-FDB7-4A7A-8053-773CE711B3E9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LSG.GenericCrud.Samples", "LSG.GenericCrud.Samples\LSG.GenericCrud.Samples.csproj", "{CAEF783B-FDB7-4A7A-8053-773CE711B3E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSG.GenericCrud.Benchmark", "LSG.GenericCrud.Benchmark\LSG.GenericCrud.Benchmark.csproj", "{BB1F34D8-04BF-4C2E-BC65-C57F963F2D61}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -44,6 +46,10 @@ Global
{CAEF783B-FDB7-4A7A-8053-773CE711B3E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAEF783B-FDB7-4A7A-8053-773CE711B3E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAEF783B-FDB7-4A7A-8053-773CE711B3E9}.Release|Any CPU.Build.0 = Release|Any CPU
{BB1F34D8-04BF-4C2E-BC65-C57F963F2D61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB1F34D8-04BF-4C2E-BC65-C57F963F2D61}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB1F34D8-04BF-4C2E-BC65-C57F963F2D61}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB1F34D8-04BF-4C2E-BC65-C57F963F2D61}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion LSG.GenericCrud/Services/HistoricalCrudServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class HistoricalCrudServiceBase<T1, T2> :
_service.AutoCommit = false;
_userInfoRepository = userInfoRepository;
_historicalCrudReadService = historicalCrudReadService;
_options = options.Value == null ? HistoricalCrudServiceOptions.DefaultValues : options.Value;
_options = options == null || options.Value == null ? HistoricalCrudServiceOptions.DefaultValues : options.Value;

AutoCommit = false;
}
Expand Down

0 comments on commit 8fbdc34

Please sign in to comment.