PerformanceTests.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.Infrastructure;
  15. using QuestPDF.ReportSample.Layouts;
  16. namespace QuestPDF.ReportSample
  17. {
  18. [SimpleJob(RunStrategy.Monitoring, launchCount: 0, warmupCount: 1, targetCount: 100)]
  19. [MinColumn, MaxColumn, MeanColumn, MedianColumn]
  20. public class PerformanceTests
  21. {
  22. private PageContext PageContext { get; set; }
  23. private DocumentMetadata Metadata { get; set; }
  24. private Container Content { get; set; }
  25. [Test]
  26. public void Run()
  27. {
  28. var configuration = ManualConfig
  29. .Create(DefaultConfig.Instance)
  30. .WithOptions(ConfigOptions.DisableOptimizationsValidator);
  31. BenchmarkRunner.Run<PerformanceTests>(configuration);
  32. }
  33. [IterationSetup]
  34. public void GenerateReportData()
  35. {
  36. var model = DataSource.GetReport();
  37. var report = new StandardReport(model);
  38. Metadata = report.GetMetadata();
  39. var documentContainer = new DocumentContainer();
  40. report.Compose(documentContainer);
  41. Content = documentContainer.Compose();
  42. PageContext = new PageContext();
  43. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  44. var sw = new Stopwatch();
  45. sw.Start();
  46. Content.HandleVisitor(x =>
  47. {
  48. if (x is ICacheable)
  49. x.CreateProxy(y => new CacheProxy(y));
  50. });
  51. sw.Stop();
  52. Console.WriteLine($"Creating cache took: {sw.ElapsedMilliseconds}");
  53. }
  54. [Benchmark]
  55. public void GenerationTest()
  56. {
  57. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  58. }
  59. }
  60. }