Добавить Persistence.Benchmark
This commit is contained in:
parent
e3319867dd
commit
74959284f3
@ -2,7 +2,8 @@
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Error"
|
||||
}
|
||||
},
|
||||
"ConnectionStrings": {
|
||||
|
12
Persistence.Benchmark/Database/BenchmarkDbContext.cs
Normal file
12
Persistence.Benchmark/Database/BenchmarkDbContext.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Persistence.Benchmark.Database.Entities;
|
||||
using Persistence.Database;
|
||||
|
||||
namespace Persistence.Benchmark.Database;
|
||||
public class BenchmarkDbContext : PersistenceDbContext
|
||||
{
|
||||
public new DbSet<ParameterData> ParameterData => Set<ParameterData>();
|
||||
public BenchmarkDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
}
|
14
Persistence.Benchmark/Database/DbConnection.cs
Normal file
14
Persistence.Benchmark/Database/DbConnection.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace Persistence.Benchmark.Database;
|
||||
public class DbConnection
|
||||
{
|
||||
public string Host { get; set; } = null!;
|
||||
|
||||
public int Port { get; set; }
|
||||
|
||||
public string Username { get; set; } = null!;
|
||||
|
||||
public string Password { get; set; } = null!;
|
||||
|
||||
public string GetConnectionString() =>
|
||||
$"Host={Host};Database={Guid.NewGuid()};Port={Port};Username={Username};Password={Password};Persist Security Info=True;Include Error Detail=True";
|
||||
}
|
22
Persistence.Benchmark/Database/Entities/ParameterData.cs
Normal file
22
Persistence.Benchmark/Database/Entities/ParameterData.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace Persistence.Benchmark.Database.Entities;
|
||||
public class ParameterData
|
||||
{
|
||||
[Key]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[Required, Comment("Дискриминатор системы")]
|
||||
public Guid DiscriminatorId { get; set; }
|
||||
|
||||
[Comment("Id параметра")]
|
||||
public int ParameterId { get; set; }
|
||||
|
||||
[Column(TypeName = "varchar(256)"), Comment("Значение параметра в виде строки")]
|
||||
public required string Value { get; set; }
|
||||
|
||||
[Comment("Временная отметка")]
|
||||
public DateTimeOffset Timestamp { get; set; }
|
||||
}
|
23
Persistence.Benchmark/Persistence.Benchmark.csproj
Normal file
23
Persistence.Benchmark/Persistence.Benchmark.csproj
Normal file
@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
|
||||
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Persistence.API\Persistence.API.csproj" />
|
||||
<ProjectReference Include="..\Persistence.Client\Persistence.Client.csproj" />
|
||||
<ProjectReference Include="..\Persistence.Database.Postgres\Persistence.Database.Postgres.csproj" />
|
||||
<ProjectReference Include="..\Persistence.Database\Persistence.Database.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
18
Persistence.Benchmark/Program.cs
Normal file
18
Persistence.Benchmark/Program.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using BenchmarkDotNet.Running;
|
||||
using Persistence.Benchmark;
|
||||
using Persistence.Benchmark.Tests;
|
||||
using Persistence.API;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
//var host = BenchmarkSwitcher.FromAssembly(typeof(Persistence.API.Program).Assembly);
|
||||
//host.Run
|
||||
|
||||
//System.Console.OutputEncoding = System.Text.Encoding.UTF8;
|
||||
|
||||
BenchmarkRunner.Run<WitsDataTest>();
|
||||
}
|
||||
}
|
18
Persistence.Benchmark/TestHttpClientFactory.cs
Normal file
18
Persistence.Benchmark/TestHttpClientFactory.cs
Normal file
@ -0,0 +1,18 @@
|
||||
namespace Persistence.Benchmark;
|
||||
|
||||
/// <summary>
|
||||
/// Фабрика HTTP клиентов для интеграционных тестов
|
||||
/// </summary>
|
||||
public class TestHttpClientFactory : IHttpClientFactory
|
||||
{
|
||||
private readonly WebAppFactoryFixture factory;
|
||||
|
||||
public TestHttpClientFactory(WebAppFactoryFixture factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
public HttpClient CreateClient(string name)
|
||||
{
|
||||
return factory.CreateClient();
|
||||
}
|
||||
}
|
26
Persistence.Benchmark/Tests/BaseIntegrationTest.cs
Normal file
26
Persistence.Benchmark/Tests/BaseIntegrationTest.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Persistence.Database;
|
||||
using Persistence.Database.Model;
|
||||
using Xunit;
|
||||
|
||||
namespace Persistence.Benchmark;
|
||||
public abstract class BaseIntegrationTest : IClassFixture<WebAppFactoryFixture>, IDisposable
|
||||
{
|
||||
protected readonly IServiceScope scope;
|
||||
|
||||
protected readonly PersistenceDbContext dbContext;
|
||||
|
||||
protected BaseIntegrationTest(WebAppFactoryFixture factory)
|
||||
{
|
||||
scope = factory.Services.CreateScope();
|
||||
|
||||
dbContext = scope.ServiceProvider.GetRequiredService<PersistencePostgresContext>();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
scope.Dispose();
|
||||
dbContext.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
82
Persistence.Benchmark/Tests/WitsDataTest.cs
Normal file
82
Persistence.Benchmark/Tests/WitsDataTest.cs
Normal file
@ -0,0 +1,82 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Engines;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Persistence.Client;
|
||||
using Persistence.Client.Clients;
|
||||
using Persistence.Client.Clients.Interfaces;
|
||||
using Persistence.Models;
|
||||
|
||||
namespace Persistence.Benchmark.Tests;
|
||||
|
||||
[SimpleJob(RunStrategy.ColdStart, 1)]
|
||||
public class WitsDataTest
|
||||
{
|
||||
private readonly IWitsDataClient client;
|
||||
public IEnumerable<WitsDataDto> data;
|
||||
public WitsDataTest()
|
||||
{
|
||||
var factory = new WebAppFactoryFixture();
|
||||
|
||||
var scope = factory.Services.CreateScope();
|
||||
var persistenceClientFactory = scope.ServiceProvider
|
||||
.GetRequiredService<PersistenceClientFactory>();
|
||||
client = persistenceClientFactory.GetWitsDataClient();
|
||||
|
||||
//data = GenerateData(1_000_000);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
[IterationCount(1)]
|
||||
public async Task WithCompositePk()
|
||||
{
|
||||
try
|
||||
{
|
||||
//var data = GenerateData(1_000_000);
|
||||
var response = await client.AddRange(data, CancellationToken.None);
|
||||
Console.WriteLine(response.ToString());
|
||||
//var discriminatorId = data.FirstOrDefault()!.DiscriminatorId;
|
||||
//var date = DateTimeOffset.UtcNow.AddDays(-1);
|
||||
//Console.WriteLine(date.ToString());
|
||||
|
||||
//var test = await client.GetPart(discriminatorId, date);
|
||||
//Console.WriteLine(test.FirstOrDefault()?.DiscriminatorId.ToString());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[GlobalSetup]
|
||||
public void GenerateData()
|
||||
{
|
||||
int countToCreate = 5_000_000;
|
||||
|
||||
var dtos = new List<WitsDataDto>();
|
||||
|
||||
for (var j = 0; j < countToCreate / 100 ; j++)
|
||||
{
|
||||
for (var i = 0; i < countToCreate && i < 100; i++)
|
||||
{
|
||||
var discriminatorId = Guid.NewGuid();
|
||||
var timestamped = DateTimeOffset.UtcNow;
|
||||
var random = new Random();
|
||||
dtos.Add(new WitsDataDto()
|
||||
{
|
||||
DiscriminatorId = discriminatorId,
|
||||
Timestamped = timestamped.AddSeconds(i),
|
||||
Values = new List<WitsValueDto>()
|
||||
{
|
||||
new WitsValueDto()
|
||||
{
|
||||
RecordId = i + 1,
|
||||
ItemId = i + 1,
|
||||
Value = random.Next(1, 100)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
data = dtos;
|
||||
}
|
||||
}
|
71
Persistence.Benchmark/WebAppFactoryFixture.cs
Normal file
71
Persistence.Benchmark/WebAppFactoryFixture.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Persistence.API;
|
||||
using Persistence.Benchmark.Database;
|
||||
using Persistence.Client;
|
||||
using Persistence.Database.Model;
|
||||
using Persistence.Database.Postgres;
|
||||
|
||||
namespace Persistence.Benchmark;
|
||||
public class WebAppFactoryFixture : WebApplicationFactory<Startup>
|
||||
{
|
||||
private string connectionString = string.Empty;
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
builder.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.AddJsonFile("appsettings.Tests.json");
|
||||
|
||||
var dbConnection = config.Build().GetSection("DbConnection").Get<DbConnection>()!;
|
||||
connectionString = dbConnection.GetConnectionString();
|
||||
});
|
||||
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<PersistencePostgresContext>));
|
||||
if (descriptor != null)
|
||||
services.Remove(descriptor);
|
||||
|
||||
services.AddDbContext<PersistencePostgresContext>(options =>
|
||||
options.UseNpgsql(connectionString));
|
||||
|
||||
services.AddLogging(builder => builder.AddConsole());
|
||||
|
||||
services.RemoveAll<IHttpClientFactory>();
|
||||
services.AddSingleton<IHttpClientFactory>(provider =>
|
||||
{
|
||||
return new TestHttpClientFactory(this);
|
||||
});
|
||||
|
||||
services.AddSingleton<PersistenceClientFactory>();
|
||||
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var scopedServices = scope.ServiceProvider;
|
||||
|
||||
var dbContext = scopedServices.GetRequiredService<PersistencePostgresContext>();
|
||||
//dbContext.Database.SetCommandTimeout(5);
|
||||
dbContext.Database.EnsureCreatedAndMigrated();
|
||||
dbContext.SaveChanges();
|
||||
});
|
||||
}
|
||||
|
||||
public override async ValueTask DisposeAsync()
|
||||
{
|
||||
var dbContext = new PersistencePostgresContext(
|
||||
new DbContextOptionsBuilder<PersistencePostgresContext>()
|
||||
.UseNpgsql(connectionString)
|
||||
.Options);
|
||||
|
||||
await dbContext.Database.EnsureDeletedAsync();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
@ -15,7 +15,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.IntegrationTest
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.Database.Postgres", "Persistence.Database.Postgres\Persistence.Database.Postgres.csproj", "{CC284D27-162D-490C-B6CF-74D666B7C5F3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence.Client", "Persistence.Client\Persistence.Client.csproj", "{84B68660-48E6-4974-A4E5-517552D9DE23}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence.Client", "Persistence.Client\Persistence.Client.csproj", "{84B68660-48E6-4974-A4E5-517552D9DE23}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence.Benchmark", "Persistence.Benchmark\Persistence.Benchmark.csproj", "{7D358CCB-41E1-4C78-A33A-F21DE929F3B4}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -51,6 +53,10 @@ Global
|
||||
{84B68660-48E6-4974-A4E5-517552D9DE23}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{84B68660-48E6-4974-A4E5-517552D9DE23}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{84B68660-48E6-4974-A4E5-517552D9DE23}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{7D358CCB-41E1-4C78-A33A-F21DE929F3B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7D358CCB-41E1-4C78-A33A-F21DE929F3B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7D358CCB-41E1-4C78-A33A-F21DE929F3B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7D358CCB-41E1-4C78-A33A-F21DE929F3B4}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
Loading…
Reference in New Issue
Block a user