2024-12-18 17:09:32 +05:00
|
|
|
|
using DD.Persistence.Client;
|
|
|
|
|
using DD.Persistence.Models;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace Persistence.Benchmark.Tests;
|
|
|
|
|
|
|
|
|
|
public static class WitsDataBenchmark
|
|
|
|
|
{
|
|
|
|
|
private const string discriminatorId = "fef21bfd-e924-473d-b6c8-e4377e38245d";
|
2024-12-25 14:54:25 +05:00
|
|
|
|
private const int chunkSize = 25_000;
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
|
|
|
|
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));
|
2024-12-26 16:53:52 +05:00
|
|
|
|
var data = GenerateData(count);
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
|
|
|
|
var sw = new Stopwatch();
|
2024-12-26 16:53:52 +05:00
|
|
|
|
var saved = 0;
|
|
|
|
|
foreach (var item in data)
|
|
|
|
|
{
|
|
|
|
|
var time = sw.Elapsed;
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
2024-12-26 16:53:52 +05:00
|
|
|
|
sw.Start();
|
|
|
|
|
var response = await client.AddRange(item, source.Token);
|
|
|
|
|
sw.Stop();
|
|
|
|
|
saved = saved + response;
|
|
|
|
|
Console.WriteLine($"Сохранено: {saved.ToString()}");
|
|
|
|
|
}
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
2024-12-26 16:53:52 +05:00
|
|
|
|
Console.WriteLine($"Затрачено времени на сохранение: {sw.Elapsed}");
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
2024-12-25 14:54:25 +05:00
|
|
|
|
var discriminator = Guid.Parse(discriminatorId);
|
2024-12-26 16:53:52 +05:00
|
|
|
|
var date = DateTime.Now.AddDays(-365);
|
2024-12-18 17:09:32 +05:00
|
|
|
|
|
2024-12-25 14:54:25 +05:00
|
|
|
|
Console.WriteLine($"\nВычитка...");
|
|
|
|
|
|
2024-12-18 17:09:32 +05:00
|
|
|
|
sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
2024-12-25 14:54:25 +05:00
|
|
|
|
var get = await client.GetPart(discriminator, date, count, source.Token);
|
2024-12-18 17:09:32 +05:00
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2024-12-26 16:53:52 +05:00
|
|
|
|
Console.WriteLine($"Затрачено времени на вычитку {get.SelectMany(e => e.Values).Count()} записей: {sw.Elapsed}");
|
2024-12-22 21:00:32 +05:00
|
|
|
|
|
2024-12-18 17:09:32 +05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<IEnumerable<WitsDataDto>> GenerateData(int countToCreate)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<List<WitsDataDto>>();
|
|
|
|
|
|
2024-12-26 16:53:52 +05:00
|
|
|
|
var time = DateTimeOffset.UtcNow;
|
|
|
|
|
var seconds = -1;
|
2024-12-25 14:54:25 +05:00
|
|
|
|
int enumerableCount = countToCreate / chunkSize + (countToCreate % chunkSize == 0 ? 0 : 1);
|
2024-12-18 17:09:32 +05:00
|
|
|
|
for (var k = 0; k < enumerableCount; k++)
|
|
|
|
|
{
|
|
|
|
|
var dtos = new List<WitsDataDto>();
|
|
|
|
|
for (var j = 0; j < 1000; j++)
|
|
|
|
|
{
|
2024-12-25 14:54:25 +05:00
|
|
|
|
for (var i = 0; i < chunkSize / 1000; i++)
|
2024-12-18 17:09:32 +05:00
|
|
|
|
{
|
|
|
|
|
var random = new Random();
|
2024-12-25 14:54:25 +05:00
|
|
|
|
|
2024-12-26 16:53:52 +05:00
|
|
|
|
var timestamped = time.AddSeconds(seconds);
|
|
|
|
|
seconds = seconds - 1;
|
2024-12-25 14:54:25 +05:00
|
|
|
|
|
2024-12-18 17:09:32 +05:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|