From 2f1dd4b383f564d2a251217b62878c1e276d6d68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Zi=C4=85bek?= Date: Mon, 6 Sep 2021 21:43:12 +0200 Subject: [PATCH] Improved tests with Benchmark.net --- QuestPDF.ReportSample/Helpers.cs | 2 +- QuestPDF.ReportSample/PerformanceTests.cs | 47 ++++++++++++++ .../QuestPDF.ReportSample.csproj | 1 + QuestPDF.ReportSample/Tests.cs | 63 ++++++------------- 4 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 QuestPDF.ReportSample/PerformanceTests.cs diff --git a/QuestPDF.ReportSample/Helpers.cs b/QuestPDF.ReportSample/Helpers.cs index 87d2ace..cb9af62 100644 --- a/QuestPDF.ReportSample/Helpers.cs +++ b/QuestPDF.ReportSample/Helpers.cs @@ -6,7 +6,7 @@ namespace QuestPDF.ReportSample { public static class Helpers { - public static Random Random { get; } = new Random(); + public static Random Random { get; } = new Random(1); public static string GetTestItem(string path) => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", path); diff --git a/QuestPDF.ReportSample/PerformanceTests.cs b/QuestPDF.ReportSample/PerformanceTests.cs new file mode 100644 index 0000000..0fd0e85 --- /dev/null +++ b/QuestPDF.ReportSample/PerformanceTests.cs @@ -0,0 +1,47 @@ +using System.Collections.Generic; +using System.Linq; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Engines; +using BenchmarkDotNet.Running; +using Microsoft.VisualStudio.TestPlatform.TestHost; +using NUnit.Framework; +using QuestPDF.Drawing; +using QuestPDF.Infrastructure; +using QuestPDF.ReportSample.Layouts; + +namespace QuestPDF.ReportSample +{ + [SimpleJob(RunStrategy.Monitoring, launchCount: 0, warmupCount: 1, targetCount: 100)] + [MinColumn, MaxColumn, MeanColumn, MedianColumn] + public class PerformanceTests + { + private StandardReport Report { get; set; } + + [Test] + public void Run() + { + BenchmarkRunner.Run(); + } + + [IterationSetup] + public void GenerateReportData() + { + var model = DataSource.GetReport(); + Report = new StandardReport(model); + } + + [Benchmark] + public void GenerationTest() + { + var container = new DocumentContainer(); + Report.Compose(container); + var content = container.Compose(); + + var metadata = Report.GetMetadata(); + var pageContext = new PageContext(); + + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); + } + } +} \ No newline at end of file diff --git a/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj b/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj index 3c49859..72e9f0b 100644 --- a/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj +++ b/QuestPDF.ReportSample/QuestPDF.ReportSample.csproj @@ -8,6 +8,7 @@ + diff --git a/QuestPDF.ReportSample/Tests.cs b/QuestPDF.ReportSample/Tests.cs index 6e68acc..78f0da4 100644 --- a/QuestPDF.ReportSample/Tests.cs +++ b/QuestPDF.ReportSample/Tests.cs @@ -5,66 +5,43 @@ using System.Linq; using NUnit.Framework; using QuestPDF.Drawing; using QuestPDF.Fluent; +using QuestPDF.Infrastructure; using QuestPDF.ReportSample.Layouts; namespace QuestPDF.ReportSample { public class ReportGeneration { + private StandardReport Report { get; set; } + + [SetUp] + public void SetUp() + { + var model = DataSource.GetReport(); + Report = new StandardReport(model); + } + [Test] public void GenerateAndShow() { - var reportModel = DataSource.GetReport(); - var report = new StandardReport(reportModel); - var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"test_result.pdf"); - report.GeneratePdf(path); + Report.GeneratePdf(path); Process.Start("explorer.exe", path); } - + [Test] - public void PerformanceBenchmark() + public void Profile() { - // target document length should be around 100 pages + var container = new DocumentContainer(); + Report.Compose(container); + var content = container.Compose(); - // test size - const int testSize = 100; - const decimal performanceTarget = 1; // documents per second + var metadata = Report.GetMetadata(); + var pageContext = new PageContext(); - // create report models - var reports = Enumerable - .Range(0, testSize) - .Select(x => - { - var reportModel = DataSource.GetReport(); - return new StandardReport(reportModel); - }) - .ToList(); - - // generate documents - var sw = new Stopwatch(); - - sw.Start(); - var totalSize = MethodToOptimize(); - sw.Stop(); - - // show summary - Console.WriteLine($"Total size: {totalSize:N0} bytes"); - - var performance = sw.ElapsedMilliseconds / (decimal)testSize; - var speed = 1000M / performance; - Console.WriteLine($"Test time: {sw.ElapsedMilliseconds} ms"); - Console.WriteLine($"Time per document: {performance:N} ms"); - Console.WriteLine($"Documents per second: {speed:N} d/s"); - - if (speed < performanceTarget) - throw new Exception("Rendering algorithm is too slow."); - - long MethodToOptimize() - { - return reports.Select(x => x.GeneratePdf()).Sum(x => (long)x.Length); - } + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); + DocumentGenerator.RenderPass(pageContext, new FreeCanvas(), content, metadata, null); } } } \ No newline at end of file