TableBenchmark.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using QuestPDF.Drawing;
  6. using QuestPDF.Elements;
  7. using QuestPDF.Examples.Engine;
  8. using QuestPDF.Fluent;
  9. using QuestPDF.Helpers;
  10. using QuestPDF.Infrastructure;
  11. namespace QuestPDF.Examples
  12. {
  13. public class TableBenchmark
  14. {
  15. [Test]
  16. public void Benchmark()
  17. {
  18. GenerateAndCollect();
  19. var stopwatch = new Stopwatch();
  20. stopwatch.Start();
  21. foreach (var _ in Enumerable.Range(0, 10000))
  22. {
  23. GenerateAndCollect();
  24. }
  25. stopwatch.Stop();
  26. Console.WriteLine($"Execution time: {stopwatch.Elapsed:g}");
  27. void GenerateAndCollect()
  28. {
  29. var container = new Container();
  30. container
  31. .Padding(10)
  32. .MinimalBox()
  33. .Border(1)
  34. .Column(column =>
  35. {
  36. const int numberOfRows = 100;
  37. const int numberOfColumns = 10;
  38. for (var y = 0; y < numberOfRows; y++)
  39. {
  40. column.Item().Row(row =>
  41. {
  42. for (var x = 0; x < numberOfColumns; x++)
  43. {
  44. row.RelativeItem()
  45. .Background(Colors.Red.Lighten5)
  46. .Padding(3)
  47. .Background(Colors.Red.Lighten4)
  48. .Padding(3)
  49. .Background(Colors.Red.Lighten3)
  50. .Padding(3)
  51. .Background(Colors.Red.Lighten2)
  52. .Padding(3)
  53. .Background(Colors.Red.Lighten1)
  54. .Padding(3)
  55. .Background(Colors.Red.Medium)
  56. .Padding(3)
  57. .Background(Colors.Red.Darken1)
  58. .Padding(3)
  59. .Background(Colors.Red.Darken2)
  60. .Padding(3)
  61. .Background(Colors.Red.Darken3)
  62. .Padding(3)
  63. .Background(Colors.Red.Darken4)
  64. .Height(3);
  65. }
  66. });
  67. }
  68. });
  69. ElementCacheManager.Collect(container);
  70. }
  71. }
  72. }
  73. }