From 7fe735771267f790e1b6100cc54cfe15514b325d Mon Sep 17 00:00:00 2001 From: Roman Efremov Date: Wed, 18 Dec 2024 17:09:32 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D0=B7=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=D1=81=D1=8F=20=D0=BE=D1=82=20DotNetBenchmark.=20=D0=94?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D0=BF=D0=BE=D1=80?= =?UTF-8?q?=D1=86=D0=B8=D0=BE=D0=BD=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.=20=D0=94=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D1=82=D1=8C=20=D1=84=D0=BE=D1=80=D0=BC?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=84=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Persistence.Benchmark.csproj | 2 + Persistence.Benchmark/Program.cs | 8 +- .../Tests/BaseIntegrationTest.cs | 26 ---- .../Tests/WitsDataBenchmark.cs | 124 ++++++++++++++++++ Persistence.Benchmark/Tests/WitsDataTest.cs | 83 ------------ Persistence.Benchmark/WebAppFactoryFixture.cs | 1 - 6 files changed, 128 insertions(+), 116 deletions(-) delete mode 100644 Persistence.Benchmark/Tests/BaseIntegrationTest.cs create mode 100644 Persistence.Benchmark/Tests/WitsDataBenchmark.cs delete mode 100644 Persistence.Benchmark/Tests/WitsDataTest.cs diff --git a/Persistence.Benchmark/Persistence.Benchmark.csproj b/Persistence.Benchmark/Persistence.Benchmark.csproj index 0a8f325..8649307 100644 --- a/Persistence.Benchmark/Persistence.Benchmark.csproj +++ b/Persistence.Benchmark/Persistence.Benchmark.csproj @@ -10,6 +10,8 @@ + + diff --git a/Persistence.Benchmark/Program.cs b/Persistence.Benchmark/Program.cs index d887886..a9285c2 100644 --- a/Persistence.Benchmark/Program.cs +++ b/Persistence.Benchmark/Program.cs @@ -8,11 +8,7 @@ public class Program { private static void Main(string[] args) { - //var host = BenchmarkSwitcher.FromAssembly(typeof(Persistence.API.Program).Assembly); - //host.Run - - //System.Console.OutputEncoding = System.Text.Encoding.UTF8; - - BenchmarkRunner.Run(); + var count = Convert.ToInt32(Console.ReadLine()); + WitsDataBenchmark.ExecuteTest(count).Wait(); } } \ No newline at end of file diff --git a/Persistence.Benchmark/Tests/BaseIntegrationTest.cs b/Persistence.Benchmark/Tests/BaseIntegrationTest.cs deleted file mode 100644 index 1fe9ed8..0000000 --- a/Persistence.Benchmark/Tests/BaseIntegrationTest.cs +++ /dev/null @@ -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, IDisposable -{ - protected readonly IServiceScope scope; - - protected readonly PersistenceDbContext dbContext; - - protected BaseIntegrationTest(WebAppFactoryFixture factory) - { - scope = factory.Services.CreateScope(); - - dbContext = scope.ServiceProvider.GetRequiredService(); - } - - public void Dispose() - { - scope.Dispose(); - dbContext.Dispose(); - GC.SuppressFinalize(this); - } -} diff --git a/Persistence.Benchmark/Tests/WitsDataBenchmark.cs b/Persistence.Benchmark/Tests/WitsDataBenchmark.cs new file mode 100644 index 0000000..0c6e18b --- /dev/null +++ b/Persistence.Benchmark/Tests/WitsDataBenchmark.cs @@ -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(); + 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() {}; + var ordinateData = new List() {}; + + 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> GenerateData(int countToCreate) + { + var result = new List>(); + + int enumerableCount = countToCreate / 100_000 + (countToCreate % 100_000 == 0 ? 0 : 1); + for (var k = 0; k < enumerableCount; k++) + { + var dtos = new List(); + 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() + { + new WitsValueDto() + { + RecordId = i + 1, + ItemId = i + 1, + Value = random.Next(1, 100) + } + } + }); + } + } + result.Add(dtos); + } + + return result; + } +} diff --git a/Persistence.Benchmark/Tests/WitsDataTest.cs b/Persistence.Benchmark/Tests/WitsDataTest.cs deleted file mode 100644 index 0fcdc59..0000000 --- a/Persistence.Benchmark/Tests/WitsDataTest.cs +++ /dev/null @@ -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 data; - public WitsDataTest() - { - var factory = new WebAppFactoryFixture(); - - var scope = factory.Services.CreateScope(); - var persistenceClientFactory = scope.ServiceProvider - .GetRequiredService(); - 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(); - - 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() - { - new WitsValueDto() - { - RecordId = i + 1, - ItemId = i + 1, - Value = random.Next(1, 100) - } - } - }); - } - } - - data = dtos; - } -} diff --git a/Persistence.Benchmark/WebAppFactoryFixture.cs b/Persistence.Benchmark/WebAppFactoryFixture.cs index ad7a0a3..8741156 100644 --- a/Persistence.Benchmark/WebAppFactoryFixture.cs +++ b/Persistence.Benchmark/WebAppFactoryFixture.cs @@ -50,7 +50,6 @@ public class WebAppFactoryFixture : WebApplicationFactory(); - //dbContext.Database.SetCommandTimeout(5); dbContext.Database.EnsureCreatedAndMigrated(); dbContext.SaveChanges(); });