PerformanceTests.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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: 256)]
  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. ImagePlaceholder.Solid = true;
  29. var configuration = ManualConfig
  30. .Create(DefaultConfig.Instance)
  31. .WithOptions(ConfigOptions.DisableOptimizationsValidator);
  32. BenchmarkRunner.Run<PerformanceTests>(configuration);
  33. }
  34. [IterationSetup]
  35. public void GenerateReportData()
  36. {
  37. ImagePlaceholder.Solid = true;
  38. var model = DataSource.GetReport();
  39. var report = new StandardReport(model);
  40. Metadata = report.GetMetadata();
  41. var documentContainer = new DocumentContainer();
  42. report.Compose(documentContainer);
  43. Content = documentContainer.Compose();
  44. PageContext = new PageContext();
  45. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  46. var sw = new Stopwatch();
  47. sw.Start();
  48. Content.HandleVisitor(x =>
  49. {
  50. if (x is ICacheable)
  51. x.CreateProxy(y => new CacheProxy(y));
  52. });
  53. sw.Stop();
  54. Console.WriteLine($"Creating cache took: {sw.ElapsedMilliseconds}");
  55. }
  56. [Benchmark]
  57. public void GenerationTest()
  58. {
  59. DocumentGenerator.RenderPass(PageContext, new FreeCanvas(), Content, Metadata, null);
  60. }
  61. }
  62. }