TextBenchmark.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using NUnit.Framework;
  8. using QuestPDF.Drawing;
  9. using QuestPDF.Examples.Engine;
  10. using QuestPDF.Fluent;
  11. using QuestPDF.Helpers;
  12. using QuestPDF.Infrastructure;
  13. namespace QuestPDF.Examples
  14. {
  15. public class TextBenchmark
  16. {
  17. [Test]
  18. public void Benchmark()
  19. {
  20. var subtitleStyle = TextStyle.Default.Size(24).SemiBold().Color(Colors.Blue.Medium);
  21. var normalStyle = TextStyle.Default.Size(14);
  22. var chapters = GetChapters().ToList();
  23. var results = PerformTest(1).ToList();
  24. var a = CanvasCache.Paints;
  25. Console.WriteLine($"Min: {results.Min():F}");
  26. Console.WriteLine($"Max: {results.Max():F}");
  27. Console.WriteLine($"Avg: {results.Average():F}");
  28. IEnumerable<(string title, string content)> GetChapters()
  29. {
  30. var book = File.ReadAllLines("quo-vadis.txt");
  31. var chapterPointers = book
  32. .Select((line, index) => new
  33. {
  34. LineNumber = index,
  35. Text = line
  36. })
  37. .Where(x => x.Text.Length < 50 && x.Text.Contains("Rozdział") || x.Text.Contains("-----"))
  38. .Select(x => x.LineNumber)
  39. .ToList();
  40. foreach (var index in Enumerable.Range(0, chapterPointers.Count - 1))
  41. {
  42. var chapter = chapterPointers[index];
  43. var title = book[chapter];
  44. var lineFrom = chapterPointers[index];
  45. var lineTo = chapterPointers[index + 1] - 1;
  46. var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom);
  47. var content = string.Join(Environment.NewLine, lines);
  48. yield return (title, content);
  49. }
  50. }
  51. void GenerateDocument()
  52. {
  53. RenderingTest
  54. .Create()
  55. .PageSize(PageSizes.A4)
  56. .FileName()
  57. .ProducePdf()
  58. .Render(ComposePage);
  59. }
  60. IEnumerable<float> PerformTest(int attempts)
  61. {
  62. foreach (var i in Enumerable.Range(0, attempts))
  63. {
  64. var timer = new Stopwatch();
  65. timer.Start();
  66. GenerateDocument();
  67. timer.Stop();
  68. Console.WriteLine($"Attempt {i}: {timer.ElapsedMilliseconds:F}");
  69. yield return timer.ElapsedMilliseconds;
  70. }
  71. }
  72. void ComposePage(IContainer container)
  73. {
  74. container
  75. .Padding(50)
  76. .Decoration(decoration =>
  77. {
  78. decoration
  79. .Content()
  80. .Stack(stack =>
  81. {
  82. stack.Item().Element(Title);
  83. stack.Item().PageBreak();
  84. stack.Item().Element(TableOfContents);
  85. stack.Item().PageBreak();
  86. Chapters(stack);
  87. });
  88. decoration.Footer().Element(Footer);
  89. });
  90. }
  91. void Title(IContainer container)
  92. {
  93. container
  94. .Extend()
  95. .PaddingBottom(200)
  96. .AlignBottom()
  97. .Stack(stack =>
  98. {
  99. stack.Item().Text("Quo Vadis", TextStyle.Default.Size(72).Bold().Color(Colors.Blue.Darken2));
  100. stack.Item().Text("Henryk Sienkiewicz", TextStyle.Default.Size(24).Color(Colors.Grey.Darken2));
  101. });
  102. }
  103. void TableOfContents(IContainer container)
  104. {
  105. container.Stack(stack =>
  106. {
  107. stack.Item().Text("Table of contents", subtitleStyle);
  108. stack.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  109. foreach (var chapter in chapters)
  110. {
  111. stack.Item().InternalLink(chapter.title).Row(row =>
  112. {
  113. row.RelativeColumn().Text(chapter.title);
  114. row.ConstantColumn(100).AlignRight().Text(text => text.PageNumberOfLocation(chapter.title, normalStyle));
  115. });
  116. }
  117. });
  118. }
  119. void Chapters(StackDescriptor stack)
  120. {
  121. foreach (var chapter in chapters)
  122. {
  123. stack.Item().Element(container => Chapter(container, chapter.title, chapter.content));
  124. }
  125. }
  126. void Chapter(IContainer container, string title, string content)
  127. {
  128. container.Stack(stack =>
  129. {
  130. stack.Item().Location(title).Text(title, subtitleStyle);
  131. stack.Item().PaddingTop(10).PaddingBottom(50).BorderBottom(1).BorderColor(Colors.Grey.Lighten2).ExtendHorizontal();
  132. stack.Item().Text(text =>
  133. {
  134. text.ParagraphSpacing(10);
  135. text.Span(content, normalStyle);
  136. });
  137. stack.Item().PageBreak();
  138. });
  139. }
  140. void Footer(IContainer container)
  141. {
  142. container
  143. .AlignCenter()
  144. .Text(text =>
  145. {
  146. text.CurrentPageNumber();
  147. text.Span(" / ");
  148. text.TotalPages();
  149. });
  150. }
  151. }
  152. }
  153. }