persistence/DD.Persistence.Benchmark/Tests/WitsDataBenchmark.cs

98 lines
3.2 KiB
C#
Raw Normal View History

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;
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();
//var saved = 0;
//foreach (var item in data)
//{
// var time = sw.Elapsed;
// sw.Start();
// var response = await client.AddRange(item, source.Token);
// sw.Stop();
// saved = saved + response;
// Console.WriteLine($"Сохранено: {saved.ToString()}");
//}
//Console.WriteLine($"Затрачено времени на сохранение: {sw.Elapsed}");
2024-12-25 14:54:25 +05:00
var discriminator = Guid.Parse(discriminatorId);
var date = DateTime.Now.AddDays(-31);
2024-12-25 14:54:25 +05:00
Console.WriteLine($"\nВычитка...");
sw = new Stopwatch();
sw.Start();
2024-12-25 14:54:25 +05:00
var get = await client.GetPart(discriminator, 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>>();
2024-12-25 14:54:25 +05:00
int enumerableCount = countToCreate / chunkSize + (countToCreate % chunkSize == 0 ? 0 : 1);
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++)
{
var random = new Random();
2024-12-25 14:54:25 +05:00
var timestamped = DateTimeOffset.UtcNow.AddSeconds(random.Next(1, 60 * 60 * 24 * 30) * -1);
2024-12-25 14:54:25 +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;
}
}