GenerationBenchmark.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 fluentTime = TimeSpan.Zero;
  59. var document = Document.Create(document =>
  60. {
  61. document.Page(page =>
  62. {
  63. page.Content()
  64. .Padding(10)
  65. .Shrink()
  66. .Border(1)
  67. .Column(column =>
  68. {
  69. var fluentTimeStopwatch = new Stopwatch();
  70. fluentTimeStopwatch.Start();
  71. column.Item().Text($"Attempts {attemptNumber}");
  72. const int numberOfRows = 100;
  73. const int numberOfColumns = 10;
  74. for (var y = 0; y < numberOfRows; y++)
  75. {
  76. column.Item().Row(row =>
  77. {
  78. for (var x = 0; x < numberOfColumns; x++)
  79. {
  80. row.RelativeItem()
  81. .Background(Colors.Red.Lighten5)
  82. .Padding(3)
  83. .Background(Colors.Red.Lighten4)
  84. .Padding(3)
  85. .Background(Colors.Red.Lighten3)
  86. .Padding(3)
  87. .Background(Colors.Red.Lighten2)
  88. .Padding(3)
  89. .Background(Colors.Red.Lighten1)
  90. .Padding(3)
  91. .Background(Colors.Red.Medium)
  92. .Padding(3)
  93. .Background(Colors.Red.Darken1)
  94. .Padding(3)
  95. .Background(Colors.Red.Darken2)
  96. .Padding(3)
  97. .Background(Colors.Red.Darken3)
  98. .Padding(3)
  99. .Background(Colors.Red.Darken4)
  100. .Height(3);
  101. }
  102. });
  103. }
  104. fluentTime = fluentTimeStopwatch.Elapsed;
  105. });
  106. });
  107. });
  108. var generationTimeStopWatch = new Stopwatch();
  109. generationTimeStopWatch.Start();
  110. var size = document.GeneratePdf().Length;
  111. var generationTime = generationTimeStopWatch.Elapsed;
  112. return new ProcessRunningTime
  113. {
  114. FluentTime = fluentTime,
  115. GenerationTime = generationTime,
  116. Size = size
  117. };
  118. }
  119. }
  120. }