persistence/DD.Persistence.Benchmark/Tests/WitsDataBenchmark.cs
Roman Efremov f06eda826b
Some checks failed
Unit tests / test (push) Failing after 3m39s
Добавить результаты тестирования
2024-12-26 16:53:52 +05:00

101 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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";
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}");
var discriminator = Guid.Parse(discriminatorId);
var date = DateTime.Now.AddDays(-365);
Console.WriteLine($"\nВычитка...");
sw = new Stopwatch();
sw.Start();
var get = await client.GetPart(discriminator, date, count, source.Token);
sw.Stop();
Console.WriteLine($"Затрачено времени на вычитку {get.SelectMany(e => e.Values).Count()} записей: {sw.Elapsed}");
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
private static IEnumerable<IEnumerable<WitsDataDto>> GenerateData(int countToCreate)
{
var result = new List<List<WitsDataDto>>();
var time = DateTimeOffset.UtcNow;
var seconds = -1;
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++)
{
for (var i = 0; i < chunkSize / 1000; i++)
{
var random = new Random();
var timestamped = time.AddSeconds(seconds);
seconds = seconds - 1;
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;
}
}