PerformanceTests.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using BenchmarkDotNet.Attributes;
  6. using BenchmarkDotNet.Configs;
  7. using BenchmarkDotNet.Engines;
  8. using BenchmarkDotNet.Running;
  9. using Microsoft.VisualStudio.TestPlatform.TestHost;
  10. using NUnit.Framework;
  11. using QuestPDF.Drawing;
  12. using QuestPDF.Drawing.Proxy;
  13. using QuestPDF.Elements;
  14. using QuestPDF.Helpers;
  15. using QuestPDF.Infrastructure;
  16. using QuestPDF.ReportSample.Layouts;
  17. namespace QuestPDF.ReportSample
  18. {
  19. [SimpleJob(RunStrategy.Monitoring, launchCount: 0, warmupCount: 1, targetCount: 256)]
  20. [MinColumn, MaxColumn, MeanColumn, MedianColumn]
  21. public class PerformanceTests
  22. {
  23. private PageContext PageContext { get; set; }
  24. private DocumentMetadata Metadata { get; set; }
  25. private Container Content { get; set; }
  26. [Test]
  27. public void Run()
  28. {
  29. ImagePlaceholder.Solid = true;
  30. var configuration = ManualConfig
  31. .Create(DefaultConfig.Instance)
  32. .WithOptions(ConfigOptions.DisableOptimizationsValidator);
  33. BenchmarkRunner.Run<PerformanceTests>(configuration);
  34. }
  35. [IterationSetup]
  36. public void GenerateReportData()
  37. {
  38. ImagePlaceholder.Solid = true;
  39. var model = DataSource.GetReport();
  40. var report = new StandardReport(model);
  41. Metadata = report.GetMetadata();
  42. var documentContainer = new DocumentContainer();
  43. report.Compose(documentContainer);
  44. Content = documentContainer.Compose();
  45. PageContext = new PageContext();
  46. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  47. var sw = new Stopwatch();
  48. sw.Start();
  49. Content.VisitChildren(x =>
  50. {
  51. if (x is ICacheable)
  52. x.CreateProxy(y => new CacheProxy(y));
  53. });
  54. sw.Stop();
  55. Console.WriteLine($"Creating cache took: {sw.ElapsedMilliseconds}");
  56. }
  57. [Benchmark]
  58. public void GenerationTest()
  59. {
  60. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  61. }
  62. }
  63. }