GenerationBenchmark.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using NUnit.Framework;
  8. using QuestPDF.Elements;
  9. using QuestPDF.Fluent;
  10. using QuestPDF.Helpers;
  11. namespace QuestPDF.Examples
  12. {
  13. public class ProcessRunningTime
  14. {
  15. public TimeSpan FluentTime { get; set; }
  16. public TimeSpan GenerationTime { get; set; }
  17. public float Size { get; set; }
  18. }
  19. public class GenerationBenchmark
  20. {
  21. public const int TestSize = 4096;
  22. [Test]
  23. public void BenchmarkAsync()
  24. {
  25. RunTest(() => Enumerable
  26. .Range(0, TestSize)
  27. .AsParallel() // difference
  28. .Select(GenerateAndCollect)
  29. .ToList());
  30. }
  31. [Test]
  32. public void BenchmarkSync()
  33. {
  34. RunTest(() => Enumerable
  35. .Range(0, TestSize)
  36. .Select(GenerateAndCollect)
  37. .ToList());
  38. }
  39. public void RunTest(Func<IEnumerable<ProcessRunningTime>> handler)
  40. {
  41. var totalFluentTime = TimeSpan.Zero;
  42. var totalGenerationTime = TimeSpan.Zero;
  43. var stopWatch = new Stopwatch();
  44. stopWatch.Start();
  45. var results = handler();
  46. stopWatch.Stop();
  47. foreach (var result in results)
  48. {
  49. totalFluentTime += result.FluentTime;
  50. totalGenerationTime += result.GenerationTime;
  51. }
  52. Console.WriteLine($"Fluent: {totalFluentTime:g}");
  53. Console.WriteLine($"Generation: {totalGenerationTime:g}");
  54. Console.WriteLine($"Total: {stopWatch.Elapsed:g}");
  55. }
  56. static ProcessRunningTime GenerateAndCollect(int attemptNumber)
  57. {
  58. var stopwatch = new Stopwatch();
  59. stopwatch.Start();
  60. var container = new Container();
  61. container
  62. .Padding(10)
  63. .MinimalBox()
  64. .Border(1)
  65. .Column(column =>
  66. {
  67. column.Item().Text($"Attempts {attemptNumber}");
  68. const int numberOfRows = 100;
  69. const int numberOfColumns = 10;
  70. for (var y = 0; y < numberOfRows; y++)
  71. {
  72. column.Item().Row(row =>
  73. {
  74. for (var x = 0; x < numberOfColumns; x++)
  75. {
  76. row.RelativeItem()
  77. .Background(Colors.Red.Lighten5)
  78. .Padding(3)
  79. .Background(Colors.Red.Lighten4)
  80. .Padding(3)
  81. .Background(Colors.Red.Lighten3)
  82. .Padding(3)
  83. .Background(Colors.Red.Lighten2)
  84. .Padding(3)
  85. .Background(Colors.Red.Lighten1)
  86. .Padding(3)
  87. .Background(Colors.Red.Medium)
  88. .Padding(3)
  89. .Background(Colors.Red.Darken1)
  90. .Padding(3)
  91. .Background(Colors.Red.Darken2)
  92. .Padding(3)
  93. .Background(Colors.Red.Darken3)
  94. .Padding(3)
  95. .Background(Colors.Red.Darken4)
  96. .Height(3);
  97. }
  98. });
  99. }
  100. });
  101. var fluentTime = stopwatch.Elapsed;
  102. stopwatch.Reset();
  103. stopwatch.Start();
  104. var size = Document
  105. .Create(x => x.Page(page => page.Content().Element(container)))
  106. .GeneratePdf()
  107. .Length;
  108. var generationTime = stopwatch.Elapsed;
  109. return new ProcessRunningTime
  110. {
  111. FluentTime = fluentTime,
  112. GenerationTime = generationTime,
  113. Size = size
  114. };
  115. }
  116. }
  117. }