Отказаться от DotNetBenchmark. Добавить порционное сохранение. Добавить формирование графика

This commit is contained in:
Roman Efremov 2024-12-18 17:09:32 +05:00
parent 86ea991934
commit 7fe7357712
6 changed files with 128 additions and 116 deletions

View File

@ -10,6 +10,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> <PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
<PackageReference Include="NPlot" Version="0.9.10" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<PackageReference Include="xunit.extensibility.core" Version="2.9.2" /> <PackageReference Include="xunit.extensibility.core" Version="2.9.2" />
</ItemGroup> </ItemGroup>

View File

@ -8,11 +8,7 @@ public class Program
{ {
private static void Main(string[] args) private static void Main(string[] args)
{ {
//var host = BenchmarkSwitcher.FromAssembly(typeof(Persistence.API.Program).Assembly); var count = Convert.ToInt32(Console.ReadLine());
//host.Run WitsDataBenchmark.ExecuteTest(count).Wait();
//System.Console.OutputEncoding = System.Text.Encoding.UTF8;
BenchmarkRunner.Run<WitsDataTest>();
} }
} }

View File

@ -1,26 +0,0 @@
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Database;
using DD.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);
}
}

View File

@ -0,0 +1,124 @@
using DD.Persistence.Client;
using DD.Persistence.Models;
using Microsoft.Extensions.DependencyInjection;
using NPlot;
using System.Diagnostics;
namespace Persistence.Benchmark.Tests;
public static class WitsDataBenchmark
{
private const string discriminatorId = "fef21bfd-e924-473d-b6c8-e4377e38245d";
public static async Task ExecuteTest(int count)
{
try
{
var factory = new WebAppFactoryFixture();
var scope = factory.Services.CreateScope();
var persistenceClientFactory = scope.ServiceProvider
.GetRequiredService<PersistenceClientFactory>();
var client = persistenceClientFactory.GetWitsDataClient();
var source = new CancellationTokenSource(TimeSpan.FromSeconds(6000));
var data = GenerateData(count);
var sw = new Stopwatch();
// Create a new bitmap plot surface
var plotSurface = new NPlot.Bitmap.PlotSurface2D(800, 600);
// Create a line plot
var linePlot = new LinePlot();
var abscissaData = new List<double>() {};
var ordinateData = new List<double>() {};
var saved = 0;
foreach (var item in data) {
var time = sw.Elapsed;
sw.Start();
var response = await client.AddRange(item, source.Token);
sw.Stop();
Console.WriteLine($"Сохранено: {response.ToString()}");
saved = saved + response;
abscissaData.Add(saved/100_000);
Console.WriteLine($"Затрачено времени на сохранение части: {sw.Elapsed - time}");
ordinateData.Add((double)(sw.Elapsed - time).TotalSeconds);
}
Console.WriteLine($"Затрачено времени на сохранение: {sw.Elapsed}");
// Add the line plot to the plot surface
linePlot.AbscissaData = abscissaData;
linePlot.OrdinateData = ordinateData;
plotSurface.Add(linePlot);
// Customize the plot (e.g., titles, labels)
plotSurface.Title = "График";
plotSurface.XAxis1.Label = "Количество сохраненных записей в БД (100.000)";
plotSurface.YAxis1.Label = "Время на сохранение 100.000 записей (сек.)";
// Refresh the plot to render it
plotSurface.Refresh();
#pragma warning disable
// Save the plot as a PNG image
plotSurface.Bitmap.Save("C:\\Users\\fremo\\source\\repos\\persistence\\Persistence.Benchmark\\plot.png", System.Drawing.Imaging.ImageFormat.Png);
#pragma warning enable
var dis = Guid.Parse(discriminatorId);
var date = DateTime.Now.AddDays(-1);
sw = new Stopwatch();
sw.Start();
var get = await client.GetPart(dis, date, count, source.Token);
sw.Stop();
Console.WriteLine($"Затрачено времени на вычитку: {sw.Elapsed}");
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
private static IEnumerable<IEnumerable<WitsDataDto>> GenerateData(int countToCreate)
{
var result = new List<List<WitsDataDto>>();
int enumerableCount = countToCreate / 100_000 + (countToCreate % 100_000 == 0 ? 0 : 1);
for (var k = 0; k < enumerableCount; k++)
{
var dtos = new List<WitsDataDto>();
for (var j = 0; j < 1000; j++)
{
for (var i = 0; i < 100; i++)
{
var timestamped = DateTimeOffset.UtcNow;
var random = new Random();
dtos.Add(new WitsDataDto()
{
DiscriminatorId = Guid.Parse(discriminatorId),
Timestamped = timestamped.AddSeconds(i),
Values = new List<WitsValueDto>()
{
new WitsValueDto()
{
RecordId = i + 1,
ItemId = i + 1,
Value = random.Next(1, 100)
}
}
});
}
}
result.Add(dtos);
}
return result;
}
}

View File

@ -1,83 +0,0 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using Microsoft.Extensions.DependencyInjection;
using DD.Persistence.Client;
using DD.Persistence.Client.Clients;
using DD.Persistence.Client.Clients.Interfaces;
using DD.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 source = new CancellationTokenSource(TimeSpan.FromSeconds(6000));
var response = await client.AddRange(data, source.Token);
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 = 80_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;
}
}

View File

@ -50,7 +50,6 @@ public class WebAppFactoryFixture : WebApplicationFactory<DD.Persistence.App.Pro
var scopedServices = scope.ServiceProvider; var scopedServices = scope.ServiceProvider;
var dbContext = scopedServices.GetRequiredService<PersistencePostgresContext>(); var dbContext = scopedServices.GetRequiredService<PersistencePostgresContext>();
//dbContext.Database.SetCommandTimeout(5);
dbContext.Database.EnsureCreatedAndMigrated(); dbContext.Database.EnsureCreatedAndMigrated();
dbContext.SaveChanges(); dbContext.SaveChanges();
}); });