Skip to content

Commit

Permalink
Added benchamark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adimiko committed Feb 7, 2024
1 parent ae173a7 commit fa58b5a
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<CentralPackageVersionOverrideEnabled>false</CentralPackageVersionOverrideEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageVersion Include="Dapper" Version="2.1.28" />
Expand Down
7 changes: 7 additions & 0 deletions TransactionContext.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext.Postgres
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext.Tests.SeedWork", "tests\TransactionContext.Tests.SeedWork\TransactionContext.Tests.SeedWork.csproj", "{6459CE92-7FD3-4FBE-86E8-4C0321B4E694}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TransactionContext.Postgres.Benchmark", "tests\TransactionContext.Postgres.Benchmark\TransactionContext.Postgres.Benchmark.csproj", "{8239CE17-8D34-4344-8F67-A071C393F8AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -37,6 +39,10 @@ Global
{6459CE92-7FD3-4FBE-86E8-4C0321B4E694}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6459CE92-7FD3-4FBE-86E8-4C0321B4E694}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6459CE92-7FD3-4FBE-86E8-4C0321B4E694}.Release|Any CPU.Build.0 = Release|Any CPU
{8239CE17-8D34-4344-8F67-A071C393F8AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8239CE17-8D34-4344-8F67-A071C393F8AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8239CE17-8D34-4344-8F67-A071C393F8AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8239CE17-8D34-4344-8F67-A071C393F8AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -46,6 +52,7 @@ Global
{DD81C9E5-5DB6-4032-BD2C-F039300A63DD} = {6004A5FC-0B64-464C-977A-5E721E8567B7}
{A7CC7D41-46AF-45D7-95C9-681253CCB885} = {5A6575AC-81E5-4E97-909F-BBE634A2BD14}
{6459CE92-7FD3-4FBE-86E8-4C0321B4E694} = {5A6575AC-81E5-4E97-909F-BBE634A2BD14}
{8239CE17-8D34-4344-8F67-A071C393F8AD} = {5A6575AC-81E5-4E97-909F-BBE634A2BD14}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {48C38B93-C350-4CC9-BCDD-CE256D01DD7E}
Expand Down
106 changes: 106 additions & 0 deletions tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using Dapper;
using Microsoft.Extensions.DependencyInjection;
using System.Transactions;
using Testcontainers.PostgreSql;
using TransactionContext.Tests.SeedWork;

namespace TransactionContext.Postgres.Benchmark
{
[SimpleJob(RunStrategy.ColdStart, iterationCount: 1000)]
public class BenchmarkTest
{
private readonly PostgreSqlContainer _postgreSqlContainer = new PostgreSqlBuilder().Build();

protected IDbConnectionFactory DbConnectionFactory { get; private set; }

Check warning on line 16 in tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DbConnectionFactory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 16 in tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'DbConnectionFactory' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

protected IServiceProvider ServiceProvider { get; private set; }

Check warning on line 18 in tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ServiceProvider' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 18 in tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ServiceProvider' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[GlobalSetup]
public async Task GlobalSetup()
{
await _postgreSqlContainer.StartAsync();

var connectionString = _postgreSqlContainer.GetConnectionString();

IServiceCollection services = new ServiceCollection();

services.AddTransactionContext(x => x.UsePostgres(connectionString));

ServiceProvider = services.BuildServiceProvider();
DbConnectionFactory = ServiceProvider.GetRequiredService<IDbConnectionFactory>();

await CreateDb();
}

const int NumberOfCustomers = 10;

[Benchmark]
public async Task DbContextTest()
{
using (var scope = ServiceProvider.CreateScope())
{
var transactionContext = scope.ServiceProvider.GetRequiredService<ITransactionContext>();

for (var i = 0; i < NumberOfCustomers; i++)
{
var customerId = Guid.NewGuid();
var orderId = Guid.NewGuid();

transactionContext.Add(CustomerSQL.Insert, new { CustomerId = customerId, Name = $"name{i}" });
transactionContext.Add(OrderSQL.Insert, new { OrderId = orderId, CustomerId = customerId });

transactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
transactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
transactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
transactionContext.Add(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
}

await transactionContext.Commit();
}
}

[Benchmark]
public async Task TransactionScopeTest()
{
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
using (var connection = DbConnectionFactory.Create())
{
for (var i = 0; i < NumberOfCustomers; i++)
{
var customerId = Guid.NewGuid();
var orderId = Guid.NewGuid();

await connection.ExecuteAsync(CustomerSQL.Insert, new { CustomerId = customerId, Name = $"name{i}" });
await connection.ExecuteAsync(OrderSQL.Insert, new { OrderId = orderId, CustomerId = customerId });

await connection.ExecuteAsync(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
await connection.ExecuteAsync(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
await connection.ExecuteAsync(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
await connection.ExecuteAsync(OrderItemSQL.Insert, new { OrderItemId = Guid.NewGuid(), OrderId = orderId, Name = $"Name{i}", Amount = i + 1 });
}

scope.Complete();
}
}

[GlobalCleanup]
public Task GlobalCleanup()
{
return _postgreSqlContainer.DisposeAsync().AsTask();
}

private async Task CreateDb()
{
const string customerSql = "CREATE TABLE IF NOT EXISTS Customers (CustomerId UUID NOT NULL, Name VARCHAR NOT NULL, PRIMARY KEY (CustomerId))";
const string orderSql = "CREATE TABLE IF NOT EXISTS Orders (OrderId UUID NOT NULL, CustomerId UUID NOT NULL, PRIMARY KEY (OrderId))";
const string orderItemSql = "CREATE TABLE IF NOT EXISTS OrderItems (OrderItemId UUID NOT NULL, OrderId UUID NOT NULL, Name VARCHAR NOT NULL, Amount INT NOT NULL, PRIMARY KEY (OrderItemId))";
using var connection = DbConnectionFactory.Create();

await connection.ExecuteAsync(customerSql);
await connection.ExecuteAsync(orderSql);
await connection.ExecuteAsync(orderItemSql);
}
}
}
3 changes: 3 additions & 0 deletions tests/TransactionContext.Postgres.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="Dapper" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Testcontainers.PostgreSql" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\source\TransactionContext.Postgres\TransactionContext.Postgres.csproj" />
<ProjectReference Include="..\TransactionContext.Tests.SeedWork\TransactionContext.Tests.SeedWork.csproj" />
</ItemGroup>

</Project>

0 comments on commit fa58b5a

Please sign in to comment.