GenerationBenchmark.cs 4.7 KB

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