GenerationBenchmark.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. [Ignore("This test takes a lot of time to run")]
  24. public void BenchmarkAsync()
  25. {
  26. RunTest(() => Enumerable
  27. .Range(0, TestSize)
  28. .AsParallel() // difference
  29. .Select(GenerateAndCollect)
  30. .ToList());
  31. }
  32. [Test]
  33. [Ignore("This test takes a lot of time to run")]
  34. public void BenchmarkSync()
  35. {
  36. RunTest(() => Enumerable
  37. .Range(0, TestSize)
  38. .Select(GenerateAndCollect)
  39. .ToList());
  40. }
  41. public void RunTest(Func<IEnumerable<ProcessRunningTime>> handler)
  42. {
  43. var totalFluentTime = TimeSpan.Zero;
  44. var totalGenerationTime = TimeSpan.Zero;
  45. var stopWatch = new Stopwatch();
  46. stopWatch.Start();
  47. var results = handler();
  48. stopWatch.Stop();
  49. foreach (var result in results)
  50. {
  51. totalFluentTime += result.FluentTime;
  52. totalGenerationTime += result.GenerationTime;
  53. }
  54. Console.WriteLine($"Fluent: {totalFluentTime:g}");
  55. Console.WriteLine($"Generation: {totalGenerationTime:g}");
  56. Console.WriteLine($"Total: {stopWatch.Elapsed:g}");
  57. }
  58. static ProcessRunningTime GenerateAndCollect(int attemptNumber)
  59. {
  60. var fluentTime = TimeSpan.Zero;
  61. var document = Document.Create(document =>
  62. {
  63. document.Page(page =>
  64. {
  65. page.Content()
  66. .Padding(10)
  67. .Shrink()
  68. .Border(1)
  69. .Column(column =>
  70. {
  71. var fluentTimeStopwatch = new Stopwatch();
  72. fluentTimeStopwatch.Start();
  73. column.Item().Text($"Attempts {attemptNumber}");
  74. const int numberOfRows = 100;
  75. const int numberOfColumns = 10;
  76. for (var y = 0; y < numberOfRows; y++)
  77. {
  78. column.Item().Row(row =>
  79. {
  80. for (var x = 0; x < numberOfColumns; x++)
  81. {
  82. row.RelativeItem()
  83. .Background(Colors.Red.Lighten5)
  84. .Padding(3)
  85. .Background(Colors.Red.Lighten4)
  86. .Padding(3)
  87. .Background(Colors.Red.Lighten3)
  88. .Padding(3)
  89. .Background(Colors.Red.Lighten2)
  90. .Padding(3)
  91. .Background(Colors.Red.Lighten1)
  92. .Padding(3)
  93. .Background(Colors.Red.Medium)
  94. .Padding(3)
  95. .Background(Colors.Red.Darken1)
  96. .Padding(3)
  97. .Background(Colors.Red.Darken2)
  98. .Padding(3)
  99. .Background(Colors.Red.Darken3)
  100. .Padding(3)
  101. .Background(Colors.Red.Darken4)
  102. .Height(3);
  103. }
  104. });
  105. }
  106. fluentTime = fluentTimeStopwatch.Elapsed;
  107. });
  108. });
  109. });
  110. var generationTimeStopWatch = new Stopwatch();
  111. generationTimeStopWatch.Start();
  112. var size = document.GeneratePdf().Length;
  113. var generationTime = generationTimeStopWatch.Elapsed;
  114. return new ProcessRunningTime
  115. {
  116. FluentTime = fluentTime,
  117. GenerationTime = generationTime,
  118. Size = size
  119. };
  120. }
  121. }
  122. }