persistence/Persistence.Benchmark/Tests/WitsDataBenchmark.cs

130 lines
4.5 KiB
C#
Raw Normal View History

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 / 25_000 + (countToCreate % 25_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 < 25; 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;
}
}