-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 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
106 changes: 106 additions & 0 deletions
106
tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs
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,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 GitHub Actions / build
|
||
|
||
protected IServiceProvider ServiceProvider { get; private set; } | ||
Check warning on line 18 in tests/TransactionContext.Postgres.Benchmark/BenchmarkTest.cs GitHub Actions / build
|
||
|
||
[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); | ||
} | ||
} | ||
} |
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,3 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
22 changes: 22 additions & 0 deletions
22
tests/TransactionContext.Postgres.Benchmark/TransactionContext.Postgres.Benchmark.csproj
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,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> |