Skip to content
Closed
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
18 changes: 18 additions & 0 deletions BenchmarkSuite1/BenchmarkSuite1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="Microsoft.VisualStudio.DiagnosticsHub.BenchmarkDotNetDiagnosers" Version="18.0.36421.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Services\Products\Distribt.Services.Products.BusinessLogic\Distribt.Services.Products.BusinessLogic.csproj" />
<ProjectReference Include="..\src\Services\Products\Distribt.Services.Products.Dtos\Distribt.Services.Products.Dtos.csproj" />
</ItemGroup>
</Project>
52 changes: 52 additions & 0 deletions BenchmarkSuite1/ProductsWriteStoreBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Distribt.Services.Products.BusinessLogic.DataAccess;
using Distribt.Services.Products.Dtos;
using Microsoft.EntityFrameworkCore;

namespace Benchmarks.Products
{
[MemoryDiagnoser]
public class ProductsWriteStoreBenchmarks
{
private static DbContextOptions<ProductsWriteStore> NewOptions()
=> new DbContextOptionsBuilder<ProductsWriteStore>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;

[Benchmark]
public async Task CreateRecord_Isolated()
{
var options = NewOptions();
await using var store = new ProductsWriteStore(options);

var details = new ProductDetails(
Name: "Product-" + Guid.NewGuid().ToString("N"),
Description: "Desc-" + Guid.NewGuid().ToString("N")
);

_ = await store.CreateRecord(details);
}

[Benchmark]
public async Task UpdateProduct_Isolated()
{
var options = NewOptions();
await using var store = new ProductsWriteStore(options);

// Seed a product to update in this isolated database
var existingId = await store.CreateRecord(new ProductDetails(
Name: "Seed-" + Guid.NewGuid().ToString("N"),
Description: "Seed-Desc"
));

var updateDetails = new ProductDetails(
Name: "Updated-" + Guid.NewGuid().ToString("N"),
Description: "Updated-Desc"
);

await store.UpdateProduct(existingId, updateDetails);
}
}
}
12 changes: 12 additions & 0 deletions BenchmarkSuite1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace BenchmarkSuite1
{
internal class Program
{
static void Main(string[] args)
{
var _ = BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
}
10 changes: 8 additions & 2 deletions Distribt.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31815.197
# Visual Studio Version 18
VisualStudioVersion = 18.0.10928.107 d18.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Api", "Api", "{25E556CF-D2DB-4BA2-9379-07CD4976BA9E}"
EndProject
Expand Down Expand Up @@ -93,6 +93,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Discovery", "Discovery", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Distribt.Test.Shared.Discovery.Tests", "src\Tests\Shared\Discovery\Distribt.Test.Shared.Discovery.Tests\Distribt.Test.Shared.Discovery.Tests.csproj", "{F9C0F635-D488-4E1F-A4AA-912EA8C518B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkSuite1", "BenchmarkSuite1\BenchmarkSuite1.csproj", "{A20861A9-411E-6150-BF5C-69E8196E5D22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -211,6 +213,10 @@ Global
{F9C0F635-D488-4E1F-A4AA-912EA8C518B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9C0F635-D488-4E1F-A4AA-912EA8C518B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9C0F635-D488-4E1F-A4AA-912EA8C518B5}.Release|Any CPU.Build.0 = Release|Any CPU
{A20861A9-411E-6150-BF5C-69E8196E5D22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A20861A9-411E-6150-BF5C-69E8196E5D22}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A20861A9-411E-6150-BF5C-69E8196E5D22}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A20861A9-411E-6150-BF5C-69E8196E5D22}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:60320;http://localhost:60310",
"applicationUrl": "https://localhost:60321;http://localhost:60311",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"VAULT-TOKEN": "vault-distribt-token"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@ public class ProductsWriteStore : DbContext, IProductsWriteStore
public ProductsWriteStore(DbContextOptions<ProductsWriteStore> options) : base(options)
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity<ProductDetailEntity>(b =>
{

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure about this? is this breaking other parts of the code?
(this comments is a test)

b.HasKey(p => p.Id);
b.Property(p => p.Id).ValueGeneratedOnAdd();
b.Property(p => p.Name).IsRequired(false);
b.Property(p => p.Description).IsRequired(false);
b.ToTable("Products");
});
}

public async Task UpdateProduct(int id, ProductDetails details)
{
var product = await Products.SingleAsync(a => a.Id == id);
product.Description = details.Description;
// Avoid extra round-trip: attach a stub entity and mark only changed properties as modified
var product = new ProductDetailEntity { Id = id };
Attach(product);

product.Name = details.Name;

product.Description = details.Description;

Entry(product).Property(p => p.Name).IsModified = true;
Entry(product).Property(p => p.Description).IsModified = true;

await SaveChangesAsync();
}

Expand All @@ -34,16 +55,16 @@ public async Task<int> CreateRecord(ProductDetails details)
Name = details.Name
};

var result = await Products.AddAsync(newProduct);
Products.Add(newProduct);
await SaveChangesAsync();

return result.Entity.Id ?? throw new ApplicationException("the record has not been inserted in the db");
return newProduct.Id;
}


private class ProductDetailEntity
{
public int? Id { get; set; }
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
}
Expand Down
Loading